|
| 1 | +--- |
| 2 | +id: autoheal-with-hooks |
| 3 | +title: How to use AutoHeal using Hooks |
| 4 | +hide_title: false |
| 5 | +sidebar_label: AutoHeal using Hooks |
| 6 | +description: Learn how to use LambdaTest Auto Healing feature in your Selenium tests to avoid test flakiness and increase reliability of your test suites. |
| 7 | +keywords: |
| 8 | +- auto heal lambdatest |
| 9 | +- auto heal test flakiness restrictions |
| 10 | +- auto healing lambdatest |
| 11 | +url: https://www.lambdatest.com/support/docs/autoheal-with-hooks/ |
| 12 | +site_name: LambdaTest |
| 13 | +slug: autoheal-with-hooks/ |
| 14 | +--- |
| 15 | + |
| 16 | +import Tabs from '@theme/Tabs'; |
| 17 | +import TabItem from '@theme/TabItem'; |
| 18 | + |
| 19 | + |
| 20 | +<script type="application/ld+json" |
| 21 | + dangerouslySetInnerHTML={{ __html: JSON.stringify({ |
| 22 | + "@context": "https://schema.org", |
| 23 | + "@type": "BreadcrumbList", |
| 24 | + "itemListElement": [{ |
| 25 | + "@type": "ListItem", |
| 26 | + "position": 1, |
| 27 | + "name": "Home", |
| 28 | + "item": "https://www.lambdatest.com" |
| 29 | + },{ |
| 30 | + "@type": "ListItem", |
| 31 | + "position": 2, |
| 32 | + "name": "Support", |
| 33 | + "item": "https://www.lambdatest.com/support/docs/" |
| 34 | + },{ |
| 35 | + "@type": "ListItem", |
| 36 | + "position": 3, |
| 37 | + "name": "AutoHeal with Hooks", |
| 38 | + "item": "https://www.lambdatest.com/support/docs/autoheal-with-hooks/" |
| 39 | + }] |
| 40 | + }) |
| 41 | + }} |
| 42 | +></script> |
| 43 | +Autoheal feature automatically handles dynamic element changes during Selenium test execution. This is especially useful for tests where elements’ IDs, XPaths, or attributes may change between runs. Autoheal ensures your tests are more resilient and reduces flakiness. |
| 44 | + |
| 45 | +Autoheal can be started or stopped at any point in your test script using simple hooks. This allows for precise control over when your tests should attempt element healing, improving reliability and reducing flaky tests. This guide explains how to enable and disable Autoheal in your Selenium scripts using the lambdatest hooks. |
| 46 | + |
| 47 | +## Using Autoheal Hooks in Tests |
| 48 | +Autoheal hooks can be inserted anywhere in the script depending on where you need dynamic element handling. Typical flow: |
| 49 | + |
| 50 | +- Navigate to your test page. |
| 51 | +- [Start Autoheal](/support/docs/autoheal-with-hooks/#enable-autoheal) before interacting with elements that may change. |
| 52 | +- Perform element actions (find, click, send keys, etc.). |
| 53 | +- [Stop Autoheal](/support/docs/autoheal-with-hooks/#disable-autoheal) after these actions. |
| 54 | +- Continue the rest of your test normally. |
| 55 | + |
| 56 | +This gives you fine-grained control over Autoheal, improving test stability without affecting unrelated test steps. |
| 57 | + |
| 58 | +## Enable AutoHeal |
| 59 | +Use the following hook to **start** Autoheal at any point in your Selenium test script: |
| 60 | + |
| 61 | +```javascript |
| 62 | +driver.execute_script('lambdatest_executor:{"action":"lambda-heal-start"}') |
| 63 | +``` |
| 64 | + |
| 65 | +**Usage :** Place this hook right before interacting with elements that may dynamically change during the test. |
| 66 | + |
| 67 | +## Disable AutoHeal |
| 68 | +Use the following hook to **stop** Autoheal at any point in your Selenium test script: |
| 69 | + |
| 70 | +```javascript |
| 71 | +driver.execute_script('lambdatest_executor:{"action":"lambda-heal-stop"}') |
| 72 | +``` |
| 73 | + |
| 74 | +**Usage :** Place this hook immediately after the actions requiring Autoheal are completed. This ensures subsequent test steps execute with normal Selenium behavior. |
| 75 | + |
| 76 | +## Sample Script |
| 77 | + |
| 78 | +```python title="Test.py" |
| 79 | +import os |
| 80 | +import time |
| 81 | +from selenium import webdriver |
| 82 | + |
| 83 | +platform = os.getenv('HYPEREXECUTE_PLATFORM') |
| 84 | + |
| 85 | +desired_cap_chrome = { |
| 86 | + "build": "Lambdatest Build 2", |
| 87 | + "name": "Lambdatest Test", |
| 88 | + "platform": platform, |
| 89 | + "browserName": "Chrome", |
| 90 | + "version": "latest", |
| 91 | + "visual": False, |
| 92 | + "video": True, |
| 93 | + "network": True, |
| 94 | + "console": True, |
| 95 | + 'goog:chromeOptions': {'args': ['--window-size=400x300']}, |
| 96 | +} |
| 97 | + |
| 98 | +hub = f"https://{os.getenv('LT_USERNAME')}:{os.getenv('LT_ACCESS_KEY')}@hub.lambdatest.com/wd/hub" |
| 99 | + |
| 100 | +def test_autoheal(caps, sleep_time=5): |
| 101 | + driver = webdriver.Remote(command_executor=hub, desired_capabilities=caps) |
| 102 | + |
| 103 | + # Navigate to page |
| 104 | + driver.get("https://www.selenium.dev") |
| 105 | + |
| 106 | + # ===== Autoheal START ===== |
| 107 | + driver.execute_script('lambdatest_executor:{"action":"lambda-heal-start"}') |
| 108 | + |
| 109 | + # Interact with elements that may change dynamically |
| 110 | + driver.find_element_by_id('td-block-1') |
| 111 | + driver.execute_script('document.getElementById("td-block-1").id="updatedtd-block-1";') |
| 112 | + driver.find_element_by_id('updatedtd-block-1') |
| 113 | + |
| 114 | + # ===== Autoheal STOP ===== |
| 115 | + driver.execute_script('lambdatest_executor:{"action":"lambda-heal-stop"}') |
| 116 | + |
| 117 | + # Continue normal test steps |
| 118 | + driver.execute_script("console.log('Test completed successfully');") |
| 119 | + driver.execute_script("lambda-status=passed") |
| 120 | + |
| 121 | + driver.quit() |
| 122 | + |
| 123 | +test_autoheal(desired_cap_chrome) |
| 124 | +``` |
| 125 | + |
| 126 | +With these steps, you can seamlessly integrate LambdaTest HyperExecute Autoheal into your Selenium tests, reducing flaky tests and improving stability. |
0 commit comments