Selenium Navigation – Complete Guide!

In this post, we will dive into what webdriver API does in terms of navigation. Selenium Navigation is the first part of Webdriver API and we will cover all navigation methods such as .get(<url>), .navigate.to, .navigate.back(), .navigate.forward(), etc…

Selenium Navigation Methods

Webdriver Navigation methods are shown below figure. In this post, I will go with method explanations then show their examples and usage.

navigation

.get (URL)

We can go to any URL with driver.get(“http://www.yahoo.com”) command.

.navigate.to(url)

We can go to any URL with driver.navigate().to(“http://www.yahoo.com”) command.

.navigate.back()

We can go back to previous page with driver.navigate().back() command.

.navigate.forward()

We can go forward from the current page to the last opened page with driver.navigate().next() command.

.navigate.refresh()

We can refresh the webpage with driver.navigate().refresh() command.

Webdriver Navigation Example

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class NavigationTests {

    private       WebDriver     driver;
    private       WebDriverWait wait;
    final private String        URL1 = "http://www.yahoo.com";
    final private String        URL2 = "http://www.amazon.com";

    @BeforeEach
    public void setupTest() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @AfterEach
    public void quitDriver() {
        driver.quit();
    }

    //.get Example
    @Test
    public void T01_getURLExample() {
        //Go to www.yahoo.com
        driver.get(URL1);

        //Check title is correct
        Assertions.assertEquals(driver.getTitle(), "Yahoo");
    }

    //.Navigate().to example
    @Test
    public void T02_navigateToExample() {
        //Go to www.amazon.com
        driver.navigate().to(URL2);

        //Check title is correct
        Assertions.assertEquals("Amazon.com. Spend less. Smile more.", driver.getTitle());
    }

    @Test
    //Back - Forward - Refresh Example
    public void T03_backForwardRefreshExample() {
        //Go to www.yahoo.com
        driver.navigate().to(URL1);
        wait.until(driver -> driver.getTitle().contentEquals("Yahoo"));
        //Check title is correct
        Assertions.assertEquals(driver.getTitle(), "Yahoo");

        //Go to www.amazon.com
        driver.navigate().to(URL2);
        wait.until(driver -> driver.getTitle().contentEquals("Amazon.com. Spend less. Smile more."));
        //Check title is correct
        Assertions.assertEquals("Amazon.com. Spend less. Smile more.", driver.getTitle());

        //***Navigate Back***
        driver.navigate().back();
        wait.until(driver -> driver.getTitle().contentEquals("Yahoo"));
        //Check title is correct
        Assertions.assertEquals(driver.getTitle(), "Yahoo");

        //***Navigate Forward***
        driver.navigate().forward();
        wait.until(driver -> driver.getTitle().contentEquals("Amazon.com. Spend less. Smile more."));
        //Check title is correct
        Assertions.assertEquals("Amazon.com. Spend less. Smile more.", driver.getTitle());

        //***Refresh The Page***
        driver.navigate().refresh();
        wait.until(driver -> driver.getTitle().contentEquals("Amazon.com. Spend less. Smile more."));
        Assertions.assertEquals("Amazon.com. Spend less. Smile more.", driver.getTitle());
    }
}

GitHub Project

https://github.com/swtestacademy/selenium-examples

Important Note: Navigation of webdriver halts until the HTML of the page is fully loaded but javascript is not included in this context. Thus, in some cases synchronization is required for error-prone test automation.

Thanks,
Onur Baskirt

5 thoughts on “Selenium Navigation – Complete Guide!”

  1. In this tutorial, you forgot to mention that you use JUnit version 4. As I read you’re this series of selenium tutorial from beginning one after another I got confused because as you mention the link of previous two tutorials here it made a direct point that reader should import Junit 5 version as you mention in your first article.

    Reply
    • Yes, you are right. I need to update all the articles. It will take time but I will do that. You can use JUnit 5 (Jupiter) dependencies or you can use TestNG. I like TestNG more for selenium usage. I use JUnit 5 for API tests along with WebTestClient and RestAssured. For virtualization, I am using Wiremock and Spring Cloud Contract.

      Reply

Leave a Comment

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