What's the difference between implicitly wait an explicitly wait?
Implicit wait and explicit wait are two different mechanisms used in test automation frameworks to handle synchronization issues between test scripts and the application being tested.
Implicit Wait:
Explicitly Wait
Here are the examples of Implicit Wait and Explicit Wait in Java using Selenium WebDriver:
java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ImplicitWaitExample {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Set implicit wait to 10 seconds
driver.get("https://www.example.com");
// Find and interact with elements
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("loginButton")).click();
// Rest of the test script...
driver.quit();
}
}
In the above example, an implicit wait of 10 seconds is set using driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS). This means that if an element is not immediately found, the driver will wait up to 10 seconds before throwing an exception.
java
import org.openqa.selenium.By;
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 ExplicitWaitExample {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10); // Set explicit wait to 10 seconds
driver.get("https://www.example.com");
// Wait for element to be visible and interact with it
WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
usernameField.sendKeys("testuser");
// Wait for element to be clickable and interact with it
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginButton")));
loginButton.click();
// Rest of the test script...
driver.quit();
}
}
In the above example, an explicit wait of 10 seconds is set using WebDriverWait and ExpectedConditions. The wait.until() method waits for a specific condition to be met before proceeding. In this case, we wait for the username field to be visible and then interact with it, and we also wait for the login button to be clickable before clicking on it.
Note: Make sure to have the Selenium WebDriver and required dependencies properly configured in your Java project for the above code examples to work correctly.
To summarize, the main differences between implicit and explicit waits are:
In general, it's recommended to use explicit wait when you need more precise control over waiting for specific elements or conditions, while implicit wait can be used for simpler scenarios where a global wait time suffices.