Selenium Navigation – Complete Guide! [2021 Update]

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 current page to last opened page with driver.navigate().next() command.

.navigate.refresh()

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

Webdriver Navigation Example

package navigation;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class NavigationTests {

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

    @BeforeAll
    public void setupTest() {
        driver = new ChromeDriver();
    }

    //.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(driver.getTitle(), "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more");
    }

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

        //Go to www.amazon.com
        driver.navigate().to(URL2);
        //Check title is correct
        Assertions.assertEquals(driver.getTitle(), "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more");

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

        //***Navigate Forward***
        driver.navigate().forward();
        //Check title is correct
        Assertions.assertEquals(driver.getTitle(), "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more");

        //***Refresh The Page***
        driver.navigate().refresh();
        Assertions.assertEquals(driver.getTitle(), "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more");
    }

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

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 manipulation and interrogation.

Thanks,
Onur

3 thoughts on “Selenium Navigation – Complete Guide! [2021 Update]”

  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.