Python驱动的端到端测试框架SeleniumBase

由Python驱动的端到端测试框架SeleniumBase

SeleniumBase详尽解析与使用指南

何为SeleniumBase?

SeleniumBase是一款基于Python的端到端测试框架,它构建在Selenium和pytest之上,为Web自动化测试以及爬虫开发提供了更为简便且高效的体验。它对Selenium的诸多复杂操作进行了简化,还新增了众多实用功能。

SeleniumBase的核心特性

  1. 语法简化:相较于原生Selenium,拥有更为简洁的API
  2. 内置等待机制:自动对元素加载进行等待处理
  3. 多样断言方法:提供多种验证方式
  4. 可视化测试:支持实时查看浏览器操作
  5. 截图与日志:自动记录测试过程
  6. 多浏览器支持:涵盖Chrome、Firefox、Edge、Safari等浏览器
  7. 无头模式:可进行无头浏览器测试
  8. 移动设备模拟:能模拟移动设备进行测试
  9. 代理支持:便于使用代理服务器
  10. 与pytest集成:充分发挥pytest的强大功能

安装SeleniumBase

pip install seleniumbase

基础使用方法

1. 简易测试示例

from seleniumbase import BaseCase

class MyTestClass(BaseCase):

    def test_basic(self):
        self.open("https://www.example.com")
        self.assert_title("Example Domain")
        self.assert_element("div h1")
        self.type("input[name='q']", "SeleniumBase\n")
        self.assert_text("Results for", "h3")

2. 元素定位与操作

SeleniumBase提供了多种元素定位和操作手段:

def test_element_operations(self):
    self.open("https://www.example.com")

    # 点击元素
    self.click("button#submit")

    # 输入文本
    self.type("input#username", "testuser")

    # 清空输入框
    self.clear("input#username")

    # 获取元素文本
    text = self.get_text("h1")

    # 获取元素属性
    attr = self.get_attribute("img#logo", "src")

    # 检查元素是否存在
    self.assert_element("div.container")

    # 检查元素是否可见
    self.assert_element_visible("div.message")

3. 断言方法

SeleniumBase具备丰富的断言方法:

def test_assertions(self):
    self.open("https://www.example.com")

    # 标题断言
    self.assert_title("Example Domain")
    self.assert_title_contains("Example")

    # 文本断言
    self.assert_text("Example Domain", "h1")
    self.assert_exact_text("Example Domain", "h1")

    # URL断言
    self.assert_url("https://www.example.com/")
    self.assert_url_contains("example.com")

    # 元素断言
    self.assert_element("div h1")
    self.assert_element_present("div h1")
    self.assert_element_absent("div.nonexistent")

    # 其他断言
    self.assert_true(1 + 1 == 2)
    self.assert_false(1 + 1 == 3)

4. 等待机制

SeleniumBase能自动处理大部分等待场景,也提供显式等待方法:

def test_waiting(self):
    self.open("https://www.example.com")

    # 等待元素出现
    self.wait_for_element("div.loading")

    # 等待元素可点击
    self.wait_for_element_clickable("button.submit")

    # 等待文本出现
    self.wait_for_text("Welcome back", "h2")

    # 自定义等待时间
    self.wait_for_element("div.result", timeout=20)

    # 等待元素消失
    self.wait_for_element_absent("div.loading")

高级功能

1. 无头模式测试

def test_headless(self):
    self.open("https://www.example.com")
    # 此处为断言相关代码...

# 运行时通过--headless参数启用无头模式
# pytest test_file.py --headless

2. 截图与日志

def test_screenshot(self):
    self.open("https://www.example.com")
    self.save_screenshot("example.png")
    self.save_screenshot_to_logs()  # 将截图保存至日志目录

3. 移动设备模拟

def test_mobile_emulation(self):
    mobile_emulation = {
        "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
        "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
    }
    self.open_with_options("https://www.example.com", mobile_emulation=mobile_emulation)

4. 使用代理

def test_with_proxy(self):
    proxy_string = "127.0.0.1:8080"
    self.open_with_proxy("https://www.example.com", proxy_string)

5. iframe操作

def test_iframe(self):
    self.open("https://www.example.com")
    self.switch_to_frame("iframe#content")
    # 在iframe内进行元素操作
    self.click("button.submit")
    self.switch_to_default_content()  # 切换回主文档

测试运行选项

SeleniumBase测试可通过pytest运行,支持多种参数:

# 基础运行
pytest test_file.py

# 无头模式
pytest test_file.py --headless

# 指定浏览器
pytest test_file.py --browser=firefox

# 慢动作模式(便于观察)
pytest test_file.py --demo_mode

# 保存失败测试的截图
pytest test_file.py --screenshot_on_failure

# 并行测试
pytest test_file.py -n 4

与CI/CD集成

SeleniumBase测试可轻松集成至CI/CD流程。例如,GitHub Actions中的配置示例:

name: SeleniumBase Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.8'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install seleniumbase
    - name: Run tests
      run: |
        pytest tests/ --headless --browser=chrome

最佳实践

  1. 采用Page Object模式:将页面元素与操作封装为类
  2. 合理运用等待:优先使用SeleniumBase的自动等待
  3. 测试命名清晰:使测试目的明确
  4. 恰当断言:每个测试验证一个明确功能点
  5. 维护测试数据:借助外部文件或数据库管理测试数据
  6. 定期维护测试:随应用更新调整测试用例
  7. 利用钩子和固件:使用pytest的固件功能减少重复代码

总结

SeleniumBase是一款功能强大且易用的测试框架,它简化了Selenium的复杂性,同时具备丰富功能。无论是简单的网站测试还是复杂的Web应用程序测试,SeleniumBase都能提供高效解决方案。结合pytest的强大功能,它可满足从简单到复杂的各类测试需求。

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12737.html

(0)
LomuLomu
上一篇 2025 年 7 月 4 日
下一篇 2025 年 7 月 4 日

相关推荐

  • 全网首发2025goland激活码免费申领,权威破解教程

    免责声明:以下教程中的 GoLand 破解补丁与激活码均源自互联网公开资源,仅供个人学习研究,禁止商业用途。若条件允许,请支持正版! 先放一张“战果图”:GoLand 2025.2.1 已顺利激活到 2099 年,爽歪歪! 下面用图文方式手把手带你搞定最新版 GoLand 的激活流程。 前期准备 提示:若你之前尝试过其他补丁却失败,建议彻底卸载旧版并重新安装…

    2025 年 10 月 21 日
    21700
  • 永久pycharm激活码激活pycharm破解全过程

    免责声明:本文所涉及的 PyCharm 破解补丁与激活码均来自互联网公开渠道,仅供个人学习与研究,禁止商业用途。若条件允许,请支持正版! 先放一张 PyCharm 2025.2.1 成功激活到 2099 年的截图,爽到飞起! 下面用图文手把手教你搞定最新版 PyCharm 的激活流程。 嫌折腾?官方正版全家桶低至 32 元/年,登录即用:https://pa…

    PyCharm激活码 2025 年 11 月 14 日
    4700
  • MySQL for update skip locked 与 for update nowait

    理论(下方有实操) for update skip locked 官方文档:https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html#innodb-locking-reads-for-update 语法:select语句后跟 for update skip locked 作用:目标对象…

    未分类 2024 年 12 月 31 日
    57100
  • IDEA激活码获取方式及IDEA破解全流程

    本教程适用于 IDEA、PyCharm、DataGrip、GoLand 等 JetBrains 全家桶,任何版本、任何系统均可照做。 先放一张最新版 IDEA 成功激活到 2099 年的截图,稳! 下面用图文方式手把手演示如何把 IDEA 直接激活到 2099 年,旧版本同样适用,Windows / macOS / Linux 全覆盖。 1. 获取 IDEA…

    IDEA破解教程 2025 年 11 月 30 日
    2200
  • GoLand激活流程适配JetBrains Toolbox?

    申明:本教程GoLand 破解补丁、激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除。若条件允许,希望大家购买正版 ! 废话不多说,先上 GoLand2025.2.1 版本破解成功的截图,如下图,可以看到已经成功破解到 2099 年辣,舒服的很! 接下来就给大家通过图文的方式分享一下如何破解最新的GoLand。 准备工作 注意:如果你…

    2025 年 9 月 29 日
    12900

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信