Selenium WebDriver - Browser Commands
The very basic browser operations of WebDriver include opening a browser; perform few tasks and then closing the browser.
Given are some of the most commonly used Browser commands for Selenium WebDriver.
1. Get Command
In WebDriver, this method loads a new web page in the existing browser window. It accepts String as parameter and returns void.
The respective command to load a new web page can be written as:
Example: For instance, the command to load the official website of javaTpoint can be written as:
2. Get Title Command
In WebDriver, this method fetches the title of the current web page. It accepts no parameter and returns a String.
The respective command to fetch the title of the current page can be written as:
3. Get Current URL Command
In WebDriver, this method fetches the string representing the Current URL of the current web page. It accepts nothing as parameter and returns a String value.
The respective command to fetch the string representing the current URL can be written as:
4. Get Page Source Command
In WebDriver, this method returns the source code of the current web page loaded on the current browser. It accepts nothing as parameter and returns a String value.
The respective command to get the source code of the current web page can be written as:
5. Close Command
This method terminates the current browser window operating by WebDriver at the current time. If the current window is the only window operating by WebDriver, it terminates the browser as well. This method accepts nothing as parameter and returns void.
The respective command to terminate the browser window can be written as:
6. Quit Command
This method terminates all windows operating by WebDriver. It terminates all tabs as well as the browser itself. It accepts nothing as parameter and returns void.
The respective command to terminate all windows can be written as:
Let us consider a sample test script in which will cover most of the Browser Commands provided by WebDriver.
In this sample test, we will automate the following test scenarios:
For our test purpose, we are using the home page of "Google" search engine.
We will create our test case step by step to give you a complete understanding on how to use Browser Commands in WebDriver.
Give your Class name as "Navigation_command" and click on "Finish" button.
Step3. Let's get to the coding ground.
To automate our test scenarios, first you need to know "How to invoke/launch web browsers in WebDriver?"
Note: To invoke a browser in Selenium, we have to download an executable file specific to that browser. For example, Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. These executable files start a server on your system which in turn is responsible for running your test scripts in Selenium.
We have stated the procedures and methods to run our tests on different browser in later sections of this tutorial. For reference you can go through each one of them before proceeding with the actual coding.
Here is the sample code to set system property for Chrome driver:
After that we have to initialize Chrome driver using ChromeDriver class.
Here is the sample code to initialize Chrome driver using ChromeDriver class:
Combining both of the above code blocks, we will get the code snippet to launch Google Chrome browser.
Here is the sample code to do that:
To print page Title name and Title length in the Console window, follow the given code snippet:
First, we will store the current URL in a String variable:
Verifying the current URL as the actual URL:
To print the length of the Page source on console window, follow the given code snippet:
Combining all of the above code blocks together, we will get the required source code to execute our test script "Web_command".
The final test script will appear something like this:
(We have embedded comment in each section to explain the steps clearly)
To run the test script on Eclipse window, right click on the screen and click
Run as → Java application
After execution, the test script will launch the chrome browser and automate all the test scenarios. The console window will show the results for print commands.