Difference Between Webdriver.Get() and Webdriver.Navigate()

Difference between webdriver.get() and webdriver.navigate()

Navigating

The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

driver.get("http://www.google.com");

WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits.

Navigation: History and Location

Earlier, we covered navigating to a page using the get command (driver.get("http://www.example.com")) As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. Because loading a page is such a fundamental requirement, the method to do this lives on the main WebDriver interface, but it’s simply a synonym to:

driver.navigate().to("http://www.example.com");

To reiterate: navigate().to() and get() do exactly the same thing. One's just a lot easier to type than the other!

The navigate interface also exposes the ability to move backwards and forwards in your browser’s history:

driver.navigate().forward();
driver.navigate().back();

(Emphasis added)

driver.get(url) vs driver.navigate().to(url);

The first thing we do when run the script is to open the browser and load the web page. We use commonly driver.get(“url”); to load the webpage. Every time we use this command, the page will be refreshed.

We can also use driver.navigate().to(“url’); to load the webpage. Both the commands work in the same way in terms of behavior. But the navigate().to() also have the other functions such as navigate().forward(), navigate().back() and navigate().refresh().

So the difference is driver.get() never stores history whereas driver.navigate().to() stores browser history so as to be used for other commands forward and back etc.

In single page applications while navigate().to() navigates to the page by changing URL like doing forward/backward, get() refreshes page.

More info here - Difference between webdriver.get() and webdriver.navigate()

get() and navigate() in WebDriver

The two methods are aliases of one another. There is no functional difference.

What is the difference between ChromeDriver and WebDriver in selenium?

ChromeDriver driver = new ChromeDriver();

If you use ChromeDriver driver = new ChromeDriver(); the ChromeDriver instance which will get created through that we will be only able to invoke and act on the methods implemented by ChromeDriver and supported by Chrome Browser only. To act with other browsers we have to specifically create individual objects as below :

  • FirefoxDriver driver = new FirefoxDriver();
  • InternetExplorerDriver driver = new InternetExplorerDriver();

WebDriver Interface

From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla, Chrome, Internet Explorer, Safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available browsers without any change.


WebDriver driver = new ChromeDriver();

Through WebDriver driver = new ChromeDriver(); we are creating an instance of the WebDriver interface and casting it to ChromeDriver class. All the browser drivers like:

  • FirefoxDriver
  • ChromeDriver
  • InternetExplorerDriver
  • PhantomJSDriver
  • SafariDriver etc

implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the Browser Drivers extends RemoteWebDriver). So if we use WebDriver driver, then we can use the already initialized driver (as common object variable) for all browsers we want to automate e.g. Mozilla, Chrome, InternetExplorer, Edge, Opera, Safari as follows:

WebDriver driver = new FirefoxDriver();
// or
WebDriver driver = new ChromeDriver();
// or
WebDriver driver = new InternetExplorerDriver();
// or
WebDriver driver = new EdgeDriver();
// or
WebDriver driver = new OperaDriver();
// or
WebDriver driver = new SafariDriver();

Trivia

As per current scenario, we have to instantiate the implementations of WebDriver Interface directly. As per the current practice we write our Automated Test Script against this interface so that in future we may swap in a more fully featured Browser when there is a requirement for one.



Related Topics



Leave a reply



Submit