There are two types of waits in selenium
1) Static wait
2) Dynamic wait
Static Wait : In static wait the driver will wait for the static amount of time or the fixed amont of time.
Thread.sleep(2000) the best example of static wait.
In Thread.sleep, whatever is the time provided inside the sleep method the driver will wait for this amount of time. If 20 seconds is the time provided inside the sleep method the driver will wait for 20 seconds.
Dynamic wait : In dynamic wait the driver will not wait for the fixed amount of time. There the dynamic wait will wait for the dynamic amount of time. It mean it wait for the condition to fulfill if the condition gets completed in 2 seconds then the driver will only wait for 2 seconds or if the condition get completed in 30 seconds the driver will wait for 30 seconds.
In Selenium there are two types of Dynamic wait
1) Implicit wait
3) Explicit wait
Implicit wait : Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.
the synax of writing the implicit wait in selenium is
driver.manage().timeouts().implicitlyWait(time, unit)
Example driver.manage().timeouts().implicitlywait(30, TimeUnit.SECONDS);
Example driver.manage().timeouts().implicitlywait(30, TimeUnit.MILLISECONDS);
Explicit wait: The syntax for writing the explicit wait is
WebDriverWait wait= new WebDriverWait(driver, timeOutInSeconds)
wait.until(ExpectedConditions.visibilityOf(element));
Example
WebElement element =driver.findElement(By.id(“UserName”));
WebDriverWait wait= new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(element));
Methods in ExpectedConditions Class
- visibilityOf(element)
- alertIsPresent()
- elementToBeClickable(locator)
- elementToBeClickable(element)
- elementToBeSelected(locator)
- frameToBeAvailableAndSwitchToIt(locator)
etc…..
