Webdriver Script Java Example

Webdriver Script: Java Example

Table of Contents

Selenium automates web applications across multiple browsers and to test the web applications we need browsers. We have different interfaces and classes to work with multiple browsers and HTML components provided by Selenium API.

Selenium Webdriver Interface?

Selenium Webdriver is an interface that automates the browser. This Webdriver interface is used for testing web applications that represent a web browser. These interfaces will instantiate the drivers provided by browser-specific classes like ChromeDriver, FirefoxDriver, SafariDriver, AndroidDriver, InternetExplorerDriver, etc…..

The most important function of the Webdriver is to control the browser. However, Webdriver controls the browser itself which helps us to choose the HTML elements on the page and perform operations such as form filling, click, etc.

If you would like to run your tests in a Chrome browser we use ChromeDriver class. In the same way, if you would like to run your tests in the IE browser we use the IEDriver class and similarly the same thing with the Firefox browser, Safari browser, etc.

Selenium WebDriver Methods

To automate the applications Selenium Webdriver has a number of methods in order to interact with the browsers. Each of these methods has its own reason to use.

Selenium Webdriver has many methods like get(), close(), quit(), getTitle(), getWindowHandle(), getCurrentUrl(), getPageSource etc. Below are the commonly used methods in Selenium Webdriver.

Method

Description

close()

This command will close the current window of the web browser

quit()

This command is used to close all the windows of the web browser which are currently opened by Webdriver and terminates the Webdriver session

findElement()

This command will return the single element on the web page 

findElements()

This command will returns a list of elements on the web page

get()

This command is used to navigate to a particular website.

getCurrentUrl()

This command will give the URL which currently focused on the web page in the form of a string

getPageSource()

This command will give the entire page source i.e HTML code of the web page which currently focused in the form of a string

getTitle()

This command will return the title of the web page which is currently focused in the form of string

getWindowHandle()

This method is used to handle the current window information. With this method we can switch between the windows. WebDriver is the written type for this method

getWindowHandles()

This method will handle all the windows information which is opened by Webdriver

clear()

This method is used to clear the content of the textbox

navigate()

This method is used to navigate to the next and previous web pages

switchTo()

This method is used to switch between the windows, frames in the application

Classes implementing Webdriver

The commonly used implementation classes of Webdriver interface are FirefoxDriver, ChromeDriver, InternetExplorerDriver, EdgeDriver, SafariDriver, etc. This implementation is provided by browser-specific classes.

Class

Description

FirefoxDriver

It helps you to run your Selenium Scripts on Firefox browser

ChromeDriver

It helps you to run your Selenium Scripts on Chrome browser

InternetExplorerDriver

It helps you to run your Selenium Scripts on InternetExplorer browser

SafariDriver

It helps you to execute your Selenium Scripts on Safari browser

Get Commands()

Get commands are used for fetching information about the page or web element on the web page. Here are some of the frequently used get commands in Selenium.

1.get():  This command is used to navigate to a particular website. It opens a given URL in the browser. Its written type is void and takes the parameter as a String object.

Syntax: 

driver.get(“URL”);

Example:

driver.get(“https://www.yahoo.com”);

2. getTitle():  This command will return the title of the web page which is currently focused in the form of string. It needs no parameters.

Syntax: 

String variable  = driver.getTitle();

Example:

driver.get(“https://www.yahoo.com”);

String webTitle = driver.getTitle();

System.out.println(webTitle);

3. getPageSource():  This command will give the entire page source i.e HTML code of the web page which currently focused in the form of a string. It needs no parameters.

Syntax:

String stringName = driver.getPageSource();

Example:

driver.get(“https://www.yahoo.com”);
String pageSource = driver.getPageSource();
System.out.println(pageSource);

4. getCurrentUrl():  This command will give the URL which currently focused on the web page in the form of a string. It needs no parameters.

Syntax:

String stringName = driver.getCurrentUrl();

Example:
driver.get(“https://www.yahoo.com”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

5.getText(): It fetches the innerText of a WebElement.

Navigation Commands

1 navigate().to(): It automatically opens a new web page in the new browser window.\

Syntax:
driver.navigate().to(Url);

Example:
driver.get(“https://in.yahoo.com/”);

String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
driver.navigate().to(“https://login.yahoo.com/”);
currentURL = driver.getCurrentURL();
System.out.println(currentURL);

2. navigate().back(): It moves one page back in the browser history.

Syntax:

driver.navigate().back();

Example:

driver.get(“https://in.yahoo.com/”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
driver.navigate().to(“https://login.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
driver.navigate().back();
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

Or

driver.get(“https://in.yahoo.com/”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
driver.navigate().to(“https://login.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
driver.navigate().to(“https://in.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

3. navigate().forward(): It moves forward by one page in the browser history.

Syntax:

driver.navigate().forward();

Example:

driver.get(“https://in.yahoo.com/”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

driver.navigate().to(“https://login.yahoo.com/”);

currentURL = driver.getCurrentUrl();
System.out.println(currentURL);


driver.navigate().back();
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

driver.navigate().forward();
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

Or

driver.get(“https://in.yahoo.com/”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);


driver.navigate().to(“https://login.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

 

driver.navigate().to(“https://in.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

driver.navigate().to(“https://login.yahoo.com/”);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

4. navigate().refresh(): It refreshes the current web page.

Syntax:

driver.navigate().refresh()

Example:

driver.get(“https://in.yahoo.com/”);
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);


driver.navigate().refresh();
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);

Close and Quit

1 close(): This command will close the current window of the web browser.

Syntax:

driver.close();

Example:

driver.get(“https://in.yahoo.com/”);
driver.close();

2. quit(): This command is used to close all the windows of a web browser which are currently opened by Webdriver and terminates the Webdriver session.

Syntax:

driver.quit();

Switching Between Frames

The Frame is a web page that is embedded in another web page or an HTML document embedded inside an HTML document on a website.

How to create iframes?

Example:

<iframe name=”First” id=”1” src=”https://www.selenium.dev/” width=”500” height=”500”>

</iframe>

Different Ways of accessing frames

  • Using name or id

Switching to IFrame using id or name, sometimes it presents both the frame name and id.

Syntax:
driver.switchTo().frame(1);
driver.switchTo().frame(“xxxx”);

  • Using index

Locating the frame using index if available.

Syntax:
driver.switchTo().frame(0); // frame index starts with 0

  • Using WebElement

Locating the frame using locators.

Syntax:
driver.switchTo().frame(“Locating the frame using any locator”);

Other Operations Using Frame

 1.  Switching Back To the Parent Frame

If you want to switch back from frame 4 to frame 3 we use “switchTo.parentFrame” command.

Syntax:

driver.switchTo().parentFrame();

2. Switching To Any Other Frame

If you want to switch from frame 4 to frame 1 or the default frame, then use “switchTo.defaultContent” command.

Syntax:

driver.switchTo().defaultContent();

Switching between Pop-up windows

Using the SwitchTo method Webdriver supports named PopUps moving between them.   Whenever you perform an action to open a popup, you can get the alert that returns the currently open alert object. With this alert object, you can perform actions like accept, dismiss, read its contents or even can type into a prompt. This interface works on alerts, prompts and confirms.

Syntax:

Alert alert = driver.switchTo().alert();

Selenium WebDriver Example – Print Website Title

Let’s consider the example of using Selenium WebDriver to invoke the Firefox browser and print the title of a website.

package com.testing.selenium.firefox;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class PageTitleExample {

            public static void main(String[] args) {

                        //specify the location of GeckoDriver for Firefox browser 

                        System.setProperty(“webdriver.gecko.driver”, “geckodriver”);

                        WebDriver driver = new FirefoxDriver();

                        driver.get(“https://yahoo.com”);

                        String PageTitle = driver.getTitle();

                        System.out.println(“Page Title is:” + PageTitle);

                        driver.close();

}

}

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class