Introduction to Selenium Commands
What is Selenium?
Selenium is an open source automation testing tool. Selenium can be said as a suite of different software which mainly consists of four components. These components are Selenium Integrated Development Environment (IDE), Selenium Remote Control (RC), WebDriver and Selenium Grid. It is mainly used for automating web applications for different testing purposes. It is supported by all major browsers and supports all APIs and frameworks. Selenium has many commands which make it easy to use. Let us have a look at a few Selenium commands which will help you in your automation and make it quicker.
Basic Selenium Commands
- Get method: This method is a part of the driver class and can be accessed by using driver.get() method. It can be used to navigate to a web page by passing a URL as a parameter.
- Navigate method: This method like ‘get’ is a part of same class and the driver.navigate().to() method opens a web page like driver.get() method.
- Writing in a textbox: The sendKeys() method help in writing in a textbox. It can also act for entering any element of text input type.
- Refreshing the browser: A browser can be refreshed in multiple ways. These are listed as below:
- Using navigate().refresh() command
- Using sendKeys(Keys.F5) on any textbox on the webpage
- Using get(“URL”) with current URL
- Using navigate().to(“URL”) with current URL
- Close command: This Selenium command helps in killing the window which is currently working and over which the WebDriver has the control. The return type for this command is void.
- Get Title command: This method helps in returning the title string of the current page in the browser. The return type for this method is a string. It is advisable to save this object as a string object or variable.
- Get Current URL command: This method is used when the string containing the URL currently opened in the browser in the window. It is useful for logging purposes.
- Get Page Source command: This method helps in getting the source code of a web page. This returns a string value.
- void forward(): This Selenium command helps in simulating the window to browse to the forward button action of a browser. It is supposed to move forward by a single page into the browser’s history data.
- Click Command: This method is used to perform the click action by the web element. This is one of the most common methods which interact with web elements like checkboxes, buttons, windows, etc.
Intermediate Selenium commands
The following commands should be known to you once you are comfortable with the basic Selenium commands.
- GetWindowHandle Command: This Selenium command helps to get the window handle of the current window.
- GetWindowHandles Command: This command helps in getting window handles of all current windows that are open in a browser.
- SwitchTo Window Command: This Web driver command helps in moving between named windows by using the switchTo method. A user can also send a window handle to switchto().window() method where it is also possible to iterate over any open window as well.
- SwitchTo Frame command: By using this command it is possible to move between named frames by using the SwitchTo method.
- SwitchTo PopUp Command: Similar to above switchTo commands, this method can also help in switching PopUp windows. Once this action is triggered then a pop up is opened and it can be accessed by an alert and it will return the currently open alert object. With the help of this object, the user can accept, dismiss or read the contents into any prompt.
- IsDisplayed Command: This method helps in checking whether an element is visible or not. It returns true if the element is present and also visible on the page. It throws the necessary exception if the element is not present.
- IsEnabled: This method returns either true or false based on the state of the element. It mostly returns true for all items except for those which are intentionally disabled.
- Submit command: This method helps in the initiation of submission of an HTML form. It does not allow any parameter and if it is leading to the current page then it will wait until the new page is loaded.
- GetCssValue Command: This method provides the value of the CSS property which belongs to the element.
- GetLocation Command: This method helps in getting the location of any element on the page. The user can easily get the X and Y coordinates of any specified element.
Advanced Selenium Commands
Below are some advanced commands in selenium:
Handling iframes by ID or Name
To identify an iframe the user must identify the ID or Name of the frame which is to be used. To do this user must first inspect the web page by using web inspector tools. Once this is done following web driver command can be used to switch between different iframes.
There may be cases where programs get interrupted and these interruptions can be said as exceptions. Like other languages, Selenium web driver also supports exception handling. It can catch a single exception or even multiple exceptions.
Handling waits and conditions
There are different commands which can handle waits. They can be named as:
PageLoadTimeOut(time,unit) and implicitlyWait()
Similarly for Conditions below can be used:
isDisplayed(), isEnabled(), isSelected()
Tips and Tricks to use Selenium Commands
- The best method for creating Web driver Instance
By making use of <Factory> design template user can create object for a particular browser type.
- Method to check if an element exists
The user can make use of <findElements> to check if an element exists or not.
4.5 (1,978 ratings)
- Avoiding exceptions and checking for an element together
The above tag may lead to <NoSuchElementException>. By making use of wait feature this error can be avoided and it also allows to test the web element.
By making using of Selenium and these commands any web application can be automated easily. Selenium being an open source tool is easily available and hence can be used for testing of all web-based applications. Above commands can briefly help you get through the basics and advanced commands that are used in Selenium.
This has been a guide to Selenium Commands. Here we have discussed basic as well as advanced Selenium Commands and some immediate Selenium commands. You may also look at the following article to learn more –