【软件测试】自动化测试框架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 则提供了与浏览器交互的能力,二者结合能帮助测试人员快速有效地进行自动化测试。

相关推荐
DevilSeagull15 小时前
电脑上安装的服务会自动消失? 推荐项目: localhostSCmanager. 更好管理你的服务!
测试工具·安全·react·vite·localhost·hono·trpc
Python大数据分析@20 小时前
浏览器自动化工具 Selenium,Playwright,Puppeteer 做爬虫有哪些弊病?
爬虫·selenium·自动化
弹简特1 天前
【精通Postman接口测试】01-基础理论+安装使用+项目实战+接口关联(万字图文,零基础保姆级)
测试工具·postman·接口关联
武帝为此2 天前
【Selenium 屏幕截图】
python·selenium·测试工具
nbwenren2 天前
2026实测:Gemini 3.1 Pro 从需求文档到 pytest 测试用例一条龙教程
测试用例·pytest
武帝为此2 天前
【Selenium 执行 JavaScript】
javascript·selenium·测试工具
llilian_162 天前
晶体频率测试仪 破解晶振品控核心难题:晶体频率网络测试仪深度解析 晶体网络分析仪
网络·功能测试·单片机·嵌入式硬件·测试工具·51单片机
深念Y2 天前
从 Playwright/Selenium 到指纹浏览器:浏览器自动化技术的进阶之路
selenium·测试工具·自动化·浏览器·账号·无头浏览器·指纹浏览器
Johnstons2 天前
Wireshark ExpertInfo是什么?一文讲透异常分级、适用场景、和传统抓包阅读的区别与排查标准
网络·测试工具·wireshark·es
我的xiaodoujiao2 天前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest