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 activity. When you are setting up a test bed for test automation scripts execution, you need to recollect the steps to install PIP on Windows.
Step #1 – Download get-pip.py
Step #2 – Run get-pip.py
Virtual Environments on Windows
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.
Step #1 – Install virtual environment package
pip install virtualenv
Step #2 – Create a virtual environment
python -m venv newenv
Note: In the above command, newenv is the environment name.
Step #3 – Activate the virtual environment. Go to the virtual environment folder on Command Prompt, navigate to ‘scripts’ folder, and run ‘activate’ command.
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.
pip freeze > requirements.txt
Note: Once the command is executed successfully, it creates the requirements.txt with the list of installed packages.
Behave JSON Report Generation
If you are using behave BDD framework and would like to generate JSON report after automated script execution, then kick-off the behave execution using the below command.
behave -f json -o reports.json
Customized PDF Report Generation
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 – Customized Test Automation PDF Report. You can create PDF report as shown below using pdf_reports package & Pug template.
Python Selenium Headless Chrome
Starting Chrome in headless mode using Selenium WebDriver can be done using ChromeOptions. Refer the below Python snippet.
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') driver = webdriver.Chrome(executable_path='chromedriver.exe',chrome_options=options)
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.
from behave import fixture, use_fixture from selenium import webdriver @fixture def launch_browser(context): #Launch Browser context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe') print("=============>Browser is launched") yield context.driver #Clean Up Browser context.driver.quit() print(context.scenario.duration) print("=============>Browser is quit") def before_scenario(context,scenario): the_fixture1 = use_fixture(launch_browser, context)
In Selenium WebDriver, you can set network upload & download throughput and latency to run the automation testing scripts in different network conditions.
driver.set_network_conditions( offline=False, latency=5, # additional latency (ms) download_throughput=500 * 1024, # maximal throughput upload_throughput=500 * 1024) # maximal throughput
Desktop App Automation Testing
Using pywinauto package, you can automate Desktop App Test cases.
from pywinauto.application import Application app = Application().start("notepad.exe")
If you want to manage all CI related operations using Python code, you can try BuildBot CI.
In ConclusionAs an automation testing company, we use Python and Selenium WebDriver for multiple projects. We hope you have gained incredible insights about various test automation tips that’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.