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.
Method:
get(String arg0) : void
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:
driver.get(URL); // Or can be written as String URL = "URL"; driver.get(URL);
Example: For instance, the command to load the official website of rookienerd can be written as:
driver.get("www.rookienerd.com")
Method:
getTitle(): String
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:
driver.getTitle(); // Or can be written as String Title = driver.getTitle();
Method:
getCurrentUrl(): String
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:
driver.getCurrentUrl(); //Or can be written as String CurrentUrl = driver.getCurrentUrl();
Method:
getPageSource(): String
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:
driver.getPageSource(); //Or can be written as String PageSource = driver.getPageSource();
Method:
close(): void
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:
driver.close();
Method:
quit(): void
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:
driver.quit();
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?"
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:
// System Property for Chrome Driver System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
After that we have to initialize Chrome driver using ChromeDriver class.
Here is the sample code to initialize Chrome driver using ChromeDriver class:
// Instantiate a ChromeDriver class. WebDriver driver=new ChromeDriver();
Combining both of the above code blocks, we will get the code snippet to launch Google Chrome browser.
// System Property for Chrome Driver System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe"); // Instantiate a ChromeDriver class. WebDriver driver=new ChromeDriver();
Here is the sample code to do that:
// Storing Title name in the String variable String title = driver.getTitle(); // Storing Title length in the Int variable int titleLength = driver.getTitle().length();
To print page Title name and Title length in the Console window, follow the given code snippet:
// Printing Title & Title length in the Console window System.out.println("Title of the page is : " + title); System.out.println("Length of the title is : "+ titleLength);
First, we will store the current URL in a String variable:
// Storing URL in String variable String actualUrl = driver.getCurrentUrl();
Verifying the current URL as the actual URL:
if (actualUrl.equals("https://www.google.co.in")) { System.out.println("Verification Successful - The correct Url is opened."); } Else { System.out.println("Verification Failed - An incorrect Url is opened."); }
// Storing Page Source in String variable String pageSource = driver.getPageSource(); // Storing Page Source length in Int variable int pageSourceLength = pageSource.length();
To print the length of the Page source on console window, follow the given code snippet:
// Printing length of the Page Source on console System.out.println("Total length of the Page Source is : " + pageSourceLength);
driver.close();
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)
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Web_command { public static void main(String[] args) { // System Property for Chrome Driver System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe"); // Instantiate a ChromeDriver class. WebDriver driver=new ChromeDriver(); // Storing the Application Url in the String variable String url = ("https://www.google.co.in/"); //Launch the ToolsQA WebSite driver.get(url); // Storing Title name in the String variable String title = driver.getTitle(); // Storing Title length in the Int variable int titleLength = driver.getTitle().length(); // Printing Title & Title length in the Console window System.out.println("Title of the page is : " + title); System.out.println("Length of the title is : "+ titleLength); // Storing URL in String variable String actualUrl = driver.getCurrentUrl(); if (actualUrl.equals("https://www.google.co.in/")){ System.out.println("Verification Successful - The correct Url is opened."); } else{ System.out.println("Verification Failed - An incorrect Url is opened."); } // Storing Page Source in String variable String pageSource = driver.getPageSource(); // Storing Page Source length in Int variable int pageSourceLength = pageSource.length(); // Printing length of the Page Source on console System.out.println("Total length of the Pgae Source is : " + pageSourceLength); //Closing browser driver.close(); } }
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.