Navigation and browser commands in selenium web driver

Navigation and browser commands

We have successfully completed Web driver, Web element, browser commands and commands to find the element. In this tutorial, we are going to cover the Navigation and browser commands in selenium web driver provided by selenium web driver.

Navigation and browser commands are used to perform automation with selenium web driver. As the name indicates Navigation commands are used to navigate the URL and the Browser commands are used to perform an operation with browsers.

Navigation and browser commands

Below is the list of Navigation commands in selenium web driver.

To find the list of all methods you just need to write code in your eclipse driver.navigate(). it will display all the methods of the interface.

Navigation Commands in selenium web driver

To Navigate:

to(String args0): void

This method loads the Application URL in the current windows of the browser. It takes a String parameter as a URL(complete URL with HTTP protocol) for example we need to load a URL in the current window.

driver.navigate().to("http://codebun.com/");

Or

String URL = "http://codebun.com/";
driver.navigate().to(URL);

To Back:

back():void

 This method performs the back operation at the current browser window It takes a step back. Like you press back button in the browser.

Syntax :

driver.navigate().back();

Example:

driver.navigate().to("http://codebun.com/category/java/");
driver.navigate().back();

To Forward:

 Forward():void

This method performs Forward operation at the current browser window It takes a step forward. Like you press forward button in the browser.

Syntax:

driver.navigate().forward();
driver.navigate().to("http://codebun.com/category/java/");
driver.navigate().back();
driver.navigate().forward();

To Refresh:

 refresh():void

This method refreshes the current URL of the current window of the browser.

Syntax:

driver.navigate().refresh();

Example:

driver.navigate().to("http://codebun.com/category/java/");
    driver.navigate().refresh();

Navigation Commands in selenium web driver

Perform following task for practice navigation command in selenium web driver.

Open Browser.

Navigate to App URL of the home page.

Click at any link.

Come to again on the home page by back().

Come to link URL by forward().

Refresh current URL.

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {
  public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe");
    // Open Browser
    WebDriver driver = new ChromeDriver();
    // Load URL in the current browser
  
    driver.navigate().to("http://codebun.com/category/java/");		
    driver.navigate().back();
    driver.navigate().forward();
    driver.navigate().refresh();
  

  }
}

Browser commands in selenium web driver

Browser commands in selenium web driver are the browser operations like open browser, close browser.

Webdriver interface provides methods to perform that commands. If you don’t know about Webdriver Interface read about Webdriver interface link.

So web driver interface provides lots of methods to perform the operation if you want to see all the method just create an object on any browser for example chrome browser

Webdriver driver = new ChromeDriver();

Now enter driver and a dot(.) you can see all the methods.

Browser commands in selenium

Followings are some important methods which are commonly used.

Get Method:

get(String args0):void

This method will open reload the URL in the current browser. It will take String as a parameter and return nothing.

Example:

driver.get("codebun.com");

This statement will load the URL “codebun.com” on the browser.

Get Tittle:

getTitle():string

It will return the title of the current page as a string.

String tittle = driver.getTitle();

It will return a String(the title of the current page).

Get Current URL

getCurrentUrl(): String – This method returns the string as Current URL which is opened in the browser and returns a String value.

Example:

String currentURL = driver.getCurrentUrl();

Get Page SourceCode

getPageSourceCode():String 

This method will return source code of the current page and return a string value.

Example:

String pageSourceCode = driver.getPageSource();

Get Window Handle

getWindowHandle():String

This method will return a string of alphanumeric window handle from the focused browser.

Example:

String windows = driver.getWindowHandle();

Get Window Handles

getWindowHandles():Set<String>

This method will return a set of windows handle from the focused browser.

Example:

Set<String> s1 = driver.getWindowHandles();

Get Class

getClass():Class<? extends WebDriver>

This method will return the class of the current object. For example, If you are using object of chrome driver(driver) then it will return “class org.openqa.selenium.chrome.ChromeDriver”

Example:

Class<? extends WebDriver> classOfTheObject = driver.getClass();

All the browser Commands in selenium web Driver with the example

import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {
  public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe");
    // Open Browser
    WebDriver driver = new ChromeDriver();

    // Load URL in the current browser
    driver.get("http://www.seleniumeasy.com/test/");

    // Get Tittle of the current page.
    String tittle = driver.getTitle();
    System.out.println("Page tittle: "+tittle);

    // Get current URL....
    String currentURL = driver.getCurrentUrl();
    System.out.println("Current URL :"+currentURL);

    //Get Source code of the current page
    String pageSourceCode = driver.getPageSource();
    //System.out.println(pageSourceCode);
         
    //get focused window
    String window = driver.getWindowHandle();
    System.out.println("Window : "+window);

    //get set of focused windows
    Set<String> windows = driver.getWindowHandles();
    System.out.println("Set of windows : "+windows);

    //get class of the current object...
    Class<? extends WebDriver> classOfTheObject = driver.getClass();
    System.out.println("class of the current object :"+classOfTheObject);

  }
}

Output:

Browser commands (open browser and close browser).

Add a Comment

Your email address will not be published. Required fields are marked *