Quality Assurance

Selenium Navigation Methods

A technology developer working with code

Navigation Methods

Let’s explore the category of Navigation Methods. It is a group of techniques for loading, refreshing, going backwards, 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().

Picture1

 

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 are 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.testproject.io/");

System.out.println("To Title: " + driver.getTitle());

driver.findElement(By.id("menu-item-7501")).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("menu-item-7501")).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. After leaving the Blog page, the following Test Script advances to TestProject’s Sign In page:

@Test

public void demoNavigationMethods() {

driver.navigate().to("https://blog.perficient.io/");

System.out.println("To Title: " + driver.getTitle());

driver.findElement(By.id("menu-item-7501")).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().

 

Conclusion:

We now have the understanding of Selenium Navigation methods and how to use them.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mangesh Sonwane

Mangesh Sonwane is an Associate Technical Consultant at Perficient in Nagpur GDC. He has an experience of 0.5+ years in Drupal Technology as a Tester. Apart from this passionate about learning new changes and expanding his knowledge in Drupal and Automation. He is very committed to his work and is always ready to face any challenging projects.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram