LambdaTest Hooks in TypeScript for wdio Test Status

How to use lambda hooks for wdio in typescript in order to mark the test are passed or failed

1 Like

Hey Tim,

In WebDriverIO (wdio) with TypeScript, using LambdaTest hooks to mark tests as either passed or failed involves utilizing the driver.execute method to send a custom command to the browser. LambdaTest allows you to set a custom status for each test using the “lambda-status” parameter.

Here’s a more detailed explanation:

// Import necessary modules and interfaces from WebDriverIO and LambdaTest
import { browser, WebDriver } from 'webdriverio';

/**
 * Function to mark the test as passed or failed on LambdaTest using custom Lambda hooks.
 * @param result - The result of the test (0 for passed, 1 for failed).
 */
async function markLambdaTestStatus(result: number): Promise<void> {
  try {
    // Access the WebDriver instance from the browser object
    const driver: WebDriver = browser;

    // Concatenate the custom status to be sent to LambdaTest based on the test result
    const lambdaStatus: string = `lambda-status=${result === 0 ? 'passed' : 'failed'}`;

    // Use driver.execute to send the custom status to LambdaTest
    await driver.execute((status: string) => {
      // Execute JavaScript code in the browser context
      // Set the custom status as a query parameter in the test's URL
      window.location.search = status;
    }, lambdaStatus);

    console.log(`LambdaTest status set to: ${result === 0 ? 'Passed' : 'Failed'}`);
  } catch (error) {
    console.error('Error occurred while setting LambdaTest status:', error);
  }
}

// Example usage: Call the function and pass the test result (0 for passed, 1 for failed)
markLambdaTestStatus(0); // Mark the test as passed

TypeScript function markLambdaTestStatus takes the test result as an argument and uses the driver.execute method to set the custom LambdaTest status based on the result. Additionally, it includes comments to explain each step and improve code readability.

Thanks for your question! We’re always here to help, so ask away anytime.