Selenium Unittest
Selenium can be integrated with various testing frameworks, such as unittest and pytest, enabling more structured test code management and test report generation.
* * *
## Integration with `unittest`
`unittest` is a testing framework in the Python standard library that provides rich assertion methods and test organization approaches.
We can integrate Selenium with `unittest` to write structured test cases.
### Basic Structure
Here is a simple example of Selenium integrated with `unittest`:
## 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
* **`setUp` method**: Runs before each test case execution, used to initialize the test environment. Here, we start the browser and open the Google homepage.
* **`test_search` method**: This is a test case used to test the Google search function. We locate the search box, input "Selenium", and then submit the search. Finally, we assert whether the page title contains "Selenium".
* **`tearDown` method**: Runs after each test case execution, used to clean up the test environment. Here, we close the browser.
### Running Tests
You can run the test with the following command:
python -m unittest test_baidu_search.py
* * *
## Integration with `pytest`
`pytest` is another popular Python testing framework that offers a more concise syntax and more powerful features. We can integrate Selenium with `pytest` to write more flexible test cases.
### Basic Structure
Here is a simple example of Selenium integrated with `pytest`:
## 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
* **`browser` fixture**: This is a `pytest` fixture used to start the browser before each test case execution and close the browser after the test case execution.
* **`test_baidu_search` function**: This is a test case used to test the Google search function. We locate the search box, input "Selenium", and then submit the search. Finally, we assert whether the page title contains "Selenium".
### Running Tests
You can run the test with the following command:
pytest test_baidu_search.py
* * *
## Generating Test Reports
In automated testing, generating test reports is very important. We can use the `pytest-html` plugin to generate HTML-formatted test reports.
### Installing `pytest-html`
You can install the `pytest-html` plugin with the following command:
## Example
pip install pytest-html
### Generating Test Reports
When running tests, you can generate an HTML-formatted test report with the following command:
pytest --html=report.html
The generated `report.html` file will contain detailed test results, including passed test cases, failed test cases, and error messages.
## 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 input 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 load (simple wait, explicit waits are recommended in actual 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 the test
if __name__ =="__main__":
test_baidu_search()
YouTip