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:
- Browser Methods perform actions on a browser.
- WebElement Methods perform actions on WebElements.
- Wait Methods pause between execution statements.
- 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
Tutorial Chapters
- Browser Methods (Chapter 1)
- WebElement Methods (Chapter 2)
- You’re here → Navigation Methods (Chapter 3)
- Wait Methods (Chapter 4) – Coming Soon
- pageLoadTimeOut
- ImplicitWait
- ExplicitWait
- FluentWait
- 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()); }
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()); }
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()); }
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()); }
This time, the Console shows print statements after executing navigate().to(), navigate().refresh(), navigate().back(), and navigate().forward().
- navigate().to() displays “To Title: Test Automation Blog | TestProject”
- navigate().refresh() displays “Refresh Title: TestProject – Login”
- navigate().back() displays “Back Title: Test Automation Blog | TestProject”
- navigate().forward() displays “Forward Title: TestProject – Login”
The next article in this series of Selenium Method Categories will be Wait Methods. Stay tuned! 🤗