{"id":22699,"date":"2024-09-19T10:29:42","date_gmt":"2024-09-19T14:29:42","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=22699"},"modified":"2024-09-19T10:29:42","modified_gmt":"2024-09-19T14:29:42","slug":"update-lambdatest-test-session-with-test-run-status","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/update-lambdatest-test-session-with-test-run-status\/","title":{"rendered":"Update LambdaTest test session with test run status"},"content":{"rendered":"<p style=\"text-align: justify;\">In this post, <a href=\"https:\/\/qxf2.com\/?utm_source=lambdatest-session-update&#038;utm_medium=click&#038;utm_campaign=From%20blog\">we<\/a> will show you how to update the test run status on <a href=\"https:\/\/www.lambdatest.com\/\" target=\"_blank\" rel=\"noopener\">LambdaTest<\/a>. 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 <a href=\"https:\/\/qxf2.com\/blog\/run-automation-tests-on-lambdatest-using-pytest\" target=\"_blank\" rel=\"noopener\">running test automation on LambdaTest using pytest<\/a>. 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&#8217;s UI as well.<\/p>\n<hr \/>\n<h3>Why do we need to update the LambdaTest session with the test run status?<\/h3>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<hr \/>\n<h3>How to update the test run status on a LambdaTest session?<\/h3>\n<p style=\"text-align: justify;\">There are several ways to update the test run status on LambdaTest, but we will discuss the following two recommended methods:<\/p>\n<ol>\n<li>Using Webdriver<\/li>\n<li>Using LambdaTest REST API<\/li>\n<\/ol>\n<hr \/>\n<h3>Update LambdaTest Session Status Using WebDriver:<\/h3>\n<p style=\"text-align: justify;\">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:<\/p>\n<pre lang=\"python\">driver.execute_script(\"lambda-status=passed\")<\/pre>\n<p>Otherwise, run:<\/p>\n<pre lang=\"python\">driver.execute_script(\"lambda-status=failed\")<\/pre>\n<p>See our updated sample test below, which demonstrates how to report the test run status to LambdaTest.<\/p>\n<p><strong>Note:<\/strong> This is highly simplified code to make this post illustrative. We do not use this quality of code at clients.<\/p>\n<pre lang=\"python\">#NOTE: This is highly simplified code to make this post illustrative\r\n#We do not use this quality of code at clients\r\n#Our framework uses the Page Object pattern\r\n\r\nfrom lambdatest_runner import LambdaTestRunner\r\n \r\n#Contents of test_example_form.py\r\ndef test_example_form(os_name, os_version, browser, browser_version,\r\n                                 remote_project_name, remote_build_name, testname):\r\n    \"Test example form\"\r\n    try:\r\n        #Create an driver object\r\n        runner = LambdaTestRunner()\r\n        driver,session_url = runner.get_lambdatest_webdriver(os_name, os_version, browser, browser_version,\r\n                                            remote_project_name, remote_build_name, testname)\r\n        print(\"\\nRunning test on LambdaTest Platform\")\r\n        print(\"Session url:\",session_url)\r\n        #Create variables to keep count of pass\/fail\r\n        pass_check_counter = 0\r\n        total_checks = 0\r\n        #Visit the tutorial page\r\n        driver.get('http:\/\/qxf2.com\/selenium-tutorial-main') \r\n        print(\"Browser: \",browser)\r\n        print(\"Browser version: \",browser_version)\r\n        print(\"OS: \",os_name)\r\n        print(\"OS version: \",os_version)\r\n        print(\"\\n\")\r\n        print(\"Navigated to Qxf2's Selenium Tutorial page\")\r\n        #Check 1: Is the page title correct?\r\n        total_checks += 1 \r\n        if(driver.title==\"Qxf2 Services: Selenium training main\"):\r\n            print(\"Success: Title of the Qxf2 Tutorial page is correct\")\r\n            pass_check_counter += 1\r\n        else:\r\n            print(\"Failed: Qxf2 Tutorial page Title is incorrect\")\r\n\r\n    except Exception as e:\r\n        print(f\"An error occurred: {e}\")\r\n\r\n    finally:\r\n        # Update the test status on LambdaTest using JavaScript\r\n        status = \"passed\" if total_checks == pass_check_counter else \"failed\"\r\n        driver.execute_script(f\"lambda-status={status}\")\r\n        \r\n        # Quit the browser window\r\n        driver.quit() \r\n        \r\n        # Assert to know test status\r\n        assert total_checks == pass_check_counter \r\n\r\n<\/pre>\n<p style=\"text-align: justify;\">Check the LambdaTest session automation page to verify the test run status update. See the attached screenshot below.<\/p>\n<figure id=\"attachment_22744\" aria-describedby=\"caption-attachment-22744\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-22744\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1-1024x442.png\" alt=\"LambdaTest Tet Run Status Update\" width=\"900\" height=\"388\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1-1024x442.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1-300x129.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1-768x331.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1-1536x663.png 1536w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LambdaTest-Web-Automation-Page-1.png 1838w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-22744\" class=\"wp-caption-text\">LamdaTest Session Status Update using Webdriver<\/figcaption><\/figure>\n<hr \/>\n<h3>Update LambdaTest Session Status Using the REST API:<\/h3>\n<p style=\"text-align: justify;\">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 <code>PATCH<\/code> 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 &#8220;pytest_terminal_summary&#8221; or &#8220;pytest_sessionfinish.&#8221;<\/p>\n<p style=\"text-align: justify;\"><strong>Note:<\/strong> This is highly simplified code to make this post illustrative. We do not use this quality of the code at clients.<\/p>\n<pre lang=\"python\">import requests\r\n\r\ndef update_lambdatest_session_status(session_id, status, username, access_key):\r\n    \"Update the test run status of a LambdaTest session.\"\r\n    # Define the LambdaTest API endpoint URL\r\n    url = f\"https:\/\/api.lambdatest.com\/automation\/api\/v1\/sessions\/{session_id}\"\r\n\r\n    # Define the headers \r\n    headers = {\"Content-Type\": \"application\/json\"}\r\n\r\n    # Define the data payload to update only the \"status_ind\"\r\n    data = {\"status_ind\": status }  # Only update the \"status_ind\"\r\n    \r\n    # Make the PATCH request with authentication\r\n    response = requests.patch(url, json=data, headers=headers, auth=(username, access_key))\r\n\r\n    # Check if the request was successful\r\n    if response.status_code == 200:\r\n        print(f\"Successfully updated session {session_id} status to '{status}'.\")\r\n    else:\r\n        print(f\"Failed to update session status. Status code: {response.status_code}\")\r\n        print(f\"Response: {response.text}\")\r\n\r\n# Example usage\r\nupdate_lambdatest_session_status(\r\n    session_id=\"your_session_id_here\",  # Replace with your session ID\r\n    status=\"test_status\",                    # Replace with desired status: \"passed\",\"failed\" or \"error\"\r\n    username=\"your_username_here\",      # Replace with your LambdaTest username\r\n    access_key=\"your_access_key_here\"   # Replace with your LambdaTest access key\r\n)\r\n<\/pre>\n<p style=\"text-align: justify;\">For demonstration purposes, I updated the same LambdaTest session shown in the previous method using the code above. See the attached screenshot.<\/p>\n<figure id=\"attachment_22746\" aria-describedby=\"caption-attachment-22746\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-22746\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update-1024x444.png\" alt=\"LamdaTest Session Status Update using REST API\" width=\"900\" height=\"390\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update-1024x444.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update-300x130.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update-768x333.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update-1536x666.png 1536w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/LamdaTest-Session-Status-Update.png 1846w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-22746\" class=\"wp-caption-text\">LamdaTest Session Status Update using REST API<\/figcaption><\/figure>\n<hr \/>\n<p style=\"text-align: justify;\">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.<\/p>\n<hr \/>\n<h3>Let Qxf2 kickstart your test automation journey<\/h3>\n<p style=\"text-align: justify;\">Several early stage products have benefited from Qxf2&#8217;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 <a href=\"https:\/\/qxf2.com\/contact?utm_source=lambdatest-session-update&#038;utm_medium=click&#038;utm_campaign=From%20blog\">note<\/a>.<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,429,107],"tags":[],"class_list":["post-22699","post","type-post","status-publish","format-standard","hentry","category-automation","category-lambdatest","category-pytest"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=22699"}],"version-history":[{"count":21,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22699\/revisions"}],"predecessor-version":[{"id":22806,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22699\/revisions\/22806"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=22699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=22699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=22699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}