Javatpoint Logo
Javatpoint Logo

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

Method:

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

Method:

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

Method:

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

Method:

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

Method:

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

Method:

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:

  • Invoke Chrome Browser
  • Open URL: https://www.google.co.in/
  • Get Page Title name and Title length
  • Print Page Title and Title length on the Eclipse Console
  • Get page URL and verify whether it is the desired page or not
  • Get page Source and Page Source length
  • Print page Length on Eclipse Console.
  • Close the Browser

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.

  • Step1. Launch Eclipse IDE and open the existing test suite "Demo_Test" which we have created in WebDriver Installation section of WebDriver tutorial.
  • Step2. Right click on the "src" folder and create a new Class File from New > Class.
Selenium WebDriver - Browser Commands

Give your Class name as "Navigation_command" and click on "Finish" button.

Selenium WebDriver - Browser Commands

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.

  1. Running test on Firefox
  2. Running test on Chrome
  3. Running test on Internet Explorer
  4. Running test on Safari
  • To invoke Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of your ChromeDriver.exe file. We have already discussed this in earlier sessions of this tutorial. You can also refer to "Running test on Chrome Browser" to learn how to download and set System property for Chrome driver.

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.

  • To automate our second test scenario i.e. "Get Page Title name and Title length", we have to store the title name and length in string and int variable respectively.

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:

  • The next test scenario requires fetching the URL and verifying it against the actual URL.

First, we will store the current URL in a String variable:

Verifying the current URL as the actual URL:

  • To automate our 6th test scenario (Get page Source and Page Source length), we will store the page source and page source length in the string and int variable respectively.

To print the length of the Page source on console window, follow the given code snippet:

  • Finally, the given code snippet will terminate the process and close the browser.

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

Selenium WebDriver - Browser Commands

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.

Selenium WebDriver - Browser Commands



Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA