After successfully running our first test case on Firefox Browser now we are stepping towards grasping the essential Selenium Navigation Commands. Thus we are going to discuss about various Selenium Navigation Commands that we would be using in our day to day automation testing. The navigate interface exposes the ability to move backwards and forwards in the browser’s history.
Selenium Webdriver - Browser Navigation Command
To access the navigation's method, just type driver.navigate(). The IntelliSense feature of the eclipse will automatically display all the public methods of Navigate Interface shown in the below image.
Note: Only methods that are followed by Navigation keyword are belongs to navigate. Rest followed by Object keyword are the generic methods gets from Object Class in Java. You will find these methods for every object of java language.
Navigate To Command - How to Navigate to URL or How to open a webpage in Selenium Browser?
to(String arg0) : void - This method Loads a new web page in the current browser window. It accepts a String parameter and returns nothing.
Command - driver.navigate().to(appUrl);
It does exactly the same thing as the driver.get(appUrl) method. Where appUrl is the website address to load. It is best to use a fully qualified URL.
driver.navigate().to("https://www.DemoQA.com");
Forward Command - How to browser Forward in Selenium Browser?
forward() : void - This method does the same operation as clicking on the Forward Button of any browser. It neither accepts nor returns anything.
Command - driver.navigate().forward();
Takes you forward by one page on the browser's history.
driver.navigate().forward();
Back Command - How to browse backward in Selenium Browser?
back() : void - This method does the same operation as clicking on the Back Button of any browser. It neither accepts nor returns anything.
Command - driver.navigate().back();
Takes youback by one page on the browser's history.
driver.navigate().back();
Refresh Command - How to Refresh Selenium Browser?
refresh() : void - This method Refresh the current page. It neither accepts nor returns anything.
Command - driver.navigate().refresh();
Perform the same function as pressing F5 in the browser.
driver.navigate().refresh();
Practice Exercises for Selenium Navigation Commands
Practice Exercise
- Launch new Browser
- Open DemoQA.com website
- Click on Registration link using "driver.findElement(By.xpath(".//[@id='menu-item-374']/a")).click();"*
- Come back to Home page (Use 'Back' command)
- Again go back to Registration page (This time use 'Forward' command)
- Again come back to Home page (This time use 'To' command)
- Refresh the Browser (Use 'Refresh' command)
- Close the Browser
Solution
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands {
public static void main(String[] args) {
// Create a new instance of the FireFox driver
WebDriver driver = new FirefoxDriver();
// Open ToolsQA web site
String appUrl = "https://www.DemoQA.com";
driver.get(appUrl);
// Click on Registration link
driver.findElement(By.xpath(".//*[@id='menu-item-374']/a")).click();
// Go back to Home Page
driver.navigate().back();
// Go forward to Registration page
driver.navigate().forward();
// Go back to Home page
driver.navigate().to(appUrl);
// Refresh browser
driver.navigate().refresh();
// Close browser
driver.close();
}
}