Waits

In Appium, waiting is an essential concept to handle synchronization issues between the automation script and the web application being tested. The primary purpose of waiting is to ensure that the script does not attempt to interact with elements before they are properly loaded or become available. There are two main types of waiting in Appium: Implicit Wait and Explicit Wait.

Implicit Wait



An implicit wait instructs the WebDriver to wait for a certain amount of time before throwing an exception. It is set globally for the entire script execution.

In the following example the implicit wait ensures that the WebDriver waits up to 30 seconds for an element to be present before throwing an exception. This wait is applied for every finding element operation throughout the script.

Java
C#
Python
Javascript
Kotlin
Copy
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
AndroidDriver driver = new AndroidDriver<>(appiumServerUrl, options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));

//Click on the login button
driver.findElement(By.id("loginButton")).click();

driver.quit();

Explicit Wait



An explicit wait is more targeted and allows the script to wait for a specific condition to be met before proceeding. It is applied to a particular element or a certain condition. The explicit wait in the following example is applied only to the specific Element ie element with id welcomelabel. The script waits up to 10 seconds for the element to be visible before throwing an exception.

Java
C#
Python
Javascript
Kotlin
Copy
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
AndroidDriver driver = new AndroidDriver(appiumServerUrl, options);

//Click on the login button
driver.findElement(By.id("loginButton")).click();

// Initialize WebDriverWait with a 10-second timeout
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

// Wait until the element is visible before interacting
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElementId")));
element.click();

driver.quit();

FluentWait

Fluent Wait is an advanced type of Explicit Wait in Appium that allows you to customize various parameters that will change how the conditions are evaluated. You can define custom polling intervals and exceptions to wait for a certain condition to be met. It is more flexible than the regular WebDriverWait by providing a mechanism for defining the maximum amount of time to wait for a condition and the frequency with which to check the condition.

Java
C#
Python
Javascript
Kotlin
Copy
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
AndroidDriver driver = new AndroidDriver(appiumServerUrl, options);

//Click on the login button
driver.findElement(By.id("loginButton")).click();

// Define the Fluent Wait
FluentWait<AndroidDriver> wait = new FluentWait<>(driver);
wait.withTimeout(Duration.ofSeconds(30))      // Maximum wait time
	.pollingEvery(Duration.ofSeconds(5))      // Frequency of checking
	.ignoring(NoSuchElementException.class)   // Exceptions to ignore
	.withMessage("Element not found after 30 seconds"); // Custom error message

// Use the wait for a specific condition
WebElement element = wait.until((Function<AppiumDriver, WebElement>) d -> d.findElement(By.id("element_id")));

driver.quit();

In the above example, the script waits up to 10 seconds for the element with the ID welcomelabel to be displayed. It checks for the element's presence every 500 milliseconds. The NoSuchElementException is ignored during the polling.