Home >>Selenium Tutorial >Selenium WebDriver - Navigation Commands
WebDriver provides several basic browser control commands enabling the user to move backward or forward throughout the history of the browser.
Much as WebDriver's browser methods, we can also use WebDriver's navigation methods by typing driver.navigate) (in the Eclipse section.
Provided are some of Selenium WebDriver's most widely used User Navigation Commands.
Method:
to(String arg0) : void
This method loads a new web page into the current browser window inside WebDriver. String is acknowledged as a parameter, and returns void.
The corresponding load / navigate instruction for a new web page may be written as:
driver.navigate().to("www.phptpoint.com");
Method:
to(String arg0) : void
This method in WebDriver allows the web browser to click on the forward button in the current browser window. It embraces nothing and it wants none.
The respective instruction that directs you through one page in the background of the app can be written as:
driver.navigate().forward();
Method:
back() : void
This approach in WebDriver helps the web browser to press on the back button in the existing browser window. It embraces nothing and it wants none.
You may compose the respective command which takes you back to browser history by one page as:
driver.navigate().back();
Method:
refresh() : void
This process refreshes / reloads the latest web page in the same user window inside WebDriver. It embraces nothing and it wants none.
You may compose the respective command which takes you back to browser history by one page as:
driver.navigate().refresh();
Let us find a sample test script that will cover much of WebDriver 's Navigation Commands.
In this sample test, we will automate the following test scenarios:
We use a dummy web site under the URL for our test purpose:
https://www.testandquiz.com/selenium/testing.html (You can also use this dummy web page for your Selenium Test Practices)
Step 1. Launch Eclipse IDE and open the current "Demo Test" test suite which we built in the WebDriver tutorial section of WebDriver Installation.
Step 2. Right click on the "src" folder and create a new Class File from New > Class. Give your Class name as "Navigation_command" and click on "Finish" button.
Step 3. Let's get to the coding ground.
Here's the sample code for Gecko driver setting system property:
// System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" )
We have to configure Gecko Driver after that using the Desired Capabilities class.
Here is the sample application for initializing gecko driver using class DesiredCapabilities.
// 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);
Below is the sample code for navigating to the URL you want to use:
//Navigate to the desired URL driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
The method for finding a unique element of identification involves inspecting the HTML codes.
It will open a window that contains all the different codes that are involved in the "This is a link" link development. From the inspector text box select the name of the connection text.
The Java Syntax is written as: for the unique identification of a web element via its Link Text
driver.findElement(By.linkText (<linktext>)
For this purpose we would use the value of the Link Text to find the Link Text on the sample web page:
driver.findElement(By.linkText (<"This is a Link">))
Now we have to compose the code that will press on the Text Link.
Here is the sample code which you need to click on the text link.
// Click on the Link Text using click() command driver.findElement(By.linkText("This is a Link")).click();
Clicking on this link will move the browser window to the phptpoint website's official web page.
Here is the sample code to go back to home page after being led to the website of phptpoint.
// Go back to Home Page driver.navigate().back();
Here's the sample application to go to phptpoint's official web site again.
// Go forward to Registration page driver.navigate().forward();
Below is the sample application for restoring to homepage.
// Go back to Home page driver.navigate().to(appUrl);
// Refresh browser driver.navigate().refresh();
driver.close();
Combining all the above code blocks together, we can get the source code needed to execute our "Navigation_command" test script
Anything similar will appear in the final test script:
(We inserted comments in-section to explicitly explain the steps)
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(); } }