{"id":2817,"date":"2020-04-19T16:57:26","date_gmt":"2020-04-19T16:57:26","guid":{"rendered":"https:\/\/codoid.com\/useful-python-test-automation-tips\/"},"modified":"2022-02-04T18:32:39","modified_gmt":"2022-02-04T18:32:39","slug":"useful-python-test-automation-tips","status":"publish","type":"post","link":"https:\/\/codoid.com\/automation-testing\/useful-python-test-automation-tips\/","title":{"rendered":"10 Most Useful Python Test Automation Tips"},"content":{"rendered":"<p>As a <a href=\"https:\/\/codoid.com\/software-testing-company\/\">software testing services<\/a> company, we encounter many test automation challenges. In this blog article, we would like to list a few useful Python Test Automation Tips, With the hope that you will bookmark this article for future references.<\/p>\n<h4>Installing PIP on Windows<\/h4>\n<p>Installing PIP on Windows is a simple task and an One-time activity. When you are setting up a test bed for <b>test automation scripts<\/b> execution, you need to recollect the steps to install PIP on Windows.<\/p>\n<p>Step #1 &#8211; Download <a href=\"https:\/\/bootstrap.pypa.io\/get-pip.py\" target=\"_blank\" rel=\"noopener noreferrer\">get-pip.py<\/a><\/p>\n<p>Step #2 &#8211; Run get-pip.py<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npython get-pip.py\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>Virtual Environments on Windows<\/h4>\n<p>Installing, creating and activating a Python virtual environment is pretty straight forward. On the contrary, when you play around with multiple OS, you must be aware of the fact that there are different steps involved to activate a virtual environment on Windows.<\/p>\n<p><b>Step #1<\/b> &#8211; Install virtual environment package<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npip install virtualenv\r\n<\/pre>\n<\/div>\n<p><b>Step #2<\/b> &#8211; Create a virtual environment<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npython -m venv newenv\r\n<\/pre>\n<\/div>\n<blockquote><p>Note: In the above command, <b>newenv<\/b> is the environment name.<\/p><\/blockquote>\n<p><b>Step #3<\/b> &#8211; Activate the virtual environment. Go to the virtual environment folder on Command Prompt, navigate to &#8216;scripts&#8217; folder, and run &#8216;activate&#8217; command.<\/p>\n<p><\/p>\n<h4>Creating requirements.txt<\/h4>\n<p>Managing the required Python packages in requirements.txt is a best practice. If you have already installed the required packages and want to create requirements.txt, then run the below command.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npip freeze &gt; requirements.txt\r\n<\/pre>\n<\/div>\n<blockquote><p>Note: Once the command is executed successfully, it creates the requirements.txt with the list of installed packages.<\/p><\/blockquote>\n<p><\/p>\n<h4>Behave JSON Report Generation<\/h4>\n<p>If you are using <a href=\"https:\/\/codoid.com\/python-behave-bdd-framework-overview\/\">behave BDD framework<\/a> and would like to generate JSON report after automated script execution, then kick-off the behave execution using the below command.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nbehave -f json -o reports.json\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>Customized PDF Report Generation<\/h4>\n<p>Publishing readable and useful report is one of the important test automation success factors. You can create customized PDF report using behave JSON report. Refer the following URL for more details &#8211; <a href=\"https:\/\/codoid.com\/customized-test-automation-pdf-report\/\">Customized Test Automation PDF Report<\/a>. You can create PDF report as shown below using pdf_reports package &#038; Pug template.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/Customized-Test-Automation-PDF-Report.png\" class=\"img-fluid\" alt=\"Customized Test Automation PDF Report\"\/><\/p>\n<h4>Python Selenium Headless Chrome<\/h4>\n<p>Starting Chrome in headless mode using Selenium WebDriver can be done using <b>ChromeOptions<\/b>. Refer the below Python snippet.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom selenium import webdriver\r\noptions = webdriver.ChromeOptions()\r\noptions.add_argument('headless')\r\ndriver = webdriver.Chrome(executable_path='chromedriver.exe',chrome_options=options)\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>Behave Teardown<\/h4>\n<p>In most of the BDD frameworks, you need to write setup and tear-down in two different methods. However, in behave framework, you can write both in one method. Refer the below snippet.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom behave import fixture, use_fixture\r\nfrom selenium import webdriver\r\n\r\n@fixture\r\ndef launch_browser(context):\r\n    #Launch Browser\r\n    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')\r\n    print(&quot;=============&gt;Browser is launched&quot;)\r\n    yield context.driver\r\n\r\n    #Clean Up Browser\r\n    context.driver.quit()\r\n    print(context.scenario.duration)\r\n    print(&quot;=============&gt;Browser is quit&quot;)\r\n\r\ndef before_scenario(context,scenario):\r\n    the_fixture1 = use_fixture(launch_browser, context)\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>Network Throttling<\/h4>\n<p>In <b>Selenium WebDriver<\/b>, you can set network upload &#038; download throughput and latency to run the automation testing scripts in different network conditions.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndriver.set_network_conditions(\r\n        offline=False,\r\n        latency=5,  # additional latency (ms)\r\n        download_throughput=500 * 1024,  # maximal throughput\r\n        upload_throughput=500 * 1024)  # maximal throughput\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>Desktop App Automation Testing<\/h4>\n<p>Using pywinauto package, you can automate Desktop App Test cases.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pywinauto.application import Application\r\napp = Application().start(&quot;notepad.exe&quot;)\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h4>BuildBot CI<\/h4>\n<p>If you want to manage all CI related operations using Python code, you can try <a href=\"https:\/\/codoid.com\/selenium-scripts-using-buildbot-ci\">BuildBot CI<\/a>.<\/p>\n<p><strong>In Conclusion<\/strong>As an <a href=\"https:\/\/codoid.com\/automation-testing-services\/\">automation testing company<\/a>, we use Python and Selenium WebDriver for multiple projects. We hope you  have gained incredible insights about various test automation tips that&#8217;s has been shared in this blog article, As a continuing trend we will be keep sharing more and more useful automation testing related articles in the coming weeks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a software testing services company, we encounter many test automation challenges. In this blog article, we would like to list a few useful Python Test Automation Tips, With the hope that you will bookmark this article for future references. Installing PIP on Windows Installing PIP on Windows is a simple task and an One-time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5071,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[182,179,20],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.3 (Yoast SEO v20.3) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>10 Most Useful Python Test Automation Tips - Codoid<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"10 Most Useful Python Test Automation Tips\" \/>\n<meta property=\"og:description\" content=\"As a software testing services company, we encounter many test automation challenges. In this blog article, we would like to list a few useful Python Test Automation Tips, With the hope that you will bookmark this article for future references. Installing PIP on Windows Installing PIP on Windows is a simple task and an One-time [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/\" \/>\n<meta property=\"og:site_name\" content=\"Codoid\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codoid.softwaretestingcompany\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-19T16:57:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-04T18:32:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/Python_Test_Automation_Tips.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codoid\" \/>\n<meta name=\"twitter:site\" content=\"@codoid\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"10 Most Useful Python Test Automation Tips - Codoid","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"10 Most Useful Python Test Automation Tips","og_description":"As a software testing services company, we encounter many test automation challenges. In this blog article, we would like to list a few useful Python Test Automation Tips, With the hope that you will bookmark this article for future references. Installing PIP on Windows Installing PIP on Windows is a simple task and an One-time [&hellip;]","og_url":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/","og_site_name":"Codoid","article_publisher":"https:\/\/www.facebook.com\/codoid.softwaretestingcompany","article_published_time":"2020-04-19T16:57:26+00:00","article_modified_time":"2022-02-04T18:32:39+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/Python_Test_Automation_Tips.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@codoid","twitter_site":"@codoid","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/#article","isPartOf":{"@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/"},"author":{"name":"admin","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586"},"headline":"10 Most Useful Python Test Automation Tips","datePublished":"2020-04-19T16:57:26+00:00","dateModified":"2022-02-04T18:32:39+00:00","mainEntityOfPage":{"@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/"},"wordCount":666,"commentCount":0,"publisher":{"@id":"https:\/\/codoid.com\/#organization"},"articleSection":["Automation Testing","Fixed","Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/","url":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/","name":"10 Most Useful Python Test Automation Tips - Codoid","isPartOf":{"@id":"https:\/\/codoid.com\/#website"},"datePublished":"2020-04-19T16:57:26+00:00","dateModified":"2022-02-04T18:32:39+00:00","breadcrumb":{"@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codoid.com\/learn\/automation-testing\/useful-python-test-automation-tips\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codoid.com\/"},{"@type":"ListItem","position":2,"name":"10 Most Useful Python Test Automation Tips"}]},{"@type":"WebSite","@id":"https:\/\/codoid.com\/#website","url":"https:\/\/codoid.com\/","name":"Codoid","description":"","publisher":{"@id":"https:\/\/codoid.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codoid.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codoid.com\/#organization","name":"Codoid - Software Testing Company","url":"https:\/\/codoid.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/","url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","contentUrl":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","width":500,"height":500,"caption":"Codoid - Software Testing Company"},"image":{"@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codoid.softwaretestingcompany","https:\/\/twitter.com\/codoid","https:\/\/www.instagram.com\/codoid.softwaretestingcompany\/","https:\/\/www.linkedin.com\/company\/codoid-qacompany","https:\/\/www.pinterest.com\/codoid9282\/codoid-software-testing-company\/","https:\/\/www.youtube.com\/channel\/UCCmOTDQgcf4W8oo22mBMUYA"]},{"@type":"Person","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/codoid.com"]}]}},"_links":{"self":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2817"}],"collection":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/comments?post=2817"}],"version-history":[{"count":4,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2817\/revisions"}],"predecessor-version":[{"id":11740,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2817\/revisions\/11740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media\/5071"}],"wp:attachment":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media?parent=2817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/categories?post=2817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/tags?post=2817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}