WebDriver Browser Commands – get, getTitle, getCurrentUrl, getPageSource, getClass, close, quit in Selenium WebDriver

WebDriver is a tool for automating web application testing, and in particular, to verify that they work as expected. WebDriver is design to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was develop to better support dynamic web pages where elements of a page may change without the page itself being reload. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems. 

In this tutorial, we are discussing about below mentioned topics:-
1) How to access the methods of WebDriver?
2) Various Browser Commands

  • get()
  • getTitle()
  • getCurrentUrl()
  • getPageSource()
  • getClass()
  • close()
  • quit()

How to access the methods of WebDriver?

To access a method of WebDriver, you need to create a driver object from WebDriver and press. dot key. This will list all the methods. Here, you can see that I have used driver.get, so it will show all the methods starts with get.

Method Name – I have explained earlier about Method. A method is a block of code that is use to perform certain actions. Method runs when it is call. Method name is the name of method we are going to use. 

Parameter Data Type – It is pass as an argument to the method to perform certain operation. Here, string data type is use as an argument. 

Return Type – Methods can return values like getTitle() : String as string is here the return type. In the above example, return type is void means do not return anything.

Browser Commands

1) Get – This command loads a new web page in the current browser window and open the webpage mentioned as argument. It accepts string as parameter and void means returns nothing. 

Syntax

get(String arg0) : void    

Command

Command - driver.get(URL) 

2) Get Title – This command retrieves the title of current web page. It does not accept any parameter and returns a string value. 

Syntax

getTitle() : String   

Command

driver.getTitle()

3) Get Current URL – This command retrieves the URL of current web page. It does not accept any parameter and returns a string value.

Syntax

getCurrentUrl() : String  

Command

driver.getCurrentUrl(); 

4) Get Page Source – This command returns the page source of current web page. . It does not accept any parameter and returns a string value.

Syntax

getPageSource() : String  

Command

driver.getPageSource();

5) Get Class – The command is use to retrieve the Class object that represents the runtime class of this object.

Syntax

getClass() 

Command

driver.getClass(); 

6) Close – This command is use to close the current web browser, which is controlled by WebDriver. This command does not require any parameter and void means return nothing.

Syntax

close(): void 

Command

driver.close()

7) Quit – This command close all the browsers opened by webDriver. This command does not require any parameter and void means return nothing.

Syntax

quit(): void 

Command

driver.quit(); 

Now, let us write a small program to show the use of methods of WebDriver.  

  1. Launch a new Firefox browser
  2. Open facebook.com
  3. Get Page Title name and print
  4. Get Page URL and print on Eclipse Console
  5. Get Page Source (HTML Source code) and Page Source length and print Page length
  6. Get Runtime class name of the object
  7. Close the Browser
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Browser_Commands {
    public static void main(String[] args) throws {
         System.setProperty("webdriver.gecko.driver",
                                              "C:\\Users\\Vibha\\Desktop\\SeleniumKT\\geckodriver-v0.27.0-win64\\geckodriver.exe");
 
             // Create a new instance of the Firefox driver 
             WebDriver driver = new FirefoxDriver();
             driver.manage().window().maximize();
 
            // Set implicit wait of 60 seconds
             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
 
            // Launch a new web page and load Facebook using Get Command
             driver.get("https://www.facebook.com/");
 
             // Get the Title of web page
             String PageTitle = driver.getTitle();
             System.out.println("Title of page is :"+ PageTitle);
 
             // Get URL of current web page
             String PageURL = driver.getCurrentUrl();
             System.out.println("URL of current page is :" + PageURL);
 
             // Get Source code of current web page
             String PageSource = driver.getPageSource();
             int PageSource_Length = PageSource.length();
             System.out.println("Length of page source of current page is :" + PageSource_Length);
 
              // Get Runtime class of this object
              Class<? extends WebDriver> className = driver.getClass();
              System.out.println("className :" + className);
 
              // Close the current web page
              driver.close();
        } 
}

Output
Title of page is :Facebook – log in or sign up
URL of current page is :https://www.facebook.com/
Length of page source of current page is :293624
className :class org.openqa.selenium.firefox.FirefoxDriver

Advertisement

4 thoughts on “WebDriver Browser Commands – get, getTitle, getCurrentUrl, getPageSource, getClass, close, quit in Selenium WebDriver

  1. I'm Selenium aspirant. I was looking for something to start from scratch and i'm able to follow your each blog. I have a question – findElement and findElements serve the same purpose or they are different?

    Like

  2. Thanks Neetu. Both findElement and findElements are used to locate web element on web page, the major difference is that findElement will return a single element whereas findElements will return multiple elements means a list. I'm planning to have a blog on this. Watch the space more details on this topic.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s