×

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

 

 


Related Topics

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

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...

6 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.