Understanding Selenium WebDriver Navigation Commands? – User friendly Tech help
After successfully running our first test case on Firefox and Chrome. We are stepping towards grasping the essential navigation commands in Selenium. Thus we are going to discuss about multifarious browser commands that we would be using in our day to day automation testing.
n
Requirement :-
nHow to navigate in Browser using Selenium Webdriver?
n
nSolution :-
nWe are focusing on Get and Navigate Commands.
n
Browser Navigation Commands |
n
n
nTo Command:-
nThis command is use to navigate to specific page or URL
ndriver.navigate().to(“http://www.uftHelp.com”);
n
nForward Command:-
nThis command is similar to browser forward button, Takes you forward by one page on the browser’s history.
ndriver.navigate().forward();
n
nBack Command:-
nUsed to go back to previous page like browser’s back button.
ndriver.navigate().back();
n
Refresh Command:-
nRefresh the current page.
n
driver.navigate().refresh();
n
n
Note:- Same can be achieved using "sendkeys"
n
n
driver.findElement(By.id(locator)).sendKeys(Keys.F5);
n
n
Get Command:-
nOpens a new browser window and navigate to given URL.
ndriver.get(“http://www.uftHelp.com”);
n
nGet v/s Navigate :-
n“navigate().to()” and “get()” do exactly the same thing. Only thing is that incase of “get” selenium would wait for the page to fully load before executing the next line of code.
n
nAlso “navigate” interface further exposes the ability to move backwards and forwards in your browser’s history.
n
nLet us test the above commands by completing the following Test Scenario:-
n
- n
- Launch Home page using Get Method
- Open Contact Us page
- Navigate Back to Home Page
- Navigate Forward to Contact Us Page
- Refresh the Contact Us Page
- Navigate To Selenium Page
- Close the Browser
n
n
n
n
n
n
n
n
nCode:-n
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
n
public class Navigation {
public static void main(String[] args) {
//Creating WebDriver Object
System.out.println("Launch Home page using Get Method");
System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Opens the given URL using Get Method
driver.get("http://www.uftHelp.com");
//Click on Contact Us Link
System.out.println("Open Contact Us page");
driver.findElement(By.id("contact-Us")).click();
//Using Back Command, go back to "Home" page
System.out.println("Navigate Back to Home Page"); driver.navigate().back();
// Using Forward Command, go to "Contact Us" link
System.out.println("Navigate Forward to Contact Us Page "); driver.navigate().forward();
//Refresh the page
System.out.println("Refresh the Contact Us Page "); driver.navigate().refresh();
//Using To command, open the Selenium Page
System.out.println("Navigate To Selenium Page");
driver.navigate().to("http://www.ufthelp.com/p/selenium.html");
System.out.println("Close the Browser");
//Closing the Browser driver.close(); }}