Guide to Set Up Modern Web Test Automation Framework with Selenium and Python

Nowadays, it has been a dominant trend to deploy big releases on an infrequent basis without sacrificing the quality of the product. With every new deployment that introduces new features, bug fixes need in-depth end to end testing to ensure the success rate of deployment. The small product or projects can be covered up with manual testing but the products or applications that are huge in amount of features definitely require automation testing to provide maximum test coverage in minimum time. Such use cases can be achieved with the use of Selenium with any robust programming language, in this post we will be using Selenium with Python.

Selenium WebDriver is a web framework that permits automation of web based applications on various supported browsers like Chrome, Firefox, Safari, etc. The selenium test automation framework enables you to define step by step interactions with a web application and adding assertions to uncover maximum bugs.

The Selenium web test automation framework can be used with different programming language like Java, Python, Javascript, Ruby, C#, etc. As per the GitHub survey, Python is the developers most demanded programming language. Python language has a smooth learning curve with easy syntax and concise code. Hence, selenium can easily be integrated with Python to automate the web testing process.

Quick Overview Of Selenium WebDriver Architecture

This image gives us a brief of the backend working of Selenium WebDriver along with the other components involved. Let’s quickly have a theoretical overview of these components:

Pros:

  • It is suitable for all types of applications like web application, mobile app, desktop app.
  • It is a Keyword Driven Approach(KDA) that allows us to create test cases in human readable keywords where no coding experience is required.
  • It supports native IF/ELSE syntax from RF v4.0
  • It provides a dedicated community support and multiple resources.

Cons:

  • Customization of reporting is complex.
  • Scope of parallel testing is limited with this framework but it can be achieved with Pabot(a parallel executor for robot framework).

Top Four Python Test Automation Frameworks

  • Provides fixtures that help in covering all the parameter combinations without re-writing the code.
  • Supports multiple range of plugins such as: pytest-randomly, pytest-cov, pytest-django, pytest-bdd.
  • Supports pytest html plugin that generates html report for the automated test suite. With just a single command the report can be generated.
  • Supports parallel execution of test cases using pytest-xdist plugin.

Cons:

With the increase in demand for Python, the popularity of Python test automation frameworks has correspondingly increased. The frameworks play an important role in organising automation projects in terms of managing the order of script execution, storing results, presenting logs and adding assertions. Initially, it might be difficult to select one framework out of the multiple frameworks available but eventually it would benefit a lot in developing a robust test framework. Here we will look closely at the top four test automation frameworks demanded in Python:

Pros:

  • Since it is a part of the default Python package, no other additional installations are required to set up PyUnit.
  • Generates both test reports in a very quick manner i.e. XML reports and unittest-sml-reporting

Cons:

  • Sometimes, there is a huge amount of boilerplate code involved.
  • Since it is based on JUnit, camelCase naming method is used instead of Python snake_case naming convention

1. Robot Framework : Robot framework is an open source automation test framework majorly used for robotic process automation(RPA) and acceptance test driven development(ATDD). It is supported with Python version 2.7.14 or higher.

Pros:

  • It has a full support of Gherkin language for writing test cases in readable languages which further leads to the participation of other non-technical teams as well.
  • It has Django and Flask integrations.

Cons:

  • It is only preferred for black box testing.
  • And doesn’t support parallel testing.

2. Pytest : Pytest is the most used Python automation framework for testing. It is majorly used for unit, functional and API testing. It is supported with Python version 3.5 or higher.

3. PyUnit : It is the default Python testing framework that comes with the Python package and is thus preferred by many developers. It is a unit testing framework that was inspired from JUnit, the most popular java testing framework.

4. Behave: Behave is one of the most popular Python BDD(behavior-driven development) test automation framework. It’s functions are very similar to the Cucumber framework.

Setting up Python with Selenium and Pytest framework

Step 1: Download Python for Windows and install the program. You can skip this step in case Python is already installed in your local machine.

pip -version

Step 2: To install and manage any package in Python, PIP has to be installed in your local machine with Python. PIP is the package management system that can be downloaded from Python official site. Once downloaded, you can install the same by using the below command:

python get-pip.py

Below is the command to confirm whether PIP is properly installed or not:

pytest -version

Step 3: Since we are using the Pytest framework to build our automation test suite, it is important to install Pytest libraries as well. Execute the below command to install pytest:

pip install -U pytest

Below is the command to confirm whether pytest is properly installed or not:

Step 4: For installing Selenium framework with PIP, execute the below command:

pip install -U selenium

To verify the selenium version installed, use the command:

Step 5: To invoke our automated browser, we will not be downloading any browser driver specifically. Instead, we will be using WebDriver-Manager, which is an open source library to automatically manage different browser drivers.We can install the webdriver-manager by running the below command:

  1. Direct to pCloudy home page
  2. Click on Login
  3. Get the page title
  4. Assert the login page title
pip install webdriver-manager

You can verify if Selenium and Webdriver-manager are installed by running the command below:

With this, we come to the end of the installation of all the prerequisites to run selenium based automated tests using python. Now let’s quickly create a short test scenario to write a script upon:

To implement the above test scenario, here is the selenium-python script using pytest framework:

import pytest from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager import sys from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from time import sleep def pcloudy_sample_test(): driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get('https://www.pcloudy.com/') driver.maximize_window() driver.find_element(By.XPATH, '//*[text()="Login"]').click() sleep(2) title = "" Remote Mobile Web & Application Testing on Real Android Devices - pCloudy "" assert title == driver.title driver.close()

Code Walkthrough:

Once the pCloudy homepage is loaded with the use of XPATH locator, we are looking for the Login button to redirect to the login page. Finally, we are asserting the actual title with the expected retrieved title of the home page.

If the assertion passes, the test case would get marked as passed, else it would be marked as failed. At the end of the execution, the running session would get closed.

Running Pytest Framework Test on pCloudy Browser Cloud for Cross Browser Testing

sample_login_test.py

With the Browser Cloud test automation platform, it also offers manual testing of web applications on these cloud hosted machines. In case, if your automated tests fails on a particular environment, you can manually run that test on the same environment on pCloudy by just selecting the same environment from UI.

Let’s have a quick look at the Pytest framework based Selenium script below that would run on pCloudy Browser Cloud to perform cross browser testing:

import pytest from os import environ from selenium import webdriver from selenium.common.exceptions import WebDriverException from selenium.webdriver.remote.remote_connection import RemoteConnection @pytest.fixture(scope = 'function') def driver(): desired_caps = {} browser = { "pCloudy_Username": "ramit.dhamija@gmail.com", "apiKey": "5TfF4UcNRbN3JhucQ", "clientName": "ramit.dhamija@gmail.com", "email": "ramit.dhamija@gmail.com", "os": "Mac", "osVersion": "Catalina", "browserName": "safari", "browserVersions": "14", "pCloudy_EnableVideo": "true", "pCloudy_EnablePerformanceData": "true", "pCloudy_EnableDeviceLogs": "true" } desired_caps.update(browser) selenium_endpoint = "https://prod-browsercloud-in.pcloudy.com/seleniumcloud/wd/hub" executor = RemoteConnection(selenium_endpoint, resolve_ip = False) browser = webdriver.Remote( command_executor = executor, desired_capabilities = desired_caps ) yield browserimport pytest import sys @pytest.mark.usefixtures('driver') class TestLink: def test_title(self, driver): "" " Verify click and title of login page: return :None "" " driver.get('https://www.pcloudy.com/') driver.maximize_window() driver.find_element(By.XPATH, '//*[text()="Login"]').click() sleep(2) title = "" Remote Mobile Web & Application Testing on Real Android Devices - pCloudy "" assert title == driver.title driver.quit()

Command to execute pytest test script:

Note: In the above command, -n is used to specify the number of parallel test executions.

Code Walkthrough:

In our first script, we have used pytest fixtures which are basically functions that run before each test function to which it is applied on. The scope defined in fixtures — ‘function’ is the default value of fixture scope which means fixture will be executed once per test session.

To run our test on pCloudy browser cloud, we have defined our desired capabilities which include pCloudy authentication in the form of username and API key. In desired capabilities, further we have defined our desired environment where we want our tests to run on the operating system as Mac, OS version as Catalina, browser as Safari and browser version as 14.

There are few in-built capabilities of pCloudy that we have used to capture test video recording, performance data and logs.

To run our tests on pCloudy Selenium grid, we have defined the selenium grid URL as selenium_endpoint. The node machines connected to this hub will get auto assigned according to the test environment being used. In the end of the script, we are yielding the browser that stores and updates the desired capabilities of our automated tests.

In the next script, we have used the same selenium script that we have used earlier to assert the title of the pCloudy login page. This automated test would run on the pCloudy browser cloud on the environment : Mac-Catalina, Safari-14.

pCloudy Output:

To view the all the tests executed till date, direct to pCloudy device page and click on Reports-> All Reports:

To view the logs and snapshots of a specific test, direct to pCloudy device page and click on Reports-> All Reports and take a action on particular session name for which logs and screenshots are required:

Originally published at https://www.pcloudy.com.

pCloudy is the most powerful cloud-based App Testing Platform. Brand Marketing @ pCloudy (www.pcloudy.com)