Test automation using pytest with Selenium WebDriver

|
| By Webner

Why Pytest

When you think of selecting a framework for test automation you would probably like to have an absolute solution that fits any type of test like unit, functional, end-to-end, acceptance, and others. You would also wish this framework to share the common data, track total test execution time, execute prerequisites and tear down steps, and show all the test reports. pytest fulfills all these requirements. The pytest framework is one of the perfect open-source test frameworks that give you the to write test cases using Python.

Pytest eases web application testing and gives you to make easy yet scalable test cases in Selenium WebDriver. It is popular among testers because of its clarity, scalability, and pythonic nature. You can run parallel your particular tests or subsets of test cases. Test cases are written as functions and test assertion failures are reported with real values. You can use plugins to add code coverage, reports, and parallel execution. If you build test cases in WebDriver then the pytest supports worthy functionalities like Log Events and HTML Test Report Generation. Since the pytest is universally popular among the Tester community.

Easy steps to install the pytest and Selenium

pytest

How pytest pick out the test files and test process
Executing the pytest command (without declaring a filename) on the terminal will run all the Python files that have filenames starting with test_* or ending with *_test. All these files will be picked up automatically as test files through the pytest framework.
These are required conditions as pytest has features create-in into the framework that supports the auto-discovery of test modules and test methods.

The first test:
from lib2to3.pgen2 import driver
import time
from selenium import webdriver
import pytest
def test_login():
driver = webdriver.Chrome(executable_path="C:/Users/ASUS/selenium/chromedriver_win32 (3)/chromedriver.exe")
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('https://al3viewer.winsurtech.com/')
#sign page xpath
driver.find_element_by_xpath("//*[@id='navbarSupportedContent']/ul/li[5]/a").click()
driver.find_element_by_id("id_login").send_keys("mahesh.kumar2@w*****s.com")
driver.find_element_by_id("id_password").send_keys("W*****7")
driver.find_element_by_xpath("//*[@id='signin_form']/div[2]/button").click()
x = driver.title
assert x == "Free Online AL3 Viewer, Download AL3 Data in JSON Format"
def test_teardown():
driver.close()
driver.quit()
print("Test complete")
test_login()
test_teardown()

Now, run the following test and to get the result

Selenium WebDriver

To make an HTML report for Selenium test cases, have to install a plugin with the command: pip install pytest-html. To make the report, we have to shift from the current directory to the directory of the Pytest file that we want to execute. Run the command: pytest –html=report.html.
The report will view like this
pytest2

Leave a Reply

Your email address will not be published. Required fields are marked *