Table of Contents
Enroll in Selenium Training

After successfully running our first test case on Firefox Browser now we are stepping towards grasping the essential Selenium Navigation Commands. Thus we are going to discuss about various Selenium Navigation Commands that we would be using in our day to day automation testing. The navigate interface exposes the ability to move backwards and forwards in the browser’s history.

Selenium Webdriver - Browser Navigation Command

To access the navigation's method, just type driver.navigate(). The IntelliSense feature of the eclipse will automatically display all the public methods of Navigate Interface shown in the below image.

Selenium Navigation Command - Selenium Webdriver Browser Navigation Command

Note: Only methods that are followed by Navigation keyword are belongs to navigate. Rest followed by Object keyword are the generic methods gets from Object Class in Java. You will find these methods for every object of java language.

Navigate To Command - How to Navigate to URL or How to open a webpage in Selenium Browser?

to(String arg0) : void - This method Loads a new web page in the current browser window. It accepts a String parameter and returns nothing.

Command - driver.navigate().to(appUrl);

It does exactly the same thing as the driver.get(appUrl) method. Where appUrl is the website address to load. It is best to use a fully qualified URL.

driver.navigate().to("https://www.DemoQA.com");

Forward Command - How to browser Forward in Selenium Browser?

forward() : void - This method does the same operation as clicking on the Forward Button of any browser. It neither accepts nor returns anything.

Command - driver.navigate().forward();

Takes you forward by one page on the browser's history.

driver.navigate().forward();

Back Command - How to browse backward in Selenium Browser?

back() : void - This method does the same operation as clicking on the Back Button of any browser. It neither accepts nor returns anything.

Command - driver.navigate().back();

Takes youback by one page on the browser's history.

driver.navigate().back();

Refresh Command - How to Refresh Selenium Browser?

refresh() : void - This method Refresh the current page. It neither accepts nor returns anything.

Command - driver.navigate().refresh();

Perform the same function as pressing F5 in the browser.

driver.navigate().refresh();

Practice Exercises for Selenium Navigation Commands

Practice Exercise

  1. Launch new Browser
  2. Open DemoQA.com website
  3. Click on Registration link using "driver.findElement(By.xpath(".//[@id='menu-item-374']/a")).click();"*
  4. Come back to Home page (Use 'Back' command)
  5. Again go back to Registration page (This time use 'Forward' command)
  6. Again come back to Home page (This time use 'To' command)
  7. Refresh the Browser (Use 'Refresh' command)
  8. Close the Browser

Solution

package automationFramework;
	import org.openqa.selenium.By;
	import org.openqa.selenium.WebDriver;
	import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands {
	public static void main(String[] args) {
		// Create a new instance of the FireFox driver
		WebDriver driver = new FirefoxDriver();

		// Open ToolsQA web site
		String appUrl = "https://www.DemoQA.com";
		driver.get(appUrl);

		// Click on Registration link
		driver.findElement(By.xpath(".//*[@id='menu-item-374']/a")).click();

		// Go back to Home Page
		driver.navigate().back();

		// Go forward to Registration page
		driver.navigate().forward();

		// Go back to Home page
		driver.navigate().to(appUrl);

		// Refresh browser
		driver.navigate().refresh();

		// Close browser
		driver.close();
	}
}
Browser Commands in Selenium WebDriver
Browser Commands in Selenium WebDriver
Previous Article
WebElement Commands
WebElement Commands
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M FULL STACK TEST AUTOMATION ENGINEER. Have passed 12 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently I am working with KNAB bank as SDET. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns. https://www.linkedin.com/in/lakshay-sharma-a4216312/
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles