Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Selenese
2.1.
Uses of Selenese
2.2.
Types of Selenese 
3.
Locator
4.
Actions
4.1.
open(url)
4.2.
type (target, text)
4.3.
sendKeys(target, text)
5.
Accessors
5.1.
storeTitle (target, pattern)
5.2.
storeText (target, pattern)
5.3.
storeValue(target, pattern)
5.4.
storeChecked (target, variable) 
6.
Assertions
6.1.
verifyText (locator, text)
6.2.
WaitForPageToLoad (max. time to wait in milliseconds) 
6.3.
waitForVisible (locator) 
7.
Frequently Asked Questions
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Selenium Commands and Types

Author Toohina Barua
0 upvote

Introduction

Selenium is an open-source umbrella project for a collection of browser automation tools and libraries. It gives you a playback tool for writing functional tests without learning a test scripting language (Selenium IDE). It also includes a test domain-specific language (Selenese) for writing tests in JavaScript (Node.js), C#, Groovy, Java, Perl, PHP, Python, Ruby, and Scala. Most modern web browsers can then be used to run the tests. 
We are going to learn about Selenium commands and types from this article. So let us dive in.

Selenese

Selenium commands, also known as Selenese, are a set of commands that run your tests in the Selenium IDE. 
Window size, mouse options, alerts, Ajax functionality, pop-up windows, event handling, and various other web application features are all tested.
A test script comprises a series of Selenium commands (Selenese).

Uses of Selenese

Selenese can be used for a variety of tasks, including:

  1. Using HTML tags to check for the presence of UI elements.
  2. Look for specific content to test.
  3. Look for any broken links.
  4. You'll be testing input fields, selection list options, submitting forms, and table data.

Types of Selenese 

The commands in Selenium are divided into three categories:

  1. Actions
  2. Accessors
  3. Assertions

Locator

Selenium uses Element Locators to figure out which HTML element the command refers to. These locators can be found using Mozilla's FirePath and FireBug plugins.

  • identifier = id: Select the element with the specified "id" attribute or the first element whose @name attribute is id if there is no match.
  • id = id: Select the element with the "id" attribute specified.
  • name = name: Choose the first element that has the "name" attribute set.
  • dom = javascriptExpression: Selenium locates an element by evaluating a string, allowing us to traverse the HTML Document Object Model using JavaScript. Users cannot return a value, but they can assess the block as an expression.
  • xpath = xpathExpression: Using an XPath expression, locate an element.
  • link = textPattern: Chooses the link element (within anchor tags) that has text that matches the pattern.
  • css = cssSelectorSyntax: CSS selector is used to select the element.

Actions

The selenium commands that manipulate the application's state are known as actions. Click this link, select that option, type this box, and other events are generated by the execution of actions. If an Action fails or contains a bug, the current test is terminated.

The following are some of the most commonly used Actions commands:

open(url)

This command in the Selenium IDE software testing tool is used the most. The "Open" command opens the URL in the selected browser tab. The open command accepts either a full URL (recommended) or a path relative to the base URL as input (outdated).

type (target, text)

TYPE changes the value of an input field to make it appear as if you typed it in. It can also be used to change the value of combo boxes, check boxes, and other similar controls. The value of the option selected, not the visible text, should be used in these cases. Only in Chrome: If a file path is provided (for type=file), it will be uploaded to the input. Also supported are XPath locators.

sendKeys(target, text)

The sendKeys command simulates keystroke events on the specified element as if you were manually typing the value. This simulates a real user typing every character in the specified string; however, it is limited by real-world constraints, such as the inability to type into invisible or read-only elements. This is useful for dynamic UI widgets that require explicit key events, such as auto-completing combo boxes. Unlike the simple "type" command, which inserts the specified value into the page without replacing the existing content, this command does not do so.

Accessors

The selenium commands that examine the application's state and store the results in variables are known as accessors. They're also used to generate Assertions automatically.

Some of the most commonly used Accessors commands include:

storeTitle (target, pattern)

In a variable, the storeTitle command saves the title of the currently open software web application.

storeText (target, pattern)

The Selenium IDE software testing tool's storeText command can be used to save the text value of a page element in a variable for later use. As a result, it's the command to use when scraping data from HTML text and tables.

storeValue(target, pattern)

The storeValue command is used to save an element's value in a variable for later use. Because text is the value in website input boxes, storeValue is used to retrieve text from within them. In other words, use storeValue to scrape data from input boxes, select boxes, and textareas on the web.

storeChecked (target, variable) 

The storeChecked command determines whether a check box or radio button is active and stores the value 'true' or 'false' in a variable. If the element is checked, it will store 'true' in the variable, and if the element is not checked or unchecked, it will store 'false'. Later on, you can use that value with commands like if and gotoIf.

Assertions

Assertions are commands that allow testers to verify the application's state. There are three types of assertions: assert, verify, and waitfor.

verifyText (locator, text)

Both assertText and verifyText get the text of an element (as defined by the locator) and check whether it satisfies the pattern's requirements. This works for any text-containing element.

The assert and verify commands are both useful for determining whether or not conditions match. The difference is that the verify command will check the condition, and if it doesn't match, it will only display an error message in the log area, while the macro will continue to run. If the condition in the assert command does not match, the remaining macro execution in the Selenium IDE software testing tools will be stopped.

WaitForPageToLoad (max. time to wait in milliseconds) 

WaitForPageToLoad instructs the IDE to keep waiting for a web page to load (page load event fired). It is usually unnecessary because all "...andWait" commands, such as ClickandWait, include it. As a result, you'll only need it in exceptional circumstances, such as when a website fires multiple page load events.

waitForVisible (locator) 

The waitForVisible property instructs the IDE to wait for an element to exist and be visible before proceeding. This command will wait until the element is visible on the page before it is executed. When the command appears on the page, the UI.Vision RPA selenium IDE moves on to the next command.

Check this out : Xpath in Selenium

Frequently Asked Questions

  1. What is the difference between type and sendKeys command?
    The sendKeys command simulates keystroke events on the specified element, as though you typed the value key-by-key. Unlike the simple "type" command, which forces the specified value into the page directly, this command will not replace the existing content.
     
  2. What should we use if sendKeys do not work?
    If Sendkeys does not work for you, use XType.
     
  3. What does verifyElementPresent command do?
    The verifyElementPresent checks if the element exists on the page and logs error if not.
     
  4. What does the AssertChecked command do?
    The AssertChecked command continues test only if a checkbox or radio button is checked.
     
  5. What does the storXpathCount do?
    In Selenium IDE, the storeXpathCount command retrieves and stores the count of UI elements found using the given XPath locating strategy into a variable.

Conclusion

In this article, we had a look at Selenium commands their uses and types. we also saw a few important commands.
We hope that this blog has helped you enhance your knowledge regarding Selenium Commands and Types and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Recommended Reading: 

Nmap commands

Live masterclass