Selenium+Unittest+HTMLTestRunner框架更改为Selenium+Pytest+Allure(二)

1 代码框架

整体项目结构如图:

Common:公共库

Logs: 日志目录

Page: 页面元素

Report:测试报告

TestCase:测试用例

TestData: 测试数据

2 单模块运行

直接上代码:

python 复制代码
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     TestJohnDeere.py
   Author :        曾良均
   QQ:             277099728
   Date:          12/1/2023 4:03 PM   
   Description :
-------------------------------------------------
   Change Activity:
                   
-------------------------------------------------
"""
__author__ = 'ljzeng'

import pytest
from Common.logger import Log
from Common.Operator import *
from Common.Logins import Logins
from Page.Credentials.CredentialsPage import CredentialsPage as cp
from selenium.webdriver.common.by import By
import allure

log = Log("TestJohnDeere")


class TestJohnDeere:
    driver = None
    lg = None
    page = None

    coll = (By.XPATH, '//*[@id="nav_arrow"]/div')

    @pytest.fixture()
    def begin(self):
        log.info('--------开始测试John Deere Credentials功能--------')
        self.driver = browser("chrome")
        self.lg = Logins()
        self.lg.login(self.driver, 'atcred@iicon004.com', 'Win.12345')
        self.driver.implicitly_wait(10)

        self.page = cp()
        ac = self.lg.get_attribute(self.coll, 'class')
        while True:
            if ac != 'icn collapse':
                ar = (By.ID, 'nav_arrow')
                self.page.click(ar)
                continue
            else:
                break
        self.lg.click(self.page.johndeere_menu)
        time.sleep(1)
        self.lg.switch_to_iframe(self.page.right_iframe)

        yield self.lg
        self.driver.quit()

    def add_jdlink(self, begin):
        log.info('点击 JD Link 的Add')
        if not begin.is_clickable(self.page.jdlink_add_btn):
            time.sleep(2)
        try:
            begin.click(self.page.jdlink_add_btn)
            time.sleep(1)
            self.driver.switch_to.window(self.driver.window_handles[1])
            time.sleep(2)
            txt = begin.get_text(self.page.jdlink_page_signin_lable)
        except Exception:
            log.info('Add 跳转失败!')
            return False
        else:
            log.info('Add 跳转成功!')
            self.driver.switch_to.window(self.driver.window_handles[0])
            if txt == 'Sign In':
                return True
            else:
                return False

    @allure.feature("测试Credentials功能")
    @allure.story("测试JD Link Credentials设置功能")
    def test_addJDlink(self, begin):
        """测试Add JD Link功能"""
        res = self.add_jdlink(begin)
        if res:
            log.info('Add JD Link 测试成功!')
        else:
            log.info('Add JD Link 测试失败!')
        assert res


if __name__ == '__main__':
    pytest.main(['-vs', 'TestJohnDeere.py'])  # 主函数模式

pytest.main(['-vs', 'TestJohnDeere.py'])

只运行当前文件,执行结果在Pycharm工作台显示。

3 主函数运行指定用例

3.1 pytest.ini定义要执行的用例文件、类、方法
python 复制代码
[pytest]
addopts=-vs                 
testpaths=./TestCase
python_files=Test*.py
python_classes=Test
python_functions=test
markers=
    smoke:
    uermanage:
3.2 主函数运行(run.py)
python 复制代码
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     run.py
   Author :        曾良均
   QQ:             277099728
   Date:          12/4/2023 9:27 AM   
   Description :    这是所有测试用例运行的主函数
-------------------------------------------------
   Change Activity:

-------------------------------------------------
"""
__author__ = 'ljzeng'
import pytest
import os


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--clean-alluredir', '--alluredir=allure-results'])
    os.system(r"copy environment.properties allure-results\\")
    os.system(r"allure generate -c -o Report\allure-report")
    # os.system(r"allure open Report\allure-report")  # 此命令会打开报告

4 查看Allure报告

通过主函数生成Allure报告文件,命令行打开报告:

python 复制代码
allure open Report\allure-report

浏览器自动打开报告:

5 遇到的问题

5.1 allure-results does not exists

当pytest.main中指定的--alluredir不是allure-results时,后面执行allure generate会把如题错误

解决方法:指定 --alluredir=allure-results 即可

相关推荐
Smoothcloud润云13 小时前
从“预测下一个词”到“预测下一个世界状态”:世界模型作为AGI新范式的深度分析报告
人工智能·测试工具·微服务·容器·github·状态模式·agi
测试199816 小时前
postman接口测试详解
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
时光不写代码16 小时前
修复 pytest-asyncio 事件循环冲突:完整解决方案
python·pytest·fastapi
上天_去_做颗惺星 EVE_BLUE18 小时前
接口自动化测试全流程:pytest 用例收集、并行执行、Allure 报告合并与上传
python·pytest
测试秃头怪21 小时前
python&selenium自动化测试实战项目详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
简单点了1 天前
mac安装wireshark
测试工具·macos·wireshark
踏着七彩祥云的小丑1 天前
pytest——Mark标记
开发语言·python·pytest
西瓜程序猿2 天前
使用手机如何将纸质礼薄转换为电子礼薄?
测试工具·智能手机·创业创新·记了么·电子礼薄·份子钱
半个俗人2 天前
fiddler的基础使用
前端·测试工具·fiddler
站长工具箱2 天前
基于浏览器的键盘按键测试工具功能解析
测试工具·计算机外设