Enhanced test automation with Selenium WebDriver and pytest
The biggest challenge for a developer today is to ensure consistent and smooth working of their website or web-application across different devices, browsers, and platforms. The easiest way to ensure all round consistency is using a cross-browser testing method for testing websites or web-applications across contrasting combinations. If you wish to test a web application, there are several frameworks available that can automate the tests performed across different web browsers to ensure consistency, repeatability, and speed. Here, I am going to discuss one such popular framework.
A brief about Selenium WebDriver
Selenium is an open-source tool that automates web application testing across numerous platforms such as Windows, Mac, and Linux. With Selenium, you can perform web testing against some of the popular browsers like Chrome, Firefox, Opera, Microsoft Edge, etc. It also supports various programming languages like Python, C#, Ruby, PERL, Java, etc.
Selenium WebDriver is one of the core components of the Selenium framework. It is a collection of open-source APIs that are used to automate the testing of a web application. It allows test automation to open a browser, then sends clicks, type keys, scrape text, and finally exit the browser cleanly. There are particular drivers for the browsers that include Chrome, Firefox, Opera, Microsoft Edge. Download these drivers first and then place them on the PATH. For a better understanding, check out the image below that depicts the architecture of a Selenium WebDriver:
Let’s see the workflow in the above-mentioned architecture:
- When you run the automation, complete automation code gets converted to JSON.
- Generated JSON is sent to the browser driver through JSON Wire Protocol Over HTTP.
- Browser Driver executes commands on the respective browser over HTTP.
- Browser Driver gets the response from the browser, which is sent back to you.
Talking about the Selenium Drivers, you can download the drivers for your respective browser here. Place the driver on the PATH such as /usr/bin/, /usr/local/bin/ or at the current working directory.
When you think of choosing a framework for test automation you would probably like to have a comprehensive solution that fits any type of tests like unit, functional, end-to-end, acceptance, and others. You would also want this framework to share common data, track how much time a test execution takes, execute prerequisites and tear down steps, and show reporting. pytest meets all these requirements. The pytest framework is one of the best open-source test frameworks that allows you to write test cases using Python.
pytest eases the web application testing and allows you to create simple yet scalable test cases in Selenium WebDriver. It is popular amongst the testers because of its simplicity, scalability, and pythonic nature. You can parallelly run specific tests or subsets of test cases. Test cases are written as functions and test assertion failures are reported with actual values. You can use plugins to add code coverage, pretty reports, and parallel execution. If you create test cases in WebDriver then the pytest supports valuable functionalities like Log Events and Test Report Generation. Features like fixture, parameterize, Xfail, and skip make pytest more powerful. Since the pytest is widely popular amongst the QA community, check out the qualities of pytest and standard practices that should be followed while writing any test case in my previous blog.
Steps to install the pytest and Selenium
There are multiple ways to install pytest with Selenium like using pytest-webdriver…read more.