Browser and Navigation commands in selenium web driver

Browser and navigation commands in selenium web driver is the most common and useful command at the initial stage of automation. In this selenium web driver tutorial, I am going to cover all the important browser and navigation commands in selenium web driver.

Browser commands in selenium web driver

In simple words, you can say perform browser operation like open browser, close browser. Webdriver interface provides methods to perform that commands. If you don’t know about Webdriver Interface read about the Webdriver interface link. Webdriver interface provide lots of methods to perform 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 the driver and a dot(.) you can see all the methods.

The followings are some important browser commands in selenium web driver.

How to load URL in selenium web driver

 get(String args0): void

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

Example:

driver.get("codebun.com");

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

How to Get Title of a page in selenium web driver

getTitle():string

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

String tittle = driver.getTitle();

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

How to Get Current URL in selenium web driver

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();

How to Get Page Source Code in selenium web driver 

getPageSourceCode():String

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

Example:

String pageSourceCode = driver.getPageSource();

How to handle windows in selenium web driver

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 the object of chrome driver(driver) then it will return “class org.openqa.selenium.chrome.ChromeDriver”

Example:

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

Browser Commands in selenium web Driver with real-time 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);

 }
}

Navigation Commands in selenium web driver

Navigation commands are used to perform browser navigation operations like back, forward, refresh, etc. 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 navigation interface.

How To Navigate URL in selenium web driver

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);

How to perform back operation is selenium web driver

back():void

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

Syntax :

driver.navigate().back();

Example:

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

driver.navigate().back();

How to perform Forward operation in selenium web driver

 Forward():void

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

Syntax:

driver.navigate().forward();

Example:

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

driver.navigate().back();

driver.navigate().forward();

How to Refresh the current page in selenium web driver

 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();

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

  • Open Browser.
  • Navigate to App URL of home page.
  • Click at any link.
  • Come to again on 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();
 
 }
}

 

Add a Comment

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

5 − 1 =