Navigation Methods
Let’s explore the category of Navigation Methods. It is a group of techniques for loading, refreshing, going backward, and moving forward in our browser’s history.
After writing navigate() followed by the dot operator, each Navigation method is made available. The driver can access our browser’s history by writing navigate(). By the article’s conclusion, you will learn how to navigate().to(), navigate().refresh(), navigate().back() and navigate().forward().
navigate().to()
The Navigation Methods Category has 2 navigate().to() methods. They are, however, overloaded methods. Either approach will result in the loading of a new web page in the active browser window. The get() function from the Browser’s Method category operates just like this one. In actuality, an HTTP GET operation is used to process the get() and to() methods. Let’s use navigate.to in the test script that follows to load the identical Blog page from Perficient.
@Test public void demoNavigationMethods() { driver.navigate().to("https://blog.perficient.io/"); System.out.println("To Title: " + driver.getTitle()); }
navigate().refresh()
All WebElements is reloaded when the current page is refreshed using the navigate().refresh() method. Sometimes a user will refresh a webpage so that the browser can provide the most recent version. The next test script hits the Login link, types in a username, and then forces a page refresh. After telling the browser to reload the website, the username is cleared as a result:
@Test public void demoNavigationMethods() { driver.navigate().to("https://blog.perficient.io/"); System.out.println("To Title: " + driver.getTitle()); driver.findElement(By.id("item-535")).click(); driver.findElement(By.id("username")).sendKeys("John.Doe@test.org"); driver.navigate().refresh(); System.out.println("Refresh Title: " + driver.getTitle()); }
navigate().back()
The navigate().back() method advances our browser’s history by one page. It mimics the same user actions while pressing the ALT key while simultaneously pressing the left arrow. This Test Script advances to the previous page, Perficient’s Blog:
@Test public void demoNavigationMethods() { driver.navigate().to("https://blog.perficient.io/"); System.out.println("To Title: " + driver.getTitle()); driver.findElement(By.id("item-3453")).click(); driver.findElement(By.id("username")).sendKeys("John.Doe@test.org"); driver.navigate().refresh(); System.out.println("Refresh Title: " + driver.getTitle()); driver.navigate().back(); System.out.println("Back Title: " + driver.getTitle()); }
navigate().forward()
The navigate().forward() method advances our browser’s history by one page. It imitates the identical user actions while pressing the ALT key while simultaneously pressing the right arrow.
@Test public void demoNavigationMethods() { driver.navigate().to("https://blog.perficient.io/"); System.out.println("To Title: " + driver.getTitle()); driver.findElement(By.id("item-6345")).click(); driver.findElement(By.id("username")).sendKeys("John.Doe@test.org"); 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 | Perficient”
- navigate().refresh() displays “Refresh Title: Perficient – Login”
- navigate().back() displays “Back Title: Test Automation Blog | Perficient”
- navigate().forward() displays “Forward Title: Perficient – Login”
Conclusion:
We now have the understanding of Selenium Navigation methods and how to use them.