Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands

WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc.

Some of the most commonly used Browser controls commands for Selenium WebDriver is as follows:-

Browser Controls Commands
  • get() Command:

The get() method is used to navigate any URL, but the URL should start with the Http because web driver uses the http protocol for internal browser communication.

Syntax:

get(String arg0)void

The return type of get command is as follow:

Parameter Return Type
String void

E.g.: To load a new web page using get command, we are loading the official website of tutorialandexample.com

driver.get(“URL”);
 driver.get("https://www.tutorialandexample.com")
or
String URL="URL";
driver.get(URL);
  • getTitle() Command:

The getTitle() method is used to capture the title of the current web page.           

Syntax:

getTitle(): String

The return type of getTitle():

Parameter Return Type
No parameter usage String

To capture the current title of web page:

driver.getTitle();
//or
StringTitle=driver.getTitle();
  • getCurrentURL() Command :

The getCurrentUrl() method is used to capture the URL of the current web pages.

Syntax:

getCurrentUrl(): String

The return type of getCurrentUrl():

Parameter Return Type
No parameter usage String

To fetch the current URL of the web page:

driver.getCurrentUrl();
//Or
String CurrentUrl = driver.getCurrentUrl();
  • getPageSource() Command:

The getPageSource() command is used to capture the HTML source code from the current page.

Syntax:

getPageSource(): String

The Return type of getPgeSource():

Parameter Return Type
No parameter usage String

To fetch the source code of the current web page:

driver.getPageSource();
//or
String PageSource = driver.getPageSource();
  • close() Command:

The close command is used to terminate the current browser window at the current time.

Syntax:

close():void 
Parameter Return Type
No parameter usage Void

To terminate the browser window:

driver.close();
  • quit() Command:

Quit method is used to terminate all windows. It will close all the browsers that are opened.

Syntax:

quit(): void 
Parameter Return Type
No parameter usage Void

To terminate all windows:

driver.quit();

Let us take a sample test script in which will try to cover most commonly used browser commands:

In this test, we will automate the following test scenarios:

Steps Actions Method used Expected result
1. Invoke the Google Chrome Browser     The Chrome browser must be opened.
2. Open URL: https://www.facebook.com   get() The Facebook login page must be opened.
3. Get Current Page Title name.   getCurrentTitle() The current page title name must be displayed.
4. Printing the Page Title on the Eclipse IDE Console     The title should be printed on the eclipse console.
5. Get Current page URL and verify whether it is the desired page or not.   getCurrentUrl() The current page URL must be verified.
6. Get page Source and Page Source length.   getPageSource() The page source must be displayed with source length.
7. Printing the page Length on Eclipse IDE Console.    The page length should be printed on the eclipse console.
8. Close the Browser.   close() The browser should be closed.

For our testing purpose, we will test the home page of a Facebook application.

We are creating our test case step by step to give you a complete understanding of the browser Commands in WebDriver.

  • Then, right-click on the src folderand create a new Class file from New à Class.
src folder and create a new Class file
  • And, give your Class name as Browser_Command and click on the Finish button.
Class name as Browser_Command

Step1:

To automate the test scenarios, first, we need to learn "How to launch web browsers in WebDriver?"

Note: To access a browser in Selenium, we have to download an executable file that is specific to that browser.

E.g., the Chrome browser executed the WebDriver protocol using an executable file called ChromeDriver.exe.

These executable files start a server on our system, which is responsible for running our test scripts in Selenium.

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

You can also refer to Web driver on Chrome Browser to learn how to download and how to set the System property for Chrome driver.

Hence, here is the code for setting the system property for google chrome:

//set the system property of Google Chrome
System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"); 

To initialize the Chrome driver using ChromeDriver class:

// create driver object for CHROME browser
 WebDriver driver=new ChromeDriver(); 

Step2:

To launch the website:

driver.get("https://www.facebook.com");

Step3:

Now, we are automating the third test scenario i.e. Get Page Title name,

//get the current page title
String title=driver.getTitle(); 

Step4:

To print page Title name in the Console, follow the given code:

// print the title on the console
System.out.println("title of the current page is :- " + title );

Step5:

The next test scenario is to fetch the URL and to verify it against the actual URL.

First, we will store the current URL:

//get the current page URL
String Url =driver.getCurrentUrl(); 

Here, we are verifying the URL :

if (Url.equals("https://www.facebook.com/")) 
{ 
System.out.println("The correct Url is opened");
}
else{
System.out.println(" An incorrect Url is opened"); 
 }

Step6:

To automate our next test scenario, which is Get page Source and Page Source length, first, we will get the current page source and page source length in the string and int variable, respectively.

// get the current page source code
 String Sourcecode = driver.getPageSource(); 
 // getting Source code length in Int variable 
 int SourcecodeLength = Sourcecode.length();  

Step7:

For printing the length of the page source code in the output console window:

// Print the length of a Page Source code on the console
 System.out.println("length of the Page Source is :- " + SourcecodeLength);

Step8:                                                                                 

Finally, we terminate the process and close the browser.

//Closing the browser
 driver.close();

Now, we are combining all the above code of blocks, and the final test script will look like this:

package testpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Browser_command {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe");
// create driver object for CHROME browser
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com");
//get the current page title
String title=driver.getTitle();
// print the title in the eclipse console
System.out.println("title of the current page is " + title );
//get the current page URL
String Url =driver.getCurrentUrl();
// printing the URL in the console
System.out.println("current url of the page " + Url);
if (Url.equals("https://www.facebook.com/"))
{
System.out.println("The correct Url is opened");
}
else {
System.out.println(" An incorrect Url is opened");
}
// get the current page source code
String Sourcecode = driver.getPageSource();
// getting the Source code length in int variable
int SourcecodeLength = Sourcecode.length();
// Printing the length of the Page Source
 System.out.println("Total length of the page" + SourcecodeLength);
//Closing the browser
driver.close();
}
}
  • Now, right-click on the window and click Run as > Java application
Run as Java Application
  • The test script will be launched in the chrome browser and automate all the test scenarios.
  • And the output of the test script will show the results for all the print commands.
script will be launched in the chrome browser and automate