Home >>Selenium Tutorial >Selenium WebDriver - Browser Commands

Selenium WebDriver - Browser Commands

Selenium WebDriver - Browser Commands

WebDriver's very simple app operations involve launching an app; executing only a few tasks and then shutting the app.

Provided are some of the Browser Commands for Selenium WebDriver that are most used.

1. Get Command

Method:

get(String arg0) : void  

This method loads a new web page into the existing browser window within WebDriver. String is accepted as a parameter, and returns void.

You may compose the respective command to load a new web page as:

driver.get(URL);     
  
// Or can be written as  
       
String URL = "URL";  
driver.get(URL);  

Example: For example, command to load phptpoint's official website may be written as:

driver.get("www.phptpoint.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.

You may compose the respective command to retrieve the current page title as:

driver.getTitle();    
         
    // Or can be written as  
       
 String Title = driver.getTitle();  

3. Get Current URL Command

Method:

getCurrentUrl(): String  

This method fetches the string representing the current web page 's Current URL in WebDriver. Nothing is acknowledged as a parameter and returns a String value.

To get the string describing the name and address, the respective command 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 latest web page loaded on the current browser. Everything is acknowledged as a parameter and returns a String value.

The respective order 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 approach terminates the latest browser window running via WebDriver at the current time. If the current window is the only window operating by WebDriver, it terminates the browser as well. This method will not consider anything as a parameter and will return empty.

The various browser window closure instruction 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 does not consider anything as a parameter, and returns void.

The corresponding order can be written to close all windows as:

driver.quit(); 

Let's consider a sample test script that will cover most of WebDriver's Browser Commands.

We will automate the following test scenarios into this sample test:

  • Invoke Chrome Browser
  • Open URL: https://www.google.co.in/
  • The Page User Description and Length of Title
  • Scan description and section length of the page on the Eclipse Console
  • Get the URL page and check whether it is the page you want, or not
  • Get the Source and Page Length page
  • Print Eclipse Console Page Length.
  • Close your Browser

For our test function, we are using the home page of "Google" search engine.

We will build our test case step by step to provide you a full understanding of how to use User Commands in WebDriver.

Step 1. Launch Eclipse IDE and open the current "Demo_Test" test suite which we created in the WebDriver tutorial section of WebDriver Installation.

Step 2. Right click on the "src" folder and create a new Class File from New > Class. Give your Class name as "Navigation_command" and click on "Finish" button.

Step 3. Let's get to the coding ground.

Throughout later sections of this article we suggested the protocols and strategies for operating our experiments on various browsers. You should go through each of them for comparison before continuing 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 the Google Chrome browser, the ChromeDriver.exe file must be downloaded and the system properties mapped to the direction of the ChromeDriver.exe file. We have discussed this in earlier tutorial sessions already. You can also respond to "Chrome Browser running test" to learn how to download and install Chrome driver's System rights.

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 all of the code blocks above, we'll get the code snippet for Google Chrome apps to launch.

// System Property for Chrome Driver   
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");                    
// Instantiate a ChromeDriver class.      
WebDriver driver=new ChromeDriver(); 
  • To addition to simplify our second test case, i.e. "Get Page Title Name and Title Length," the title name and duration must be stored to 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 comparison scenario requires that the URL be fetched and tested against the real URL.

First, we store the current URL in a variable String:

// 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 ");  
}  
Else  
{  
System.out.println("Verification Failed ");  
}  

To simplify our 6th test scenario (Get Source page and Source page length), we must store the source page and source page length respectively in the string and in the int variable.

// Storing Page Source in String variable  
String pageSource = driver.getPageSource();  
           
// Storing Page Source length in Int variable  
int pageSourceLength = pageSource.length(); 

Follow the provided code snippet to print the duration of the Page source on the console window:

// Printing length of the Page Source on console  
System.out.println("Total length of the Page Source is : " + pageSourceLength);  
  • The provided code snippet finishes the process and closes the browser. driver.close();

Combining all the above code blocks together, we can get the source code needed to execute our "Web command" test script

Anything similar will appear in the final test script:

(We embedded comments in every section to explicitly explain the steps)

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");  
}  
else{  
  
System.out.println("Verification Failed ");  
         }  
       
// 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();   
}  
}  

No Sidebar ads