Web测试示例

场景分析

Web测试是一种 有状态测试 , 步骤之间往往有着巨大的依赖性和影响里,所以一个Web测试通常包含大量的测试步骤;

再加上Web测试通常使用Selenium或Playwright进行浏览器控制, 由于浏览器功能繁多,无法像requests那样的 统一传参接口 , 所以在执行测试步骤时,需要根据实时状态调用不同的方法、传递不同的参数;

此外,对浏览器进行自动化操作之后,其结果并不会进行返回,而是作用在Web页面的上, 所以不能直接对操作进行断言,而是需要再一次主动 从页面中获取结果 ,然后再进行断言,

由此可见,Web自动化测试的显著特点有:

  • 步骤数量多

  • 参数变化大

  • 断言不直接

备注

在实际项目中通常会对Selenium或Playwright进行二次封装, 以减少步骤和参数,从而降低一定难度。 但是受限于Web项目自身的特点,封装后的效率和难度依旧无法和API测试相比。

安装依赖

下面以Selenium为例

pip install selenium

Selenium 现在既可以自动下载驱动,又能够进行浏览器控制,所以只需要安装它一个即可。

设计用例

鉴于web测试的复杂度,可以先用python写出测试用例,然后再改写为xlsx。

# test_web.py

from selenium import webdriver


def test_web():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)

    driver.get('https://www.baidu.com')
    driver.find_element('xpath','//*[@id="kw"]').send_keys("大红灯笼高高挂")
    driver.find_element('xpath','//*[@id="su"]').click()

    driver.get_screenshot_as_file('page.png')

    driver.quit()

(为了降低难度,该用例没有使用断言)

从Python用例可以看出,web测试用例步骤大致可分为3类:

  • 外部函数

  • driver中的成员方法

  • find_element返回值element的成员方法

其中不同函数的参数是完全不同,没有统一规律的,不好设计列名。 所以我们会用到 _BlankField 字段来获取无列名(也称"空白字段")的数据。

根据python版本web测试用例,设计xlsx版Web测试用例如下:

test_web.xlsx

说明

关键字

用例名称

name

web测试

启动浏览器

get_driver

chrome

访问百度

get

https://www.baidu.com

定位搜索框

find_element

xpath

//*[@id="kw"]

输入内容

send_keys

大红灯笼高高挂

定位按钮

find_element

xpath

//*[@id="su"]

点击按钮

click

关闭浏览器

quit

警告

当各行数据长度不一时,会使用None填充为满足矩形表格的要求。 读取参数时可以编写函数按需移除None。

Added in version 2.1.0: 在配置文件中添加 xlsx_remove_none_by_blank_field = True 会自动移除空白字段中None

实现钩子

接下来根据用例内容,实现hook,使得每个步骤都能够正确的执行

# conftest.py

def pytest_xlsx_run_step(item: XlsxItem):
    step = item.current_step
    action = step.get("关键字")
    args = step.get("_BlankField")  # `_BlankField` 存储所有空白字段数据
    print(f"{action=}, {args=}")

    if action == 'get_driver':  # 调用外部函数
        item.driver = webdriver.Chrome()
        return True

    if hasattr(item.driver, action):  # 调用driver成员方法
        res = getattr(item.driver, action)(*args)
        if isinstance(res, WebElement):
            item.element = res
        return True
    elif hasattr(item.element,action):  # 调用element成员方法
        getattr(item.element, action)(*args)
        return True

    else:
        raise ValueError('无法识别的步骤类型')

    return True

执行结果如下

(.venv) C:\demo\pytest-xlsx-demo>pytest
=========================== test session starts ============================
platform win32 -- Python 3.12.0, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\demo\pytest-xlsx-demo
configfile: pytest.ini
plugins: xlsx-2.1.0.dev
collected 1 item

tests\example\test_web.xlsx
DevTools listening on ws://127.0.0.1:11274/devtools/browser/590757e6-147c-4878-b3ad-f432a0c23fdb
.                                         [100%]

============================ 1 passed in 4.90s =============================

小结

至此,用于Web测试的xlsx用例已成功实现。