Selenium is a popular and widely used testing framework for automating interactions with web applications. It helps testers perform automated tests by mimicking user actions like clicking links, filling out forms, and validating web page content. Automated testing with Selenium is important because it enhances testing efficiency, reduces human errors, and ensures web applications function reliably and as expected. This ultimately improves user experience and user satisfaction.
A TestNG report is a structured document that provides a comprehensive overview of the test results. It includes detailed information about test cases, their execution status, and any failures or errors encountered during testing. TestNG reports are useful because of the following reasons:
They present test results in an organized and easy-to-understand format.
They include stack traces and error messages, which help in identifying the exact cause of test failures. This ultimately accelerates the debugging process.
Their appearance and content can be customized for different stakeholders, such as developers, testers, and managers.
We will be using Python, Selenium, and the pytest framework to generate a TestNG report.
Note: The pytest testing framework is commonly used for automating and running Selenium tests. It provides a structured and efficient way to perform web testing.
First, we will set up a web application. This application will have a simple login form with email and password inputs and a button for submitting the form. We will be testing and generating a TestNG report for this application using Selenium and the pytest framework. The code snippet below shows the web application we will be using.
Once the web application has been set up, we need to write the test script using Python, Selenium, and the pytest framework. We will be using the test script to interact with the web application and test its functionalities. Once the functionalities have been tested, we will be able to generate a TestNG report. The code snippet below shows the test script that we will be using.
from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdef test_email_input_presence(driver):try:driver.get("http://localhost:8000/index.html")email_input = driver.find_element(By.ID, "email")assert email_input.is_displayed()except Exception as e:print(e)def test_password_input_presence(driver):try:driver.get("http://localhost:8000/index.html")password_input = driver.find_element(By.ID, "password")assert password_input.is_displayed()except Exception as e:print(e)def test_submit_button_presence(driver):try:driver.get("http://localhost:8000/index.html")submit_button = driver.find_element(By.XPATH, "//input[@type='button']")assert submit_button.is_displayed()except Exception as e:print(e)def test_successful_login(driver):try:driver.get("http://localhost:8000/index.html")email_input = driver.find_element(By.ID, "email")password_input = driver.find_element(By.ID, "password")submit_button = driver.find_element(By.XPATH, "//input[@type='button']")email_input.send_keys("test@example.com")password_input.send_keys("password")submit_button.click()WebDriverWait(driver, 10).until(EC.alert_is_present())alert = driver.switch_to.alertassert "Logged in!" in alert.textalert.accept()except Exception as e:print(e)def test_empty_fields_submission(driver):try:driver.get("http://localhost:8000/index.html")submit_button = driver.find_element(By.XPATH, "//input[@type='button']")submit_button.click()WebDriverWait(driver, 10).until(EC.alert_is_present())alert = driver.switch_to.alertassert "Both email and password are required!" in alert.textalert.accept()except Exception as e:print(e)
In the code snippet above:
Lines 1–3: We import the required libraries.
Lines 5–11: We define the test_email_input_presence
function that takes the instance of the Selenium WebDriver as the parameter. In this function, we test whether the email input field within the web application is visible or not. We use Selenium to locate the email input field, and if it is visible within the web application, the test passes. Otherwise, the test fails, and we print the relevant error.
Lines 13–19: We define the test_password_input_presence
function that takes the instance of the Selenium WebDriver as the parameter. In this function, we test whether the password input field within the web application is visible or not. We use Selenium to locate the password input field, and if it is visible within the web application, the test passes. Otherwise, the test fails, and we print the relevant error.
Lines 21–27: We define the test_submit_button_presence
function that takes the instance of the Selenium WebDriver as the parameter. In this function, we test whether the “Submit” button within the web application is visible or not. We use Selenium to locate the submit button, and if it is visible within the web application, the test passes. Otherwise, the test fails, and we print the relevant error.
Lines 29–43: We define the test_successful_login
function that takes the instance of the Selenium WebDriver as the parameter. In this function, we test for a successful login. We use Selenium to locate the email and password input fields and the submit button within the web application. Once located, we input test values within the email and password input fields and then click the “Submit” button. The test passes if an alert with the message “Logged in!” is displayed after the “Submit” button has been clicked. Otherwise, the test fails, and we print the relevant error.
Lines 45–55: We define the test_empty_fields_submission
function that takes the instance of the Selenium WebDriver as the parameter. In this function, we test for a scenario where the “Submit” button is clicked while the email and password input fields are empty. We use Selenium to locate the submit button within the web application. Once located, we click the “Submit” button. The test passes if an alert with the message “Both email and password are required!” is displayed after the “Submit” button has been clicked. Otherwise, the test fails, and we print the relevant error.
Lastly, we need to configure pytest. Configuration is important because it allows us to customize test behaviors and define test run options. This ensures that the testing environment aligns with the specific requirements. We will be configuring the following two files:
pytest.ini
: This is a configuration file for pytest that allows us to specify test-related settings, such as custom markers, plugins, or test discovery options.
conftest.py
: This file allows us to define settings that can be shared across multiple test scripts, enhancing code reusability and maintainability.
The code snippet below shows the pytest.ini
and conftest.py
files.
import pytestfrom selenium import webdriverfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.chrome.service import Service as ChromeServicefrom time import sleep@pytest.fixture(scope="session")def driver():options = webdriver.ChromeOptions()options.add_argument("--no-sandbox")chrome_service = ChromeService(ChromeDriverManager().install())driver = webdriver.Chrome(service=chrome_service, options=options)yield driversleep(2)driver.quit()
In the pytest.ini
file within the code snippet above:
Lines 1–2: We disable the cache provider plugin during test execution. This ensures that the test results are not influenced by cached data. Therefore, more accurate and reliable test outcomes will be achieved.
In the conftest.py
file within the code snippet above:
Lines 1–5: We import the required libraries.
Lines 7–15: We configure an instance of the Selenium WebDriver for the Chrome browser. We then set various options for the WebDriver. This instance of the Selenium WebDriver will automatically be passed as a parameter to all the test cases we have defined previously. Once all the test cases have been completed, the WebDriver will exit.
Once all the above steps have been completed, we can generate a TestNG report. Execute the coding playground below by clicking the “Run” button. Once the Chrome browser has been launched within the “Output” tab, click the “+” button to open another terminal tab. Within that terminal tab execute the following command to generate the TestNG report:
cd /usercode && pytest --html=report.html
Once the execution has been successful, a file named report.html
will be generated inside the /usercode
directory. To view the contents of the file, navigate to the following URL within the Chrome browser that is in the “Output” tab: http://localhost:8000/report.html
.
Please type the http://localhost:8000/report.html
URL in the search bar of Chrome browser instead of copying and pasting it to see the results.
import pytest from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from time import sleep @pytest.fixture(scope="session") def driver(): options = webdriver.ChromeOptions() options.add_argument("--no-sandbox") chrome_service = ChromeService(ChromeDriverManager().install()) driver = webdriver.Chrome(service=chrome_service, options=options) yield driver sleep(2) driver.quit()
To sum it up, learning how to make TestNG reports with Selenium can really boost the testing game. When we understand Selenium well, know why reports matter, and follow the steps, we can create super helpful reports that make testing easier and better. Keep testing and enjoy!
Free Resources