Synchronization commands in webdriver

When test automation executes, there should be appropriate communication between automation tool and application. This means when automation tool executes faster and application objects are not loaded that time, then automation test case will fails. In such case, automation tool should wait till objects are ready in application so that synchronization happens between tool and application under test so that chances of test case execution will pass.

Synchronization can be achieve in two ways.
  1. Implicit Waits
  2. Explicit Waits
ImplicitWait and ExplicitWait in Selenium
1) Implicit Waits:
Implicit wait is to tell webdriver to wait for a certain amount of time when trying to find an element or elements if they are not immediately available. This means selenium will wait maximum of implicit wait time before throwing an exception error, but in case that element exists before that maximum time, still it will be waiting for that much time.

The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Syntax:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

2) Explicit Waits:
We can tell the tool to wait only till the Condition satisfy. Once the condition is satisfy, the tool proceed with the next step. This can be done with WebDriverWait in conjunction with ExpectedConditions Class. 

Syntax:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement o_element = wait.until(ExpectedConditions.elementToBeClickable(By.Name("ObjectName")));






ImplicitWait and ExplicitWait in Selenium

1) Wait for element to be clickable on webpage by Selenium WebDriver
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));

2) Wait for text to be present on webpage
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("time"), "Time left: 10 secs"));

3) Wait for alert
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.alertIsPresent());

4) Wait till element visible or appear or present on page
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("text3")));

5) Wait till element becomes invisible or hidden
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//input[@id='text1']")));

3) Fluent Waits:
The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.

Syntax:
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)

.ignoring(Exception.class);


<-- Previous || Next -->

No comments:

Post a Comment