Implementing Explicit and Implicit Waits in Appium with Python

Implementing Explicit and Implicit Waits in Appium with Python

5 April 2024 Stephan Petzl Leave a comment Tech-Help

When automating tests for a native iOS application using Appium with Python, one common challenge is ensuring that the test scripts wait adequately for elements to be loaded or become interactive before proceeding with further actions. This article provides guidance on how to implement waits effectively in your test scripts.

Understanding Waits in Appium

There are two primary types of waits that can be used in Appium:

  • Explicit Waits: These waits are used to halt the execution until a certain condition is met or until a maximum time has elapsed. Explicit waits are beneficial when you need to wait for a specific element to reach a particular state, such as becoming clickable or visible.
  • Implicit Waits: These waits are set for the duration of the WebDriver object and provide a default waiting time between each consecutive test step across the entire test script. Implicit waits are useful for setting a baseline wait time but can lead to longer test times if set too high.

Using Explicit Waits

To implement an explicit wait, you will need to use the WebDriverWait class in conjunction with expected_conditions provided by Selenium WebDriver. Here’s a step-by-step example:

  1. Import the necessary modules at the beginning of your test script:
  2. from appium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
                    
  3. Set up your WebDriver instance:
  4. self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
                    
  5. Implement the explicit wait where necessary in your test script:
  6. wait = WebDriverWait(self.driver, 20)
    currently_waiting_for = wait.until(EC.element_to_be_clickable((By.XPATH, '//UIAApplication[1]/UIAWindow[1]/UIAButton[@text="example text"]')))
                    

    This code waits up to 20 seconds for an element with the specified XPATH to become clickable.

Using Implicit Waits

For scenarios where you expect all elements to be loaded within a certain time frame, you can set an implicit wait as follows:

self.driver.implicitly_wait(10)  # Time in seconds
            

This line instructs the WebDriver to wait up to 10 seconds before throwing a ‘NoSuchElement’ exception when trying to find an element.

Additional Tips

  • It’s generally recommended to use explicit waits rather than implicit waits, as they allow for more granular control over the wait conditions.
  • If you’re new to locating elements, consider using tools like uiautomatorviewer that comes with Appium to find XPATHs and other locators.
  • Be mindful of using waits appropriately to avoid unnecessarily long test execution times.

By implementing waits effectively, you can create more reliable and robust Appium tests for your iOS applications. With the knowledge of explicit and implicit waits, you’re now equipped to handle synchronization issues in your test scripts.

Like this article? there’s more where that came from!