Selenium Waits

public class Waits {


    /**
     * Implicit Wait
     * The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
     * In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.
     */
    @Test
    public void implicitWait(){
        // set driver system path
        System.setProperty("webdriver.gecko.driver", StartBrowsers.pathToDrivers+"geckodriver.exe");
        // initialise browser
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
    }

    /**
     * The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception.
     * The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
     */
    @Test
    public void explicitWait(){
        // set driver system path
      setProperty("webdriver.gecko.driver", StartBrowsers.pathToDrivers+"geckodriver.exe");
        // initialise browser
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( "someId")));
    }


}

Posted

in

,

by

Tags:

Comments

Leave a Reply