TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium WebDriver - Browser Commands

Selenium WebDriver - Browser Commands with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

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:

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 javaTpoint can be written as:

driver.get("www.TheDeveloperBlog.com")

2. Get Title Command

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();

3. Get Current URL Command

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();	

4. Get Page Source Command

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();

5. Close Command

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();

6. Quit Command

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:

  • 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:

// 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();
  • 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:

// 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);
  • 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:

// 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.");
}
  • 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.
// 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);
  • Finally, the given code snippet will terminate the process and close the browser.
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

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



Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf