Synchronization commands in Selenium Webdriver

  • Thread.sleep(9000);

  • WebDriverWaitwait=newWebDriverWait(driver,9);
    wait.until(ExpectedConditions.elementToBeClickable(By.id("element-id")));
     

  • driver.manage().timeouts().implicitlyWait(9,TimeUnit.SECONDS);

  • driver.manage().timeouts().pageLoadTimeout(9,TimeUnit.SECONDS);

  • Wait<WebDriver>  wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .polingEvery(5, SECONDS)
    .ignoring(NoSuchElemntException.clas);

  • robot.delay(900);

  • selenium.waitForPageToLoad("9000");

  • selenium.setTimeout("90000");

Examples :-

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
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.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;

    @Test
    public void threadSleep() throws Exception {
        driver.get("http://www.seleniumlearn.com");
        Thread.sleep(9000);
        driver.findElement(By.linkText("Java")).click();
    }

@Test
    public void implicitlyWait() {
        driver.get("http://www.seleniumlearn.com");
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.findElement(By.linkText("Java")).click();
    }

@Test
    public void explicitwaits() {
        driver.get("http://www.seleniumlearn.com/selenium");
        WebDriverWait wait=new WebDriverWait(driver,9);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("edit-search-block-form--2")));

        driver.findElement(By.id("edit-search-block-form--2")).sendKeys("selenium");
    }

    @BeforeTest

    public void beforeTest() {
        driver=new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
        //driver = new ChromeDriver();
        driver.manage().window().maximize();

    }

}

Thread.sleep() method will tell the webdriver to be stable for a particular time which u will pass in the sleep method..eg, thread.sleep(9000).This will tell webdriver to wait for 9 seconds,whereas implicit wait will continue to execute the next line if element is present, or will wait until 9seconds to find the element. If not it will  through an error (element not found exception,waiting for nine seconds )

Comments