C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Selenium WebDriver - Navigation CommandsWebDriver provides some basic Browser Navigation Commands that allows the browser to move backwards or forwards in the browser's history. Just like the browser methods provided by WebDriver, we can also access the navigation methods provided by WebDriver by typing driver.navigate() in the Eclipse panel. Note: The methods having 'Navigation' as keyword are declared as Navigation commands.Given are some of the most commonly used Browser Navigation commands for Selenium WebDriver. 1. Navigate To CommandMethod: to(String arg0) : void In WebDriver, this method loads a new web page in the existing browser window. It accepts String as parameter and returns void. The respective command to load/navigate a new web page can be written as: driver.navigate().to("www.TheDeveloperBlog.com"); Note: The get command (driver.get(URL);) which lies in the browser commands section does the same function as the navigate command(driver.navigate().to("www.TheDeveloperBlog.com"); 2. Forward CommandMethod: to(String arg0) : void In WebDriver, this method enables the web browser to click on the forward button in the existing browser window. It neither accepts anything nor returns anything. The respective command that takes you forward by one page on the browser's history can be written as: driver.navigate().forward(); 3. Back CommandMethod: back() : void In WebDriver, this method enables the web browser to click on the back button in the existing browser window. It neither accepts anything nor returns anything. The respective command that takes you back by one page on the browser's history can be written as: driver.navigate().back(); 4. Refresh CommandMethod: refresh() : void In WebDriver, this method refresh/reloads the current web page in the existing browser window. It neither accepts anything nor returns anything. The respective command that takes you back by one page on the browser's history can be written as: driver.navigate().refresh(); Let us consider a sample test script which will cover most of the Navigation Commands provided by WebDriver. In this sample test, we will automate the following test scenarios:
For our test purpose, we are using a dummy web page under the URL: https://www.testandquiz.com/selenium/testing.html (You can also use this dummy web page for your Selenium Test Practices)
Give your Class name as "Navigation_command" and click on "Finish" button. Step3. Let's get to the coding ground.
Here is the sample code to set system property for Gecko driver: // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" ) After that we have to initialize Gecko Driver using Desired Capabilities Class. Here is the sample code to initialize gecko driver using DesiredCapabilities class. // Initialize Gecko Driver using Desired Capabilities Class DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette",true); WebDriver driver= new FirefoxDriver(capabilities); Combining both of the above code blocks, we will get the code snippet to launch Firefox browser. // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" ); // Initialize Gecko Driver using Desired Capabilities Class DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette",true); WebDriver driver= new FirefoxDriver(capabilities);
Here is the sample code to navigate to the desired URL: //Navigate to the desired URL driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
The method for finding a unique identification element involves inspection of HTML codes.
It will launch a window containing all the specific codes involved in the development of the "This is a link" link. Select the name of the link text from inspector text box. The Java Syntax for uniquely identifying a web element through its Link Text is written as: driver.findElement(By.linkText ( Therefore, for locating the Link Text on the sample web page we will use the value of its Link Text: driver.findElement(By.linkText (<"This is a Link">)) Now, we need to write the code which will click on the Link Text. Here is the sample code to click on the Link Text. // Click on the Link Text using click() command driver.findElement(By.linkText("This is a Link")).click(); On click, the link will redirect the browser window to the official web page of javaTpoint website.
Here is the sample code to return to the home page after being directed to the javaTpoint website. // Go back to Home Page driver.navigate().back();
Here is the sample code to go forward again to the official web page of javaTpoint website. // Go forward to Registration page driver.navigate().forward();
Here is the sample code to go back to the home page. // Go back to Home page driver.navigate().to(appUrl);
// Refresh browser driver.navigate().refresh();
driver.close(); Combining all of the above code blocks together, we will get the required source code to execute our test script "Navigation_command". The final test script will appear something like this: (We have embedded comment in each section to explain the steps clearly) import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class Navigation_command { public static void main(String[] args) { // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" ); // Initialize Gecko Driver using Desired Capabilities Class DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette",true); WebDriver driver= new FirefoxDriver(capabilities); // Launch Website driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); //Click on the Link Text using click() command driver.findElement(By.linkText("This is a Link")).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("https://www.testandquiz.com/selenium/testing.html"); //Refresh browser driver.navigate().refresh(); //Closing browser driver.close(); } } To run the test script on Eclipse window, right click on the screen and click Run as → Java application Upon execution the test script will launch the Firefox browser and automate all the test scenarios.
Next TopicWebDriver - WebElement Commands
|