For AI agents and LLMs: a machine-readable index is available at llms.txt. A plain-Markdown version of any documentation page is available by appending .md to its URL.
Skip to main content

How to Use Lambda Hooks in Selenium on TestMu AI


TestMu AI offers a set of hooks (Lambda Hooks) that you can use to modify your automation test cases and perform multiple operations in your automation scripts: set the test status, verify downloaded files, throttle the network, capture screenshots, and more.

Hooks are grouped into three categories so you can quickly find what applies to your framework:

  • Common Lambda Hooks: capabilities available in both Selenium and Playwright sessions
  • Selenium-only Lambda Hooks: available only via the Selenium JavascriptExecutor
  • Playwright-only Lambda Hooks: available only in Playwright (and other CDP-based) sessions

How Lambda Hooks Are Invoked​


The invocation syntax differs by framework.

Selenium: pass the hook as a string to the JavascriptExecutor:

Verified
((JavascriptExecutor) driver).executeScript("lambda-status=passed");

Some Selenium hooks (AutoHeal, Lighthouse, accessibility scan) instead take a JSON payload via the lambdatest_executor form:

Verified
driver.executeScript("lambdatest_executor:{\"action\":\"lambda-heal-start\"}");

Playwright: pass a lambdatest_action JSON payload as the argument of an (empty) page.evaluate call:

Verified
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'passed', remark: 'Title matched' } })}`);

Common Lambda Hooks (Selenium & Playwright)​


These capabilities are available in both frameworks. Note that the hook name and syntax differ per framework, so use the form shown for your framework. For example, the lighthouseReport hook lets you generate Lighthouse reports for any URL during a session.

HookSeleniumPlaywrightDescription
lambda-statusdriver.executeScript("lambda-status=passed");

Supported values: passed, failed, skipped, ignored, unknown, error
Use the setTestStatus action (also supports a remark shown on the dashboard):

await page.evaluate(_ => {}, 'lambdatest_action: {"action": "setTestStatus", "arguments": {"status": "passed", "remark": "Title matched"}}');
Mark the test passed / failed (also skipped, ignored, unknown, error).
lambda-testCase-start / lambda-testCase-enddriver.executeScript("lambda-testCase-start=login flow");

// ...test steps...

driver.executeScript("lambda-testCase-end=login flow");
await page.evaluate(_ => {}, 'lambdatest_action: {"action": "lambda-testCase-start", "arguments": {"name": "login flow"}}');

// ...test steps...

await page.evaluate(_ => {}, 'lambdatest_action: {"action": "lambda-testCase-end", "arguments": {"name": "login flow"}}');
Group commands into test cases

(annotations in the command-logs view).
lambdaUpdateNamedriver.executeScript("lambdaUpdateName=TestName");await page.evaluate(_ => {}, 'lambdatest_action: {"action": "lambdaUpdateName", "arguments": {"name": "TestName"}}');Update the test name during execution.
lambda-unbound-pingdriver.executeScript("lambda-unbound-ping=lambdatest.com");Use the lambda-unbound-ping action via the lambdatest_action pattern.Fetch IPs from the outbound domain.
lighthouseReportdriver.executeScript("lambdatest_executor:{\"action\":\"lighthouseReport\"}");

Lighthouse reports are also generated automatically on navigation when the performance capability is enabled.
await page.evaluate(_ => {}, 'lambdatest_action: {"action": "lighthouseReport"}');Generate a Lighthouse performance report.
lambda-accessibility-scandriver.executeScript("lambdatest_executor:{\"action\":\"lambda-accessibility-scan\"}");Use the lambda-accessibility-scan action via the lambdatest_action pattern.Run an accessibility scan.

Selenium-Only Lambda Hooks​


These hooks work only through the Selenium JavascriptExecutor and are not available in Playwright sessions. Some behaviors that these hooks toggle at runtime can also be enabled up front through Selenium automation capabilities.

HookDescriptionExample
lambda-file-existsCheck whether the downloaded file exists in the test machine.((JavascriptExecutor) driver).executeScript("lambda-file-exists=file-name.file_format");
lambda-file-statsRetrieve file metadata such as md5 code, modified time, name and size.((JavascriptExecutor) driver).executeScript("lambda-file-stats=file-name.file_format");
lambda-file-contentDownload file content using base64 encoding.((JavascriptExecutor) driver).executeScript("lambda-file-content=file-name.file_format");
lambda-file-listList down the file in download directory.print driver.execute_script("lambda-file-list={match string with filename}");

ie:print driver.execute_script("lambda-file-list=sample");

Response: List of files in downloads dir starting with sample
lambda-files-deleteDeletes the file in the download directory in the virtual machines (VMs).driver.executeScript("lambda-files-delete=file1.csv,file2.csv);
lambda-nameFor changing the test name.((JavascriptExecutor) driver).executeScript("lambda-name=TestName");

((JavascriptExecutor) driver).executeScript("lambda-name=" + "name from hooks");
lambda-buildFor updating the build name.executeScript("lambda-build=BUILD_NAME");
lambda-actionUsed to mark a test as passed/failed. Moreover, it allows the option to include a failure reason, which will be visible on the TestMu AI Automation Dashboard inside the session view.Map<String, String> action = new HashMap();action.put("status", "failed"); action.put("reason", "tmp reason"); driver.executeScript("lambda-action", action);

((JavascriptExecutor) driver).executeScript("lambda-action=" + "Lambda Error");
lambda-perform-keyboard-eventsYou can simulate keyboard shortcuts like ctrl + c, ctrl + v in automation test scenarios. This hook is supported on both Windows and MacOS.js.executeScript("lambda-perform-keyboard-events:tab");
lambda-breakpointAborts the test execution to use the live interaction feature.driver.executeScript("lambda-breakpoint=true");
lambda-screenshotCaptures the async screenshot during test execution.driver.executeScript("lambda-screenshot=true");
lambda-throttle-networkThrottles network speed during test execution.executeScript("lambda-throttle-network","Regular 4G")
lambda-pingFetches the IPs of the domain.driver.executeScript("lambda-ping=lambdatest.com");
lambda-exceptionsUploads the exceptions for tests that are captured on the console.driver.executeScript('lambda-exceptions', [[message]])
lambda-get-clipboardPrints the clipboard data on the console.driver.executeScript("lambda-get-clipboard");
lambda-set-clipboardSets the clipboard data.driver.executeScript("lambda-set-clipboard= Amit");
lambda-clear-clipboardClears the data of the clipboard.driver.executeScript("lambda-clear-clipboard");
lambda:networkFetches the network log entries in array format during session.driver.execute_script("lambda:network");- Fetch the network log from last fetch request time to current time.

driver.execute_script("lambda:network=all");- Fetch from start of test session to current time.
lambda:network=full.harFetches the complete network HAR log at runtime during the test session.Syntax : driver.execute_script("lambda:network=full.har");

Note :
Required Capabilities: Both network and network.full.har must be set to true in your test capabilities
lambda-test-tagsDynamically update your test tags for a test session which can be used to organize and filter your test results.Syntax : driver.executeScript("lambda-test-tags", "Tag 1,Tag 3,Tag 2");

Limitations :
1. Maximum Character Length per Tag: Each tag can have up to 50 characters.
2. Maximum Number of Tags: A maximum of 15 tags can be assigned to a single test session.
lambda-heal-start / lambda-heal-stopEnables / disables AutoHeal with hooks for the portion of the test between the two calls, so locator failures are healed automatically.driver.execute_script('lambdatest_executor:{"action":"lambda-heal-start"}')

// ...steps with dynamic locators...

driver.execute_script('lambdatest_executor:{"action":"lambda-heal-stop"}')

Playwright-Only Lambda Hooks​


These hooks are available only in Playwright (and other CDP-based) sessions, using the lambdatest_action pattern.

HookDescription
getTestDetailsReturns details of the running test, such as the test ID and session information.

await page.evaluate(_ => {}, 'lambdatest_action: {"action": "getTestDetails"}');
lambdaSetBrowserPositionSets the browser window position (useful when running multiple browser windows in a single session).

await page.evaluate(_ => {}, 'lambdatest_action: {"action": "lambdaSetBrowserPosition"}');

Note: These hooks only work if you are connected to your TestMu AI Hub URL. If you use these hooks on any other platform, you might see the error: javascript error: Invalid left-hand side in assignment

Next Steps​


Continue with these related guides:

Terminal First Testing With Kane CLI

Natural language browser & mobile app tests right from terminal.

×
Schedule Your Personal Demo
Kane CLI terminal

Help and Support

Related Articles