×

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

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

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

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.

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.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

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

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes 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: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

Selenium WebDriver &ndash; Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

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

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

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.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.