前端自动化测试----百度搜索功能实战

一pytest可以与allure结合生成测试报告。在实际项目中,一旦用例报错,我们希望能够将当时应用的状态纪录下来,纪录这些状态的方法可以以日志、截图、

视频等手段。并将这些纪录在测试报告中,方便相关人员定位问题。allure就能很好的实现这个目标

以web网页版本的百度为例

#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest
import allure
from selenium import webdriver
import yaml
import time


@allure.testcase("https://www.baidu.com")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data1',yaml.safe_load(open("data/data.yml")))
def test_steps_demo(test_data1):
    with allure.step("打开百度网页"):
        driver = webdriver.Chrome()
        driver.get("https://www.baidu.com")
        driver.maximize_window()

    with allure.step(f"输入搜索词:{test_data1}"):
        driver.find_element_by_id("kw").send_keys(test_data1)
        time.sleep(2)
        driver.find_element_by_id("su").click()
        time.sleep(2)

    with allure.step("保存图片"):
        driver.save_screenshot("./result/b.png")
        allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG)
    with allure.step("关闭浏览器"):
        driver.quit()

运行结果:

Testing started at 19:34 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/test_baidu/test_baidudemo.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/test_baidu/test_baidudemo.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\test_baidu
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\test_baidu
plugins: allure-pytest-2.8.32, html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items

test_baidudemo.py                                                     [100%]

============================= 3 passed in 49.84s ==============================

Process finished with exit code 0

代码解析:

allure.testcase用例标识,给定用例的连接,可以与用例的管理地址关联。

allure.feature功能 模块划分,方便管理和运行测试用例。

pytest.mark.parametrize用来参数化测试用例。

allure.step 用来添加测试步骤,在测试报告里面会展示出来这个步骤说明

执行

pytest test_baidudemo.py -s -q --alluredir=./result/

DevTools listening on ws://127.0.0.1:57134/devtools/browser/285c4e64-1440-4dce-8f73-234dfc79c4da
[17956:15624:0130/194230.821:ERROR:device_event_log_impl.cc(211)] [19:42:30.822] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
[17956:15624:0130/194230.823:ERROR:device_event_log_impl.cc(211)] [19:42:30.824] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
.
DevTools listening on ws://127.0.0.1:57242/devtools/browser/87e7ecdb-9963-4d67-990b-92ba3c7dbb67
[1344:14476:0130/194244.233:ERROR:device_event_log_impl.cc(211)] [19:42:44.232] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
[1344:14476:0130/194244.233:ERROR:device_event_log_impl.cc(211)] [19:42:44.233] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
.
DevTools listening on ws://127.0.0.1:57352/devtools/browser/d0d8c07b-eeac-4ef6-8e94-af78cd951982
[6212:3144:0130/194254.709:ERROR:device_event_log_impl.cc(211)] [19:42:54.708] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)
[6212:3144:0130/194254.709:ERROR:device_event_log_impl.cc(211)] [19:42:54.709] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F)

执行allure serve ./result/

 

 

 

posted @ 2021-01-30 19:49  剑尊  阅读(630)  评论(0编辑  收藏  举报