Selenium-WebDriver: Browser Navigation Methods

To test any web application, It is important to know how users will navigate through it. Whenever we open URLs in the browser and take any action on the page, the control takes us to the respective page, So to get back to the original page sometimes we have to make the browser back and forth, and the page refresh manually.

When it comes to automation, the selenium WebDriver provides  Browser Navigation Methods which can be mainly used for navigating into the browser. Let’s see the methods listed below.

  • Navigate To Method
  • Forward Method
  • Back Method
  • Refresh Method

Navigation is actually a collection of methods. We need to mention navigate() along with the methods as shown in the below image. Navigate method is implemented under the navigation interface. This allows the driver method to hit the URL in a browser, move into history in the browser, and refresh a browser web page.

The navigate to method is used for opening a String https URL request in a new window and open a new web page in the current browser window. It works the same as get()method in the selenium browser method.
Syntax:
void navigate().to(String URL);
here, void represents return type(which means it returns nothing), navigate() and  to() represents method names which has defined under the  navigation interface. (String URL) represents string type URL input we have to pass in it.
Code Snippet:

driver.navigate().to("https://plgworks.com");

Forward Method:

Navigate to forward method is used to navigate to a page one step further based on the browser's history. In a browser with a newly opened page and no recent forward history at that time, we were unable to use the forward method.
Syntax:
void navigate().forward();
Here, void represents return type, navigate() represents method name, forward() represents method name defined under navigation interface.
Code Snippet:

driver.navigate().forward();

Back Method:

The back method is used for navigating to go one window history back according to browser history. Simply we can use it to go to the previous page we visited.
Syntax:
void navigate().back();
Here, void is return type, navigate() represents method name, back() represents method name defined under navigation interface.
Code Snippet:

driver.navigate().back();

Refresh Method:

Navigate refresh method is used to refresh a current open web page in a browser.
Syntax:
void navigate().refresh();
Here, void represents return type,  navigate() represents method name and refresh() represents method name defined under navigation interface.
Code Snippet:

driver.navigate().refresh();

Let's combine all methods and see one example in a code snippet.

public class Ex_Navigation_Methods {
	public static void main(String[] args) {
    
    //Set Property and giving path of browser path from device folder.
		System.setProperty("webdriver.chrome.driver","/Users/XX/Documents/Drivers/chromedriver");
        
        //driver object declaration
		WebDriver driver = new ChromeDriver();
        
		driver.get("https://plgworks.com");
        
//navigate().to() >> this method we can use for opening another window

        driver.navigate().to("https://plgworks.com/slackmin”);

/*navigate().back() ..>> method use for go back to the previous page or go back to one current history*/

		driver.navigate().back();

//go to one history forward on web page
		
        driver.navigate().forward());

//for refreshing web page

		driver.navigate().refresh();
	}
}

Visit this link for more information and to explore this topic. Stay tuned for the next blog.












Ganesh Chavan

Ganesh Chavan

QA Test Engineer at PLGWorks.com