Thursday 4 February 2016

Difference between get() and navigate() in selenium webdriver

Difference between get() and navigate() in selenium web driver:
=================================================

driver.get("URL"):
===================

get() is a method to enter a url
GET will wait till the whole page gets loaded.
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:

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

Webdriver will wait until the page has fully loaded before returning the control to test or script.


driver.navigate().to("URL"):
===============================
NAVIGATE will just redirect to our required page and will not wait.
It will guide us through the history like refresh, back, forward


syntax:
--------
driver.navigate().to("http://www.google.com");

driver.navigate().forward();

driver.navigate().back();


driver.navigate().refresh();



Example on get() and navigate():
------------------------------------------

package blog;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class RightClickDemo {

WebDriver webDriver;
Actions actions;
@BeforeSuite
public void launchingIEBrowser() {
//System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
  // webDriver = new InternetExplorerDriver();
webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}
@Test
public void testScript() throws InterruptedException {
//using get() method
webDriver.get("https://google.com");
//using navigate() mechanism
webDriver.navigate().to("https://google.com");
webDriver.navigate().refresh();
webDriver.navigate().back();
webDriver.navigate().forward();
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
WebElement element =webDriver.findElement(By.linkText("Selenium By Ramesh Anapati"));
  actions = new Actions(webDriver);
  //going to open in new window
 // actions.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
  
  //going to open in same window but in new tab
  actions.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
  
  Thread.sleep(10000);

}
@AfterSuite
public void closeBrowser() {
webDriver.quit();

}

}



















No comments:

Post a Comment