Update LambdaTest test session with test run status

In this post, we will show you how to update the test run status on LambdaTest. Although there are many articles online about integrating LambdaTest for running automation tests, most of them do not discuss how to update the test run status and importance of it. This post is a follow up to running test automation on LambdaTest using pytest. In that post, we covered how to integrate LambdaTest to run automation tests and retrieve the session URL to review the test results. A logical next step would be ensuring your team can see the test results reported on LambdaTest’s UI as well.


Why do we need to update the LambdaTest session with the test run status?

Updating the test run status on LambdaTest is crucial for clear reporting and effective test management. It allows everyone on the team to see which tests passed or failed in a centralized dashboard, making it easier to track progress and outcomes. This visibility speeds up debugging by providing logs, screenshots, and videos of failed tests, helping developers quickly identify and fix issues.

It also integrates well with CI/CD pipelines, ensuring that build statuses are updated automatically and triggering alerts when necessary. By keeping the team aligned on test results, it enhances collaboration and decision-making.

Furthermore, updating test statuses helps identify flaky tests and optimize the test suite for better stability. It provides valuable analytics and insights on test trends and performance, aiding in more informed decisions. For teams that need to comply with audit requirements, it offers a clear record of test results.


How to update the test run status on a LambdaTest session?

There are several ways to update the test run status on LambdaTest, but we will discuss the following two recommended methods:

  1. Using Webdriver
  2. Using LambdaTest REST API

Update LambdaTest Session Status Using WebDriver:

This is a very easy, quick, and robust way to update the test run status on a LambdaTest session. We need to execute a JavaScript command using WebDriver. If the test passes, run:

driver.execute_script("lambda-status=passed")

Otherwise, run:

driver.execute_script("lambda-status=failed")

See our updated sample test below, which demonstrates how to report the test run status to LambdaTest.

Note: This is highly simplified code to make this post illustrative. We do not use this quality of code at clients.

#NOTE: This is highly simplified code to make this post illustrative
#We do not use this quality of code at clients
#Our framework uses the Page Object pattern
 
from lambdatest_runner import LambdaTestRunner
 
#Contents of test_example_form.py
def test_example_form(os_name, os_version, browser, browser_version,
                                 remote_project_name, remote_build_name, testname):
    "Test example form"
    try:
        #Create an driver object
        runner = LambdaTestRunner()
        driver,session_url = runner.get_lambdatest_webdriver(os_name, os_version, browser, browser_version,
                                            remote_project_name, remote_build_name, testname)
        print("\nRunning test on LambdaTest Platform")
        print("Session url:",session_url)
        #Create variables to keep count of pass/fail
        pass_check_counter = 0
        total_checks = 0
        #Visit the tutorial page
        driver.get('http://qxf2.com/selenium-tutorial-main') 
        print("Browser: ",browser)
        print("Browser version: ",browser_version)
        print("OS: ",os_name)
        print("OS version: ",os_version)
        print("\n")
        print("Navigated to Qxf2's Selenium Tutorial page")
        #Check 1: Is the page title correct?
        total_checks += 1 
        if(driver.title=="Qxf2 Services: Selenium training main"):
            print("Success: Title of the Qxf2 Tutorial page is correct")
            pass_check_counter += 1
        else:
            print("Failed: Qxf2 Tutorial page Title is incorrect")
 
    except Exception as e:
        print(f"An error occurred: {e}")
 
    finally:
        # Update the test status on LambdaTest using JavaScript
        status = "passed" if total_checks == pass_check_counter else "failed"
        driver.execute_script(f"lambda-status={status}")
 
        # Quit the browser window
        driver.quit() 
 
        # Assert to know test status
        assert total_checks == pass_check_counter

Check the LambdaTest session automation page to verify the test run status update. See the attached screenshot below.

LambdaTest Tet Run Status Update
LamdaTest Session Status Update using Webdriver

Update LambdaTest Session Status Using the REST API:

This method is useful when your WebDriver is not active or not in scope. It requires the session ID to update the LambdaTest session status. You need to make a PATCH call after the completion of the test run. Call the method below with the LambdaTest session ID, test run status, and LambdaTest credentials at the end of the test. You can call it from pytest hooks that execute at the end of the test, such as “pytest_terminal_summary” or “pytest_sessionfinish.”

Note: This is highly simplified code to make this post illustrative. We do not use this quality of the code at clients.

import requests
 
def update_lambdatest_session_status(session_id, status, username, access_key):
    "Update the test run status of a LambdaTest session."
    # Define the LambdaTest API endpoint URL
    url = f"https://api.lambdatest.com/automation/api/v1/sessions/{session_id}"
 
    # Define the headers 
    headers = {"Content-Type": "application/json"}
 
    # Define the data payload to update only the "status_ind"
    data = {"status_ind": status }  # Only update the "status_ind"
 
    # Make the PATCH request with authentication
    response = requests.patch(url, json=data, headers=headers, auth=(username, access_key))
 
    # Check if the request was successful
    if response.status_code == 200:
        print(f"Successfully updated session {session_id} status to '{status}'.")
    else:
        print(f"Failed to update session status. Status code: {response.status_code}")
        print(f"Response: {response.text}")
 
# Example usage
update_lambdatest_session_status(
    session_id="your_session_id_here",  # Replace with your session ID
    status="test_status",                    # Replace with desired status: "passed","failed" or "error"
    username="your_username_here",      # Replace with your LambdaTest username
    access_key="your_access_key_here"   # Replace with your LambdaTest access key
)

For demonstration purposes, I updated the same LambdaTest session shown in the previous method using the code above. See the attached screenshot.

LamdaTest Session Status Update using REST API
LamdaTest Session Status Update using REST API

I hope this post helps you understand the importance of updating the test run status on a LambdaTest session and the different methods available for doing so.


Let Qxf2 kickstart your test automation journey

Several early stage products have benefited from Qxf2’s brand of testing. Whether you are looking to lay the foundation for good testing or trying kickstart specific project or simply wanting to augment your current QA team, Qxf2 can help. Learn more about our various offerings by simply dropping a note.


Leave a Reply

Your email address will not be published. Required fields are marked *