Selenium WebDriver- Commands
As we have discussed earlier in the IDE section, Selenium commands are the set of commands that are used to run our Selenium tests.
In Selenium WebDriver, we have an entirely different set of commands for performing different operations. Since we are using Selenium WebDriver with Java, commands are simply methods written in Java language.
Note: A java method is a collection of statements that are grouped together to perform a specific operation.
Before getting into the details of commands provided by Selenium WebDriver, we insist you to go through the Java OOPs (Object-Oriented Programming) concepts in java programming language. You can also refer to our Java OOPs concepts section provided in the Java Tutorial.
Now, the question arises is that how can we access methods provided by WebDriver.
Till now, we have successfully created our first test script in Selenium WebDriver. Therefore, one possible way to view the methods provided by WebDriver is to open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver and press the dot key. It will show you all of the possible methods provided by WebDriver.
Let us consider an example form the suggestions displayed by Eclipse to understand the syntax of the methods provided by WebDriver.
Method Name
To access any method of any class, we need to create an object of class and then all the public methods will appear for the object.
Parameter
A parameter is an argument which is passed to a method to perform some specific operation.
Return type
Methods can return a value or returning nothing (void). If the void is mentioned after the method, it means, the method is returning no value. If it is returning a value, then it must display the type of the value for e.g. getTitle(): String.
Now, we will discuss the various commands provided by WebDriver. The commands provided by Selenium WebDriver can be broadly classified in following categories:
Given below are some of the most commonly used Selenium commands in WebDriver:
1. Fetching a web page
There are two methods to fetch a web page:
- Using Get method
- Using Navigate method
2. Locating forms and sending user inputs
3. Clearing User inputs
The clear() method is used to clear the user inputs from the text box.
4. Fetching data over any web element
Sometimes we need to fetch the text written over a web element for performing some assertions and debugging. We use getText() method to fetch data written over any web element.
5. Performing Click event
The click() method is used to perform click operation on any web element.
6. Navigating backward in browser history
7. Navigating forward in browser history
8. Refresh/ Reload a web page
9. Closing Browser
10. Closing Browser and other all other windows associated with the driver
11. Moving between Windows
13. Moving between Frames
14. Drag and Drop
Drag and Drop operation is performed using the Action class.
Let us consider a sample test script which will cover most of the commonly used WebDriver commands.
For our test purpose, we are using a dummy web page under the URL:
https://www.testandquiz.com/selenium/testing.html
The default interface of the web page looks like this:
You can also use this dummy web page for your Selenium Testing practice.
First you need to download the browser driver for the browser on which you are willing to automate your test scenarios. We have already discussed execution of Selenium test scripts on different browsers in the previous sections of this tutorial.
For this test, we are using Firefox Gecko driver to automate our test scenarios on Firefox browser.
Below is the sample test script with embedded comments.