Types of Waits:
1. Implicit Wait
2. Explicit Wait
- Webdriver Wait
- Fluent Wait
Implicit Wait-
Pros-
1. Declared globally. Wait for a max of n number of seconds before you throw error.
2. Readable code.
Cons-
1. Performance cause issues
2. if implicit wait is set to 20 s say, then we cannot find which components are slowly loading.
Explicit Wait-
Pros-
1. Specific to that locator only.
2. Wait is applied at only targeted elements. So no performance issues.
Cons-
1. More written code and disturbs readability.
_____________________________________________________________________________
Explicit Waits are of two types:
1. Webdriver Wait
2. Fluent Wait
Both implement the Wait interface.
FluentWait doesn't have methods to use, so we need to create custom methods for it to use it.
________________________________________________________________________________
Syntax/examples:
1. Fluent Wait
Wait<Webdriver> wait = new FluentWait<Webdriver>();
.withTimeout(10,SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class)
2. Implicit Wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Duration.of(5) not working
3. Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.className("message"), "Success"));
wait.until(ExpectedConditions.alertIsPresent());
4. Thread.sleep
Halts the code for n seconds.
No comments:
Post a Comment