Implicit and Explicit Wait in Selenium Thread.sleep Fluent Wait:
Implicit Wait
Explicit Wait
sleep
WebDriverWait
PageLoadTimeout Command
SetScriptTimeout Command
FluentWait
1. Implicit Wait:
- Implicit Wait means informing selenium web driver to wait for specific amount of time.
- If the web element is not visible after waiting for that specific point then throw “NoSuchElementException”.
Syntax Of Implicit Wait:
1 | driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); |
Example of Implicitlywait:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class ImplicitlyWait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Wait 30 Seconds until Page loading driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } } |
2. Explicit Wait:
- In Explicit wait along with wait time we also provide the wait condition for particular WebElement.
- It will wait till the condition or the maximum wait time provided before throwing the Exception “ElementNotVisibleException”.
Thread.sleep
WebDriverWait
PageLoadTimeout Command
SetScriptTimeout Command
1. Thread.sleep Command
- The sleep code always has to wait for the seconds mentioned within the bracket, even if the work page is ready after 5 seconds. So this test can slow down.
Example:
1 2 | // Wait 5 Seconds Thread.sleep(5000); |
Example of Thread.Sleep:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class Thread_Sleep_Wait { public static void main(String[] args) { System.setProperty(webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.stqatools.com"); // Wait 5 Seconds Compulsory Thread.sleep(5000); } } |
2. WebDriverWait
- Before proceeding any operation, wait for a certain position before proceeding with the code.
- Wait for the WebDriver to check whether the element exists or to operate on it, visible or enabled or disabled or clickable.
1 2 3 4 5 | // Create a Object wait of WebdriverWait class WebDriverWait wait = new WebDriverWait(driver,30); // Using ExpectedConditions wait until element visibility wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input"))); |
ExpectedConditions class can be useful in a lot of cases and provides some set of predefined condition to wait for the element. Here are some of them below:
findElement using locator and store into WebElement element to use in ExpectedConditions class:
1 | WebElement element = driver.findElement(By.id("id")); |
alertIsPresent :
1 2 3 | // Wait until alert present on page wait.until(ExpectedConditions.alertIsPresent()); |
elementToBeClickable:
1 2 3 | // Wait until element to be clickable on page wait.until(ExpectedConditions.elementToBeClickable(element)); |
elementToBeSelected:
1 2 3 | // Wait until element to be selectable on page wait.until(ExpectedConditions.elementToBeSelected(element)); |
frameToBeAvailableAndSwitchToIt:
1 2 3 | // Wait until frame is available and frame selected. wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element)); |
invisibilityOf:
1 2 3 | // Wait and check element is invisibility wait.until(ExpectedConditions.invisibilityOf(element)); |
presenceOfAllElementsLocatedBy:
1 2 3 | // Wait until present element located by. wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By) element)); |
textToBePresentInElement:
1 2 3 | // Wait until text present on particular an element wait.until(ExpectedConditions.textToBePresentInElement(element, "Text")); |
textToBePresentInElementValue:
1 2 3 | // Wait until element value present for a particular element. wait.until(ExpectedConditions.textToBePresentInElementValue(element, "Value")); |
visibilityOf:
1 2 3 | // check element visibility wait.until(ExpectedConditions.visibilityOf(element)); |
titleContains:
1 2 3 | // Wait and check title contains or not. wait.until(ExpectedConditions.titleContains("Tilte")); |
Example of WebDriverWait:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class WebDriver_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); WebElement element = driver.findElement(By.id("id")); // Create a Object wait of WebdriverWait class WebDriverWait wait = new WebDriverWait(driver,30); // Using ExpectedConditions wait until element visibility wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input"))); // Wait until alert present on page wait.until(ExpectedConditions.alertIsPresent()); // Wait until element to be clickable on page wait.until(ExpectedConditions.elementToBeClickable(element)); // Wait until element to be selectable on page wait.until(ExpectedConditions.elementToBeSelected(element)); // Wait until frame is available and frame selected. wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element)); // Wait and check element is invisibility wait.until(ExpectedConditions.invisibilityOf(element)); // Wait until present element located by. wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By) element)); // Wait until text present on particular an element wait.until(ExpectedConditions.textToBePresentInElement(element, "Text")); // Wait until element value present for a particular element. wait.until(ExpectedConditions.textToBePresentInElementValue(element, "Value")); // check element visibility wait.until(ExpectedConditions.visibilityOf(element)); // Wait and check title contains or not. wait.until(ExpectedConditions.titleContains("Tilte")); } } |
3. PageLoadTimeout Command
- Determines the amount of time to wait for the page load to complete before the error is thrown at the end of time.
- If the timeout is negative, then the page load may be indefinite until the pageload.
Example:
1 | driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS); |
Example of PageLoadTimeout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class pageLoadTimeout_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS); } } |
4. SetScriptTimeout Command
- Before throwing an error, the amount of time is set to wait for the asynchronous script to complete the execution.
- If timeout is negative, then the script will be allowed to run indefinitely.
Example:
1 | driver.manage().timeouts().setScriptTimeout(500, TimeUnit.SECONDS); |
Example of SetScriptTimeout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class setScriptTimeout_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.manage().timeouts().setScriptTimeout(500, TimeUnit.SECONDS); } } |
3. FluentWait Command
- Fluent Wait uses two parameters to handle wait– timeout value and polling frequency.
- Fluent wait is another type of Explicit wait and you can define polling and ignore the exception to continue with script execution in case element is not found in webpage.
First of all, it sets the following values.
- Maximum time to wait for any condition.
- Frequency to check the success or failure of a specified position.
1 2 3 4 5 6 7 | FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(timeoutSeconds, TimeUnit.SECONDS) .pollingEvery(500, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); |
Example of FluentWait using WebDriver wait:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Fluent_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); WebElement stqatools = driver.findElement(By.id("stqatools")); new WebDriverWait(driver, 10) .pollingEvery(2, TimeUnit.SECONDS) .withTimeout(10, TimeUnit.SECONDS) .until(ExpectedConditions.visibilityOf(stqatools)); } } |
Implicit, Explicit, & Fluent Wait in Selenium WebDriver and WebDriver Wait, Implicit Wait, Explicit Wait, Fluent Wait and Implicit and Explicit Wait in Selenium and Implicit and Explicit Wait in Selenium and Implicit and Explicit Wait in Selenium