Synchronization in selenium webdriver are handled by using "Waits".
Two types of waits available in selenium webdriver are
- Implicit wait
- Explicit wait
Implicit wait:
Implicit waits are set to default waiting time for whole testcase. Means it applies throughout the program.
Syntax:
driver.manage().timeouts().implicitlywait(10, TimeUnit.SECONDS);
In the above syntax we have given time for "10" seconds means it waits for maximum 10 seconds for element to be present in webpage.We can also change waiting time as per requirement.like 10,15,20 seconds etc.
Example:
package synchronization;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Implicitwait {
@Test
public void implywait()
{
WebDriver driver=new FirefoxDriver();
//implicit waits are used here
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();
driver.findElement(By.name("userName")).sendKeys("user1");
driver.quit();
}
}
Explicit wait:
Explicit waits are used when we want to wait for particular element to be present .
Ex: A particular element in a webpage is taking time to present on a webpage then we go for explicit waits.
Syntax:
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visiblilityofElementsLocated(By.id("username")));
driver.findelement(By.id("username")).sendkeys("user1");
Example:
package synchronization;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class Explicitwait {
@Test
public void expicitexp()
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com/");
//Explicit Wait
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));
driver.findElement(By.name("userName")).sendKeys("user1");
driver.quit();
}
}
No comments:
Post a Comment