Exploring Navigate
As we know, WebDriver talks to individual browsers natively. This way it has better control, not just over the web page, but over the browser itself. Navigate is one such feature of WebDriver that allows the test script developer to work with the browser's back, forward, and refresh controls. As users of a web application, quite often, we use the browser's back and forward controls to navigate between the pages of a single application, or, sometimes, multiple applications. As a test-script developer, you may want to develop tests that observe the behavior of the application when browser navigation buttons are clicked, especially the back button. For example, if you use your navigation button in a banking application, the session should expire and the user should be logged out. So, using the WebDriver's navigation feature, you can emulate those actions.
The method that is used for this purpose is navigate(). The following is its API syntax:
WebDriver.Navigation navigate()
Obviously, there is no input parameter for this method, but the return type is the WebDriver.Navigation interface, which contains all of the browser navigation options that help you navigate through your browser's history.
Now let's see a code example and then analyze the code:
@Test
public void searchProduct() {
driver.navigate().to("http://demo-store.seleniumacademy.com/");
// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");
WebElement searchButton =
driver.findElement(By.className("search-button"));
searchButton.click();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
}
The preceding code opens the demo application's Homepage, and, at first, searches for Phone; then, after the search results are loaded. Now that we have a navigation history created in the browser, it uses WebDriver navigation to go back in the browser history, and then go forward and refresh the page.
Let's analyze the navigation methods used in the preceding code. The line of code that initially loads the demo application's Homepage uses the to() method of the Navigation class, as follows:
driver.navigate().to("http://demo-store.seleniumacademy.com/");
Here, the driver.navigate() method returns the WebDriver.Navigation interface on which the to() method is used to navigate to a web URL.
The API syntax is as follows:
void to(java.lang.String url)
The input parameter for this method is the url string that has to be loaded in the browser. This method will load the page in the browser by using the HTTP GET operation, and it will block everything else until the page is completely loaded. This method is the same as the driver.get(String url) method.
The WebDriver.Navigation interface also provides an overloaded method of this to() method to make it easy to pass the URL. The API syntax for it is as follows:
void to(java.net.URL url)
Next, in the code example, we did a search for Phone. Then, we tried to use Navigation's back() method to emulate our browser's back button, using the following line of code:
driver.navigate().back();
This will take the browser to the home page. The API syntax for this method is pretty straightforward; it's as follows:
void back()
This method doesn't take any input and doesn't return anything as well, but it takes the browser one level back in its history.
Then, the next method in the navigation is the forward() method, which is pretty much similar to the back() method, but it takes the browser one level in the opposite direction. In the preceding code example, the following is invoked:
driver.navigate().forward();
The API syntax for the method is as follows:
void forward()
This method doesn't take any input, and doesn't return anything either, but it takes the browser one level forward in its history.
The last line of code in the code example uses the refresh() method of WebDriver's navigation:
driver.navigate().refresh();
This method will reload the current URL to emulate the browser's refresh (F5 key) action. The API syntax is as follows:
void refresh()
As you can see, the syntax is very similar to the back() and forward() methods, and this method will reload the current URL. Hence, these are the various methods WebDriver provides developers to emulate some browser actions.