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

相关推荐
not coder5 小时前
pytest 常用命令参数
pytest
Feng.Lee13 小时前
聊一聊接口测试中缓存处理策略
功能测试·测试工具·缓存
HAPPY酷2 天前
selenium基础
selenium·测试工具
逾非时2 天前
python:selenium爬取网站信息
开发语言·python·selenium
天才测试猿2 天前
Selenium操作指南(全)
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
白嫖不白嫖2 天前
Selenium基础操作方法详解
selenium·测试工具
哈哈哈哈哈哈哈哈哈...........2 天前
【软件】在 macOS 上安装 Postman
测试工具·macos·postman
mizuhokaga2 天前
Postman 发送 SOAP 请求步骤 归档
测试工具·postman
测试19982 天前
接口自动化测试用例的编写方法
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
代码的乐趣3 天前
支持selenium的chrome driver更新到136.0.7103.113
chrome·python·selenium