logo logo

Selenium Navigation Methods (Chapter 3)

main post image

In this 3rd Selenium article, let’s dive into the Navigation Methods category. It’s a collection of methods that loads a web page, refreshes a web page, moves backward, and moves forward in our browser’s history. There’s a total of 5 Selenium categories and the other 4 categories are:

  1. Browser Methods perform actions on a browser.
  2. WebElement Methods perform actions on WebElements.
  3. Wait Methods pause between execution statements.
  4. Switch Methods switch to alerts, windows, and frames.

Each Navigation method becomes available after writing navigate() then the dot operator. Writing navigate() allows the driver to access our browser’s history. By the end of this article, you will read about navigate().to(), navigate().refresh(), navigate().back(), and navigate().forward(). The code is located on GitHub, here: https://github.com/RexJonesII/TestProject-TestNG

Selenium Navigation Methods

Tutorial Chapters

  1. Browser Methods (Chapter 1)
  2. WebElement Methods (Chapter 2)
  3. You’re here → Navigation Methods (Chapter 3)
  4. Wait Methods (Chapter 4) – Coming Soon
    • pageLoadTimeOut
    • ImplicitWait
    • ExplicitWait
    • FluentWait
  5. Switch Methods (Chapter 5) – Coming Soon
    • Switch Methods for Frames
    • Switch Methods for Alerts
    • Switch Methods for Windows

navigate().to()

There are 2 navigate().to() methods in the Navigation Methods Category. However, they are overloaded methods. We can use either method and it will load a new web page in the current browser window. This method operates exactly like the get() method from Browser’s Method category. In fact, get() and to() are processed using an HTTP GET operation. In the following Test Script, let’s load the same Blog page from TestProject using navigate.to().

@Test
public void demoNavigationMethods() {
  driver.navigate().to("https://blog.testproject.io/");
  System.out.println("To Title: " + driver.getTitle());
}

TestProject Test Automation Blog

navigate().refresh()

The navigate().refresh() method refreshes the current page thereby reloading all WebElements. Sometimes a user refreshes a web page so the browser can send the latest version of the web page. The following Test Script clicks the Login link, enters a username then refreshes the web page. As a result, the username is cleared after instructing the browser to refresh the web page:

@Test
public void demoNavigationMethods() {
  driver.navigate().to("https://blog.testproject.io/");
  System.out.println("To Title: " + driver.getTitle());

  driver.findElement(By.id("menu-item-7501")).click();
  driver.findElement(By.id("username")).sendKeys("[email protected]");
  driver.navigate().refresh();
  System.out.println("Refresh Title: " + driver.getTitle());
}

TestProject Sign In TestProject Sign In

navigate().back()

The navigate().back() method moves back a single page in our browser’s history. It simulates the same user actions when clicking the left arrow button or ALT key + left arrow key. This Test Script moves back to TestProject’s Blog page which is the previous page:

@Test
public void demoNavigationMethods() {
  driver.navigate().to("https://blog.testproject.io/");
  System.out.println("To Title: " + driver.getTitle());

  driver.findElement(By.id("menu-item-7501")).click();
  driver.findElement(By.id("username")).sendKeys("[email protected]");
  driver.navigate().refresh();
  System.out.println("Refresh Title: " + driver.getTitle());

  driver.navigate().back();
  System.out.println("Back Title: " + driver.getTitle());
}

Navigate Back Selenium Method

The Console shows print statements after executing navigate().to(), navigate().refresh(), and navigate().back(). We see how the first and last title displays “Test Automation Blog | TestProject”.

navigate().forward()

The navigate().forward() method moves forward one page in our browser’s history. It mimics the same user actions when clicking the right arrow button or ALT key + right arrow key. The following Test Script moves forward to TestProject’s Sign In page after navigating from the Blog page:

@Test
public void demoNavigationMethods() {
  driver.navigate().to("https://blog.testproject.io/");
  System.out.println("To Title: " + driver.getTitle());

  driver.findElement(By.id("menu-item-7501")).click();
  driver.findElement(By.id("username")).sendKeys("[email protected]");
  driver.navigate().refresh();
  System.out.println("Refresh Title: " + driver.getTitle());

  driver.navigate().back();
  System.out.println("Back Title: " + driver.getTitle());

  driver.navigate().forward();
  System.out.println("Forward Title: " + driver.getTitle());
}

Navigate Forward Selenium Method

This time, the Console shows print statements after executing navigate().to(), navigate().refresh(), navigate().back(), and navigate().forward().

The next article in this series of Selenium Method Categories will be Wait Methods. Stay tuned! 🤗

Rex Jones II

About the author

Rex Jones II

Rex Jones II has a passion for sharing knowledge about testing software. His background is development but enjoys testing applications.

Rex is an author, trainer, consultant, and former Board of Director for User Group: Dallas / Fort Worth Mercury User Group (DFWMUG) and member of User Group: Dallas / Fort Worth Quality Assurance Association (DFWQAA). In addition, he is a Certified Software Tester Engineer (CSTE) and has a Test Management Approach (TMap) certification.

Recently, Rex created a social network that demonstrate automation videos. In addition to the social network, he has written 6 Programming / Automation books covering VBScript the programming language for QTP/UFT, Java, Selenium WebDriver, and TestNG.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

Selenium for Teams

Sharing and distributing Selenium tests has never been so easy! With TestProject's FREE Selenium based platform, you can finally create awesome tests with the freedom to collaborate with your team effortlessly.
Sign Up Now right arrow
FacebookLinkedInTwitterEmail