Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Improving Selenium Test Stability with Pytest Retries and Waits

    Improving Selenium Test Stability with Pytest Retries and Waits

    December 23, 2024

    Introduction

    Flaky tests—those that fail intermittently—are a common headache for test automation teams. They can be especially frustrating in Selenium tests because of the dynamic nature of web applications. Elements might take time to load, page navigation could be slow, or JavaScript-heavy applications might delay interactions. These issues lead to false negatives in tests, where tests fail even though the application works fine.

    In this blog, we’ll explore how to use Pytest retries and explicit/implicit waits to improve the stability of your Selenium tests and reduce flaky test failures.

    Picture9

    Why Selenium Tests Are Flaky

    Flaky tests typically fail due to the following issues:

    • Dynamic Content: Elements that take time to load (like AJAX content) or slow-rendering pages.
    • Network Issues: Delays or failures in loading resources or API calls.
    • Timing Issues: Trying to interact with elements before they’re fully loaded or ready.

    The key to reducing flaky tests lies in two techniques: retries and waits.

    Using Pytest Retries for Flaky Tests with pytest-rerunfailures

    A simple solution to mitigate flaky tests is to retry failed tests a certain number of times. The pytest-rerunfailures plugin allows you to automatically rerun tests that fail, thus reducing the impact of intermittent failures.

    1. Installation: Install the pytest-rerunfailures plugin:
      bash
      pip install pytest-rerunfailures
      
    2. Configuration: To enable retries, use the –reruns option when running your tests. For example, to retry a failed test 3 times, run:
      bash
      pytest --reruns 3
      

      You can also set the number of retries in your pytest.ini configuration file:

      ini
      
      [pytest]
      
      reruns = 3
      
      rerunsDelay = 2  #Delay between retries in seconds

       

    3. Example of Retries: Let’s say you have a test that clicks a button to submit a form. Sometimes, due to timing issues, the button might not be clickable. By adding retries, the test will automatically retry if the failure is due to a transient issue.
      def test_submit_button(mocker):
          # Simulate flaky behavior
          mocker.patch('selenium.webdriver.common.by.By.ID', return_value='submit')
          # Trigger a click action on the button
          button = driver.find_element_by_id('submit')
          button.click()
          assert button.is_enabled()  # Check button state
      

    Using Waits to Ensure Elements Are Ready

    In Selenium, waits are crucial to ensure that the elements you want to interact with are available and ready. There are two types of waits: implicit and explicit.

    1. Implicit Waits: Implicit waits instruct the WebDriver to wait a certain amount of time for elements to appear before throwing an exception.
      driver.implicitly_wait(10)  # Waits for 10 seconds for elements to load

      While easy to use, implicit waits can sometimes slow down tests and make debugging more difficult because they apply globally to all elements.

    2. Explicit Waits: Explicit waits are more powerful and precise. They allow you to wait for specific conditions before proceeding with interactions. WebDriverWait combined with expected conditions is commonly used.Example: Wait for an element to be clickable before clicking on it:
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      
      wait = WebDriverWait(driver, 10)
      element = wait.until(EC.element_to_be_clickable((By.ID, "submit_button")))
      element.click()
      
    3. Using Waits for AJAX Content: Often, web pages use AJAX to load content dynamically. Explicit waits are perfect for waiting until AJAX calls finish loading.
      # Wait until the AJAX content is visible
      wait.until(EC.visibility_of_element_located((By.ID, "ajax-content")))
      

    Best Practices for Waits and Retries

    • Use explicit waits for better control: Explicit waits allow you to wait for specific conditions (like visibility or clickability), improving test reliability.
    • Combine retries with waits: Ensure that retries are only triggered after sufficient wait time to account for potential page load or element rendering delays.
    • Optimize test timing: Use waits for specific elements rather than using global implicit waits, which can slow down tests.

    Conclusion

    By using Pytest retries and explicit/implicit waits, you can significantly improve the stability of your Selenium tests. Retries help handle intermittent failures, while waits ensure that elements are ready before interacting with them. Together, these strategies reduce flaky test results, making your test suite more reliable and consistent. Happy Testing!

     

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleLog Alarm Package for Laravel
    Next Article The Next Frontier in QA : Highlights from the Perficient – BrowserStack Partner Day

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 15, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4695 – PHPGurukul Cyber Cafe Management System SQL Injection

    May 15, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    B2B Testers

    Development

    How to Draw Basic Shapes with SVG

    Development

    How to Manage Recurring ACH Payments in QuickBooks

    Artificial Intelligence

    CVE-2025-3488 – WordPress WPML Stored Cross-Site Scripting Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    PlayStation launches its Portal remote player in midnight black color

    January 8, 2025

    Sony unveils new PlayStation accessories in Midnight Black, including the PS Portal, DualSense controllers, and…

    AI Guide for new CFOs

    July 27, 2024

    CVE-2025-4176 – PHPGurukul Blood Bank & Donor Management System SQL Injection Vulnerability

    May 1, 2025

    CVE-2025-28367 – MojoPortal Directory Traversal Vulnerability

    April 21, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.