{"id":22565,"date":"2024-09-09T02:40:28","date_gmt":"2024-09-09T06:40:28","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=22565"},"modified":"2024-09-09T02:40:28","modified_gmt":"2024-09-09T06:40:28","slug":"replayio-with-selenium-python","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/replayio-with-selenium-python\/","title":{"rendered":"Replay.io with Selenium Python"},"content":{"rendered":"<p>At <a href=\"https:\/\/qxf2.com\/?utm_source=replay_with_python&#038;utm_medium=click&#038;utm_campaign=From%20blog\" rel=\"noopener\" target=\"_blank\">Qxf2<\/a>, we recently explored a tool called <a href=\"https:\/\/www.replay.io\/\" rel=\"noopener\" target=\"_blank\">Replay<\/a>, and got a chance to play with some of it&#8217;s features and get a feel of the tool as a whole. For starters Replay.io is a tool designed to help developers or testers record, replay, and debug browser sessions by capturing every interaction and event that occurred in the session. For QA teams, it\u2019s a helpful tool when developers struggle to debug an issue. You can use Replay to record and share the sessions, making it easier for them to troubleshoot.<br \/>\nWhile Replay provides native support for JavaScript based frameworks like <a href=\"https:\/\/www.cypress.io\/\" rel=\"noopener\" target=\"_blank\">Cypress<\/a>, there is very little to no information about how we could use this tool with other programming languages. So, in this blog, we will take you through setting up Replay to run with your Python <a href=\"https:\/\/www.selenium.dev\/\" rel=\"noopener\" target=\"_blank\">Selenium<\/a> tests.<\/p>\n<h4>1. Setting Up Replay.io<\/h4>\n<p><strong>a. Generating the API key<\/strong><br \/>\nBefore you can start using Replay, you&#8217;ll need to sign up for an account on their website. Once you&#8217;ve completed the registration process, navigate to your account settings to generate an API key.<br \/>\n<figure id=\"attachment_22591\" aria-describedby=\"caption-attachment-22591\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key.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\/08\/api_key-1024x226.png\" alt=\"setting API key in replay io\" width=\"900\" height=\"199\" class=\"size-large wp-image-22591\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key-1024x226.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key-300x66.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key-768x169.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key-1536x338.png 1536w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/api_key.png 1906w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-22591\" class=\"wp-caption-text\">Generating API key in replay.io<\/figcaption><\/figure><br \/>\nThis key will be used to authenticate your requests and upload your replays to the Replay.io server.<\/p>\n<p><strong>b. Installing Replay on Ubuntu (WSL)<\/strong><br \/>\nWe first tried running Replay on Windows. However, we were hit with the realization that replay does not support Windows machines, so we had to set it up on <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/wsl\/install\" rel=\"noopener\" target=\"_blank\">WSL<\/a>. Installing Replay is straightforward process. You can follow these steps:<\/p>\n<p>Update your package lists:<\/p>\n<pre lang=\"bash\">\r\nsudo apt-get update\r\n<\/pre>\n<p>Install Replay.io using npm:<\/p>\n<pre lang=\"bash\">\r\nnpm i -g replayio\r\n<\/pre>\n<p>Verify the installation:<\/p>\n<pre lang=\"bash\">\r\nreplayio info\r\n<\/pre>\n<p>It should show you the version of Replay that you are using.<\/p>\n<p><strong>c. Setting the API key.<\/strong><br \/>\nWe can now set the API key that we generated earlier. To do this, create an environment variable <code>REPLAY_APP_API_KEY<\/code> and set the API key as it&#8217;s value<\/p>\n<pre lang=\"bash\">\r\nexport REPLAY_APP_API_KEY=<Your-API-Key>\r\n<\/pre>\n<h4>2. Integrating Replay with Selenium tests in Python<\/h4>\n<p>Now, let&#8217;s take a look at how we can integrate Replay with your Selenium tests.<br \/>\na. For this, we will first write a simple test that navigates to <a href=\"https:\/\/qxf2.com\/selenium-tutorial-main\" rel=\"noopener\" target=\"_blank\">Qxf2&#8217;s selenium tutorial page<\/a> and verifies the title of the page. <strong>(Note: This code is written solely for demonstration purposes for this blog post. Qxf2 does not use this approach in client projects)<\/strong>.<\/p>\n<pre lang=\"python\">\r\n\"\"\"'\r\nThis is a simple test to demonstrate integration of Replay with Selenium\r\n\"\"\"\r\nfrom selenium import webdriver\r\n\r\ndriver = webdriver.Chrome()\r\n\r\ndef test_title_selenium_tutorial():\r\n    \"\"\"\r\n    This is an sample automated test to check if the title of the page is correct\r\n    \"\"\"\r\n    driver.get(\"https:\/\/qxf2.com\/selenium-tutorial-main\")\r\n    expected_title = \"Qxf2 Services: Selenium training main\"\r\n    assert expected_title in driver.title, f\"Expected title: {expected_title}, but got: {driver.title}\"\r\n    driver.quit()\r\n<\/pre>\n<p>b. When you run the test above, it will execute using the default Chrome browser installed on your system. However, our goal is to run it using the Replay browser. To do this, we need to figure out the location of Replay browser&#8217;s binary file. You can find it in the following directory within your Replay installation: <em>.replay\/runtimes\/chrome-linux\/chrome<\/em>. The absolute path would look similar to this: <em>\/home\/ubuntu\/.replay\/runtimes\/chrome-linux\/chrome<\/em>.<\/p>\n<p>c. Next, we need to set this path as the browser&#8217;s binary location in our test. We can do this by setting the Chrome driver options using the Chrome <code>'Options'<\/code> class.<\/p>\n<pre lang=\"python\">\r\n\"\"\"'\r\nThis is a simple test to demonstrate integration of Replay with Selenium\r\n\"\"\"\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.chrome.options import Options\r\n\r\n# Set the Chrome driver options\r\noptions = Options()\r\noptions.binary_location = \"\/home\/ubuntu\/.replay\/runtimes\/chrome-linux\/chrome\"\r\n\r\ndriver = webdriver.Chrome(options=options)\r\n\r\ndef test_title_selenium_tutorial():\r\n    \"\"\"\r\n    This is an sample automated test to check if the title of the page is correct\r\n    \"\"\"\r\n    driver.get(\"https:\/\/qxf2.com\/selenium-tutorial-main\")\r\n    expected_title = \"Qxf2 Services: Selenium training main\"\r\n    assert expected_title in driver.title, f\"Expected title: {expected_title}, but got: {driver.title}\"\r\n    driver.quit()\r\n<\/pre>\n<p>In the above code, we have used <code>options.binary_location<\/code> to set the location of the Chrome binary that selenium uses.<\/p>\n<p>d. Your tests should now run against the Replay browser. However, it wouldn&#8217;t record anything yet.<br \/>\n<figure id=\"attachment_22595\" aria-describedby=\"caption-attachment-22595\" style=\"width: 1841px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_test-ezgif.com-loop-count.gif\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_test-ezgif.com-loop-count.gif\" alt=\"python selenium test running against the replay browser\" width=\"1841\" height=\"938\" class=\"size-full wp-image-22595\" \/><\/a><figcaption id=\"caption-attachment-22595\" class=\"wp-caption-text\">Selenium test running on Replay browser<\/figcaption><\/figure><\/p>\n<h4>3. Key step: Enable recording the browser Session<\/h4>\n<p>By default, the Replay browser does not record your browser session. To enable it, we need to set the environment variable <code>RECORD_ALL_CONTENT<\/code> to <code>1<\/code>.<\/p>\n<pre lang=\"bash\">\r\nexport RECORD_ALL_CONTENT=1\r\n<\/pre>\n<p>Once enabled, run the test again. It should now record your browser session. To view the recordings you can run the following command: <\/p>\n<pre lang=\"bash\">\r\nreplayio list\r\n<\/pre>\n<figure id=\"attachment_22592\" aria-describedby=\"caption-attachment-22592\" style=\"width: 862px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_list.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_list.png\" alt=\"List of recordings in replay\" width=\"862\" height=\"195\" class=\"size-full wp-image-22592\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_list.png 862w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_list-300x68.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_list-768x174.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/a><figcaption id=\"caption-attachment-22592\" class=\"wp-caption-text\">List of recordings<\/figcaption><\/figure>\n<h4>4. Uploading and debugging the replay<\/h4>\n<p>To upload your replay to the Replay.io server, you can run the following command:<\/p>\n<pre lang=\"bash\">\r\nreplayio upload <recording-id>\r\n<\/pre>\n<p>This will upload your replay to the Replay.io server and provide you with a link to the recording. You can then examine and debug the replay as per your requirements.<br \/>\n<figure id=\"attachment_22593\" aria-describedby=\"caption-attachment-22593\" style=\"width: 937px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_upload.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_upload.png\" alt=\"uploading replay to server\" width=\"937\" height=\"150\" class=\"size-full wp-image-22593\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_upload.png 937w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_upload-300x48.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/08\/replay_upload-768x123.png 768w\" sizes=\"auto, (max-width: 937px) 100vw, 937px\" \/><\/a><figcaption id=\"caption-attachment-22593\" class=\"wp-caption-text\">Uploading the replay to the server<\/figcaption><\/figure><\/p>\n<h4>5. When to Use Replay<\/h4>\n<p>Now, let&#8217;s take a look at some scenarios where Replay can improve your testing process:<\/p>\n<p>a. <strong>Complex Debugging:<\/strong> If you&#8217;re dealing with complex issues that are difficult to reproduce, Replay is an excellent tool. It captures all interactions, making it easier to record and reproduce bugs that may not be apparent in a single test run.<\/p>\n<p>b. <strong>Collaboration:<\/strong> Replay allows you to share replays with team members, enabling collaboration and easier communication of issues.<\/p>\n<h4>6. Where to Avoid using Replay<\/h4>\n<p>Lastly, let&#8217;s look at certain scenarios where it would be better to avoid using Replay.<\/p>\n<p><strong>a. Tests with Sensitive Data:<\/strong> If your tests interact with sensitive or personal data, be cautious about using Replay. The tool captures all browser interactions, which could inadvertently include the sensitive information. Ensure that you&#8217;re compliant with your organization&#8217;s data protection policies before using Replay in such scenarios.<\/p>\n<p><strong>b. Browser-Specific Tests:<\/strong> If your tests are designed to verify functionality in a particular browser environment or rely on browser-specific features, it may be better to avoid using Replay. Replay might not accurately represent the unique behaviors or quirks of specific browsers.<\/p>\n<p>We hope this blog has provided valuable insights into setting up Replay.io with Python Selenium.  If you have any questions or need further assistance, feel free to reach out. Happy testing!<\/p>\n<hr>\n<h4>Hire testers from Qxf2<\/h4>\n<p>Qxf2 employs highly technical software testers. We help early stage companies setup their QA function, lay the foundation for good testing, kickstart test automation, augment existing teams and so much more. If you are looking for experienced testers with strong technical capabilities, <a href=\"https:\/\/qxf2.com\/contact\" rel=\"noopener\" target=\"_blank\">reach out to us<\/a>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>At Qxf2, we recently explored a tool called Replay, and got a chance to play with some of it&#8217;s features and get a feel of the tool as a whole. For starters Replay.io is a tool designed to help developers or testers record, replay, and debug browser sessions by capturing every interaction and event that occurred in the session. For [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,18,428,30],"tags":[],"class_list":["post-22565","post","type-post","status-publish","format-standard","hentry","category-debugging","category-python","category-replay-io","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22565","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\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=22565"}],"version-history":[{"count":38,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22565\/revisions"}],"predecessor-version":[{"id":22662,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/22565\/revisions\/22662"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=22565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=22565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=22565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}