【软件测试】自动化测试框架Pytest + Selenium的使用

Pytest + Selenium 是一种常见的自动化测试框架组合,用于编写和执行 Web 应用程序的自动化测试。Pytest 是一个强大的 Python 测试框架,而 Selenium 是一个用于浏览器自动化的工具,二者结合使用可以高效地进行 Web 应用的功能测试、UI 测试等。

1. 安装 Pytest 和 Selenium

首先,需要安装所需的库。在命令行中使用 pip 安装 Pytest 和 Selenium:

bash 复制代码
pip install pytest selenium

另外,还需要安装一个浏览器驱动(如 ChromeDriver、GeckoDriver 等)来启动浏览器。以 Chrome 浏览器为例,可以在以下网址下载对应版本的 ChromeDriver

2. 配置 WebDriver

Selenium 通过 WebDriver 来控制浏览器,常见的浏览器有 Chrome、Firefox、Edge 等。以下是如何配置 Selenium 使用 Chrome 浏览器的例子:

python 复制代码
from selenium import webdriver

# 设置 ChromeDriver 路径
driver = webdriver.Chrome(executable_path='path_to_chromedriver')

# 打开网页
driver.get("https://www.example.com")

# 关闭浏览器
driver.quit()

3. 编写简单的测试用例

在 Pytest 中,我们可以编写以 test_ 开头的函数作为测试用例。以下是一个简单的 Pytest + Selenium 测试用例示范:

python 复制代码
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

# 设置 WebDriver
@pytest.fixture(scope="function")
def driver():
    driver = webdriver.Chrome(executable_path='path_to_chromedriver')
    yield driver
    driver.quit()

# 测试用例:访问网页并检查标题
def test_title(driver):
    driver.get("https://www.example.com")
    assert "Example Domain" in driver.title

# 测试用例:点击链接并检查跳转
def test_click_link(driver):
    driver.get("https://www.example.com")
    link = driver.find_element(By.LINK_TEXT, "More information...")
    link.click()
    assert "IANA" in driver.title

4. Pytest 配置

在上面的示例中,@pytest.fixture 用于创建一个 WebDriver 实例并在测试执行前进行设置,测试执行完后关闭浏览器。scope="function" 表示每个测试用例都会创建一个新的 WebDriver 实例。如果希望跨多个测试共享 WebDriver,可以将 scope 设置为 "module""session"

5. 使用 Selenium 控制浏览器

Selenium 提供了丰富的 API 来与网页交互。以下是一些常见的操作:

  • 查找元素:
python 复制代码
# 使用 ID 查找元素
element = driver.find_element(By.ID, "element_id")

# 使用类名查找元素
element = driver.find_element(By.CLASS_NAME, "class_name")

# 使用 XPath 查找元素
element = driver.find_element(By.XPATH, "//div[@class='example']")
  • 发送键盘输入:
python 复制代码
# 向输入框发送文本
input_element = driver.find_element(By.NAME, "q")
input_element.send_keys("Selenium")
  • 点击按钮或链接:
python 复制代码
button = driver.find_element(By.ID, "submit_button")
button.click()
  • 获取元素文本:
python 复制代码
text = driver.find_element(By.TAG_NAME, "h1").text
  • 等待元素加载:
python 复制代码
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待某个元素可点击
element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "submit_button"))
)

6. 运行测试

你可以在命令行运行 Pytest 来执行测试:

bash 复制代码
pytest test_example.py

Pytest 会自动发现以 test_ 开头的函数并执行测试。

7. 常见的 Pytest 功能

  • 断言(Assertions) :Pytest 使用 Python 内建的 assert 语句进行测试断言,检查预期结果是否符合实际。
  • 测试报告 :Pytest 会输出测试结果。如果想要生成 HTML 或 JUnit 风格的报告,可以使用 pytest-html 插件:
bash 复制代码
pip install pytest-html
pytest --html=report.html
  • 并行执行 :可以使用 pytest-xdist 插件实现测试的并行执行,提高执行速度。
bash 复制代码
pip install pytest-xdist
pytest -n 4  # 使用 4 个进程并行执行测试

总结

通过 Pytest 和 Selenium 的结合,可以非常方便地编写、组织和执行 Web 自动化测试。Pytest 提供了强大的测试功能和灵活的配置,而 Selenium 则提供了与浏览器交互的能力,二者结合能帮助测试人员快速有效地进行自动化测试。

相关推荐
小白学大数据13 小时前
Scrapy结合Selenium实现搜索点击爬虫的最佳实践
开发语言·chrome·爬虫·selenium·scrapy
码媛14 小时前
A006-基于Selenium和JMeter的吉屋web端的自动化测试设计与实现
selenium·测试工具·jmeter
小鑫仔_x1 天前
selenium之Token
python·selenium·测试工具
小鑫仔_x1 天前
Selenium之Actions事件
selenium·测试工具
第三方软件测评2 天前
软件功能性测试有多重要?功能性测试工具有哪些?
软件测试·功能测试·测试工具
大霸王龙2 天前
Selenium中`driver.get(htmlfile)`方法可能出现的超时问题
selenium·测试工具
北极的冰箱2 天前
自动化运行后BeautifulReport内容为空
运维·python·测试工具·自动化
Respect@2 天前
tcpdump`是一个非常强大的命令行工具,用于在网络上捕获并分析数据包
网络·测试工具·tcpdump
#岩王爷2 天前
自动化测试——selenium
python·selenium·测试工具