{"id":22621,"date":"2024-09-18T08:27:40","date_gmt":"2024-09-18T12:27:40","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=22621"},"modified":"2024-09-18T08:27:40","modified_gmt":"2024-09-18T12:27:40","slug":"upload-test-terminal-logs-to-browserstack","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/upload-test-terminal-logs-to-browserstack\/","title":{"rendered":"Upload test terminal logs to BrowserStack"},"content":{"rendered":"<p><a href=\"https:\/\/qxf2.com\/?utm_source=browserstack-terminal-logs&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2<\/a> engineers have used <a href=\"https:\/\/www.browserstack.com\/\" rel=\"noopener\" target=\"_blank\">BrowserStack<\/a> for many years. We added it to our <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\" rel=\"noopener\" target=\"_blank\">testing framework<\/a> too. When we check test failures, we use the video from BrowserStack. But seeing the log messages from our framework also helps. Developers find it easier to fix problems when they can see the logs. We noticed that adding this feature made developers more likely to help find and report issues.<\/p>\n<p>In this post, we will show you how to upload your logs to BrowserStack. We noticed there aren&#8217;t many guides for this.<\/p>\n<p><strong>Assumption:<\/strong> Your tests must create a log file.<\/p>\n<hr>\n<h3>How to upload the test terminal logs to BrowserStack Session: <\/h3>\n<p>To upload test terminal logs to <a href=\"https:\/\/www.browserstack.com\/\" rel=\"noopener\" target=\"_blank\">BrowserStack<\/a>, you need to follow the below steps.<\/p>\n<ul>\n<li>Get BrowserStack session details<\/li>\n<li>Choose the correct BrowserStack Cloud API endpoint<\/li>\n<li>Upload the test logs to BrowserStack test session<\/li>\n<\/ul>\n<hr>\n<h3>Get BrowserStack session details:<\/h3>\n<p>There are many ways to get the BrowserStack session details, but a robust and easy way to get the session details is via driver. Use the following line of code to get BrowserStack session details and grab the session ID which we need in the upcoming steps.<\/p>\n<pre lang=\"python\">\r\ncurrent_session = driver.execute_script('browserstack_executor: {\"action\": \"getSessionDetails\"}')\r\nsession_details = json.loads(current_session)\r\nsession_id = session_details['hashed_id']\r\n<\/pre>\n<p>Above line of code runs the provided script to obtain the session details. This method is effective even when running tests in parallel, which is why we incorporate it into our framework to retrieve session details.<\/p>\n<hr>\n<h3>Choose the correct BrowserStack Cloud API endpoint:<\/h3>\n<p>BrowserStack has different API endpoints for web apps and mobile apps to upload terminal logs. So we need to select the correct BrowserStack Cloud API endpoint based on the test type. Whether your test is a mobile test or a web app\/website test?<\/p>\n<p>For Web app automation tests, use the endpoint:<\/p>\n<pre lang=\"python\">url = https:\/\/api-cloud.browserstack.com\/automate\/sessions\/{session_id}\/terminallogs <\/pre>\n<p>For Mobile app automation tests, use the endpoint:<\/p>\n<pre lang=\"python\">url = https:\/\/api-cloud.browserstack.com\/app-automate\/sessions\/{session_id}\/terminallogs\r\n<\/pre>\n<p>If you select the wrong endpoint, you end up with the session_id does not exist error.<\/p>\n<hr>\n<h3>Upload the test logs to the BrowserStack test session:<\/h3>\n<p>To upload the test logs to the respective BrowserStack test session, we use the following method. It is important to call this method at the end of the test after closing the browser. To execute the following method, we need the current session_id which you get from the previous step, your test file path and we need to know which test we are running. Look at our method below:<\/p>\n<p><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\">\r\nimport request\r\n\r\nbrowserstack_cloud_api_server_url = \"https:\/\/api-cloud.browserstack.com\"\r\n \r\ndef upload_terminal_logs(file_path, session_id, appium_test = False, timeout=30):\r\n    \"Upload the terminal log to BrowserStack\"\r\n    try:\r\n        # Determine the URL based on the type of test\r\n        if appium_test:\r\n            url = f'{browserstack_cloud_api_server_url}\/app-automate\/sessions\/{session_id}\/terminallogs'\r\n        else:\r\n            url = f'{browserstack_cloud_api_server_url}\/automate\/sessions\/{session_id}\/terminallogs'\r\n\r\n        # Open the file using a context manager to ensure it is properly closed\r\n        with open(file_path, 'rb') as file:\r\n            files = {'file': file}\r\n            # Make the POST request to upload the file\r\n            response = requests.post(url, auth=self.auth, files=files, timeout=timeout)\r\n\r\n        # Check if the request was successful\r\n        if response.status_code == 200:\r\n            print(\"Log file uploaded to BrowserStack session successfully.\")\r\n        else:\r\n            print(f\"Failed to upload log file. Status code: {response.status_code}\")\r\n            print(response.text)\r\n\r\n        return response\r\n\r\n    except FileNotFoundError as e:\r\n        print(f\"Error: Log file '{file_path}' not found.\")\r\n        return {\"error\": \"Log file not found.\", \"details\": str(e)}\r\n\r\n    except requests.exceptions.RequestException as e:\r\n        # Handle network-related errors\r\n        print(f\"Error: Failed to upload log file to BrowserStack. Network error: {str(e)}\")\r\n        return {\"error\": \"Network error during file upload.\", \"details\": str(e)}\r\n\r\n    except Exception as e:\r\n        # Catch any other unexpected errors\r\n        print(f\"An unexpected error occurred: {str(e)}\")\r\n        return {\"error\": \"Unexpected error occurred during file upload.\", \"details\": str(e)}\r\n<\/pre>\n<p>If you provided the correct test log file path, session_id and test type (appium_test = False\/True), the above method executes and sends the test logs to BrowserStack without fail. And you notice, the message \u201cLog file uploaded to BrowserStack session successfully.\u201d on terminal logs.<\/p>\n<p>You can verify the test logs uploaded to BrowserStack by navigating to the BrowserStack session. Once you land on the correct BrowserStack session, Click on the \u201cOthers\u201d session and then click on the \u201cTerminal Logs\u201d panel. Now you will see logs uploaded to the session. Look at the attached screenshot below.<br \/>\n<figure id=\"attachment_22660\" aria-describedby=\"caption-attachment-22660\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs-1024x624.png\" alt=\"BrowserStack Terminal Logs\" width=\"900\" height=\"548\" class=\"size-large wp-image-22660\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs-1024x624.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs-300x183.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs-768x468.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs-1536x936.png 1536w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/09\/terminal-logs.png 1604w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-22660\" class=\"wp-caption-text\">BrowserStack Terminal Logs<\/figcaption><\/figure><\/p>\n<hr>\n<p>Hope our post helps you to learn and upload your tests terminal logs to BrowserStack. <\/p>\n<hr>\n<h3>Hire Qxf2 for your testing needs<\/h3>\n<p>Want to start your test automation? Qxf2 offers custom services to help teams reduce their test automation backlog. We provide innovative <a href=\"https:\/\/qxf2.com\/qait\">UI test automation<\/a> and <a href=\"https:\/\/qxf2.com\/api-tests-autogenerate\">API test automation<\/a> solutions. Interested? Send us a <a href=\"https:\/\/qxf2.com\/contact?utm_source=browserstack-terminal-logs&#038;utm_medium=click&#038;utm_campaign=From%20blog\">message<\/a>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>Qxf2 engineers have used BrowserStack for many years. We added it to our testing framework too. When we check test failures, we use the video from BrowserStack. But seeing the log messages from our framework also helps. Developers find it easier to fix problems when they can see the logs. We noticed that adding this feature made developers more likely [&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,40],"tags":[],"class_list":["post-22621","post","type-post","status-publish","format-standard","hentry","category-automation","category-browserstack"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22621","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=22621"}],"version-history":[{"count":27,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22621\/revisions"}],"predecessor-version":[{"id":22800,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22621\/revisions\/22800"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=22621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=22621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=22621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}