Selenium Testing Framework Integration

Selenium can be integrated with multiple testing frameworks, such as unittest and pytest, to achieve more structured test code management and test report generation.


andunittestIntegration

unittestis a testing framework in the Python standard library, providing rich assertion methods and test organization approaches.

We can integrate Selenium withunittestto write structured test cases.

Basic Structure

The following is a simple Selenium withunittestintegration example:

Example

import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

class TestGoogleSearch(unittest.TestCase):

    def setUp(self):
        # Set the correct driver path
        service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
        options = webdriver.ChromeOptions()
        driver = webdriver.Chrome(service=service, options=options)
        self.driver.get("https://www.baidu.com")

    def test_search(self):
        search_box = self.driver.find_element(By.NAME, "wd")
        search_box.send_keys("Selenium")
        search_box.submit()
        self.assertIn("Selenium", self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

Code Explanation

  • setUpMethod: Runs before each test case to initialize the test environment. Here, we start the browser and open the Google homepage.
  • test_searchMethod: This is a test case used to test the Google search functionality. We find the search box, enter "Selenium", and submit the search. Finally, we assert whether the page title contains "Selenium".
  • tearDownMethod: Runs after each test case to clean up the test environment. Here, we close the browser.

Run Tests

You can run the tests with the following command:

python -m unittest test_baidu_search.py

andpytestIntegration

pytestis another popular Python testing framework that provides a more concise syntax and more powerful features. We can integrate Selenium withpytestIntegration

Basic Structure

The following is a simple Selenium withpytestintegration example:

Example

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

@pytest.fixture
def browser():
    # Set the correct driver path
    service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)
    yield driver
    driver.quit()

def test_baidu_search(browser):
    browser.get("https://www.baidu.com")
    search_box = browser.find_element(By.NAME, "wd")
    search_box.send_keys("Selenium")
    search_box.submit()
    assert "Selenium" in browser.title

Code Explanation

  • browserFixture: This is apytestfixture, used to start the browser before each test case execution and close the browser after the test case execution.
  • test_baidu_searchFunction: This is a test case used to test the Google search functionality. We find the search box, enter "Selenium", and submit the search. Finally, we assert whether the page title contains "Selenium".

Run Tests

You can run the tests with the following command:

pytest test_baidu_search.py

Generate Test Reports

In automated testing, generating test reports is very important. We can usepytest-htmlplugin to generate test reports in HTML format.

Installationpytest-html

You can install thepytest-htmlplugin:

Example

pip install pytest-html

Generate Test Reports

When running tests, you can generate an HTML-format test report with the following command:

pytest --html=report.html

The generatedreport.htmlfile will contain detailed test results, including passed test cases, failed test cases, and error information.

Test Code

Selenium 4 introduces many new features, such as relative locators, improved window management, better DevTools support, etc.:

Example

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

def test_baidu_search():
    # Set the correct driver path
    service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)

    # Open the Baidu homepage
    driver.get("https://www.baidu.com")

    # Locate the search box and enter the keyword
    search_box = driver.find_element(By.NAME, "wd")  # The name of the Baidu search box is "wd"
    search_box.send_keys("Selenium")

    # Locate the search button and click it
    search_button = driver.find_element(By.ID, "su")  # The id of the Baidu search button is "su"
    search_button.click()

    # Wait for the page to finish loading (simple wait; explicit wait is recommended in real projects)
    driver.implicitly_wait(5)

    # Assert whether the search results page contains the keyword
    assert "Selenium" in driver.page_source

    # Close the browser
    driver.quit()

# Run tests
if __name__ == "__main__":
    test_baidu_search()
Other Extensions