|

Selenium WebDriver Commands/Methods – Browser methods Part 1

Welcome to the Selenium Automation Free training tutorial. We have seen the following stuff so far in the last tutorials:

  1. Created a successful test script.
  2. How to assert the title of the webpage, and what are the scenarios where we can use it?
  3. What are Web elements & locators?

As we mentioned, we will talk about the methods or commands of Selenium WebDriver in this tutorial. So, what we do is we will classify methods of WebDriver into 3 categories:

  1. Browser methods or commands
  2. Navigation methods or commands
  3. Web elements methods or commands

and every category will have code snippets to understand methods and their uses.

Selenium Webdriver Browser command/methods

All right. Let’s jump on the category Browser methods or commands. Rest 2 we will talk about in the upcoming tutorials:

keepersecurity.com
keepersecurity.com

1. Get command

Method: get(String arg0)

This method helps to load the web pages in the browser that we pass in a given parameter.

Return: void

Parameters: one parameter for URL.

GotPrint
GotPrint

Example: 

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

The above code snippet will open the qaonlinetraining website in the browser.

2. Get Title command

Method: getTitle()

This method returns the title of a webpage in a String object.

Return: String

Edureka - Month End Offer-Flat 30% OFF ON Live Course
Edureka - Month End Offer-Flat 30% OFF ON Live Course

Parameters: no parameter.

Example:

driver.getTitle();

3. Get Current URL Command

Method: getCurrentUrl()

This method returns the URL of the current page loaded on the current browser tab in String format.

Return: URL in string format.

Parameters: no parameter.

Example:

driver.getCurrentUrl();

4. Get Page Source Command

Method: getPageSource()

This method returns the page source of the current page loaded on the current browser tab in String format.

Return: page source in string format.

Parameters: no parameter.

Example:

driver.getPageSource();

5. Close Command

Method: close()

This method is used to close the current window or tab of an opened browser, but the WebDriver session will remain. Let’s say you opened more than tabs on the browser, and you execute the call to close method of WebDriver, then it will terminate only the current window. In the case of one tab, the close command will terminate the current tab and browser as well with the WebDriver session.

Return: void

Parameters: no parameter

Example:

driver.close();

6. Quit Command

Method: quit()

It works like the close command, but the only difference is it will terminate all tabs and browser as well with WebDriver session.

Return: void

Parameters: no parameter

Example:
driver.quit();

These all are browser methods. Let’s work on some codes and understand how to use these methods:

Selenium Webdriver script with Browser get method

1. In this script, we will open https://www.qaonlinetraining.com, get the title of the page, assert the title of the web page, and close it.

import org.junit.Assert;
import org.openqa.selenium.chrome.ChromeDriver;
public class AssertTitle {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //open https://www.qaonlinetraining.com
        driver.get("https://www.qaonlinetraining.com/");
        String expectedTitle = "QA Training in Virginia | Selenium Testing Course Online USA | Job placement |";
        //get the title of the page
        String actualTitle = driver.getTitle();
        //assert the actual title of the web page with expected title.
        Assert.assertEquals(actualTitle, expectedTitle);
        //close the webdriver
        driver.close();
    }
}
This script will help to check the login process or registration process or submitting form process of any type.

Selenium Webdriver script with Browser getCurrentUrl method

2. In this script, we will open https://www.qaonlinetraining.com, get the current URL of the page and assert the achieved URL with opened one and at last we will terminate the browser by calling the quit method.
import org.junit.Assert;
import org.openqa.selenium.chrome.ChromeDriver;
public class AssertURL {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        String expectedUrl = "https://www.qaonlinetraining.com";
        //open https://www.qaonlinetraining.com
        driver.get(expectedUrl);
        //get the current URL of the page
        String actualUrl = driver.getCurrentUrl();
        //assert the achieved URL with opened one
        Assert.assertEquals(actualUrl, expectedUrl);
        //terminate the browser by calling the quit method
        driver.quit();
    }
}

This script will help to check the redirection of URLs. If the current URL and given URL are different, it means the website link is redirecting to another source.

Also read How should explain selenium project to the interviewer?

Selenium Webdriver script with Browser getPageSource method

3. In this script, we will open https://www.qaonlinetraining.com, get the source code of the web page and check the length of the web page content, and last will close the browser.

import org.openqa.selenium.chrome.ChromeDriver;
public class CheckLengthSourceCode {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        String expectedUrl = "https://www.qaonlinetraining.com";
        // open https://www.qaonlinetraining.com
        driver.get(expectedUrl);
        // get the source code of the web page
        String sourceCode = driver.getPageSource();
        // check the length of the web page content
        System.out.println("Web page length is " + sourceCode.length());
        //last will close the browser.
        driver.quit();
    }
}

Instructor-led Training

Important note: we would like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)

Conclusion

All right. We hope you understood all the browser methods of Selenium WebDriver. In the next tutorial, we will talk about Navigation methods or commands.
Happy Learning until then!

Similar Posts