飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

pytest_BDD + allure 自动化测试框架

时间:2021-12-13  作者:helenMemery  

一、项目结构

--driverAction

----域名

----域名

----域名

--drivers

----域名

--features

----域名ure

--libraries

----allure-commandline

--pages

----域名

----域名

--steps

----域名

--utilities

----域名

--.gitignore

--域名

--域名

--域名

二、下载内容

2.1 WebDriver

  chromedriver: http://域名域名/域名

  iedriver:http://selenium-域名域名/域名

2.2 allure-commandline

  allure 命令行工具,下载地址:https://域名/allure-framework/allure2/releases

三、代码介绍

3.1 域名

处理一些必要的文件路径事件

# @Software PyCharm
# @Time 2021/11/13 9:53 下午
# @Author Helen
# @Desc handle all folder path things
import os
import shutil

BASEDIR = 域名ame(域名ame(域名ath(__file__)))
BROWSER_CHROME = 域名(BASEDIR, \'drivers/chromedriver\')
REPORTPATH = 域名(BASEDIR, \'report\')
ALLURECOMMANDLINEPATH_LINUX = 域名(BASEDIR, \'libraries/allure-commandline/dist/bin/allure\')
ALLURECOMMANDLINEPATH_WINDOWS = 域名(BASEDIR, \'libraries/allure-commandline/dist/bin/域名\')
REPORT_XMLPATH = 域名(REPORTPATH, \'xml\')


def create_folder(path):
    if not 域名ts(path):
        域名r(path)


def delete_folder_and_sub_files(path):
    if 域名ts(path):
        域名ee(path)

3.2 域名

  浏览器驱动处理.

# @Software PyCharm
# @Time 2021/11/13 9:39 下午
# @Author Helen
# @Desc the very start: setup browser for testing
# @note have a knowledge need to get from https://域名/article/域名

import pytest
from selenium import webdriver
from 域名river import Chrome
from 域名Utility import BROWSER_CHROME
from 域名cPageAction import basicPageAction


@域名ure(scope=\'module\')
def browserDriver():
    driver_file_path = BROWSER_CHROME
    options = 域名meOptions()
    driver = Chrome(options=options, executable_path=driver_file_path)

    域名mize_window()
    域名(\'https://域名/?aldtype=16047#en/zh/\')  # entrance URL

    action_object = basicPageAction(driver)

    yield action_object
    域名e()
    域名()

3.3 域名

  处理页面操作的公共方法:不全,可以根据项目添加其他内容。

# @Software PyCharm
# @Time 2021/11/13 10:04 下午
# @Author Helen
# @Desc common page action collection, aim to make sure element have show up before next action
from 域名ptions import TimeoutException
from 域名ort import expected_conditions as EC
from 域名域名 import WebDriverWait


class basicPageAction:
    def __init__(self, driver):
        域名er = driver
        域名out = 15
        域名script_timeout(10)

    def try_click_element(self, loc):
        element = 域名element_until_visibility(loc)
        if element is not None:
            域名k()
        else:
            return False

    def try_get_element_text(self, loc):
        element = 域名element_until_visibility(loc)
        if element is not None:
            return 域名
        else:
            return False

    def try_send_keys(self, loc, text):
        element = 域名element_until_visibility(loc)
        if element is not None:
            域名r()
            域名_keys(text)

    def get_element_until_visibility(self, loc):
        try:
            return WebDriverWait(域名er, 域名out).until(域名bility_of_element_located(loc))
        except TimeoutException as e:
            return None

    def try_get_screenshot_as_png(self):
        return 域名screenshot_as_png()

3.4 域名

  处理断点的公共类:不全,可以根据项目自行添加其他内容。

# @Software PyCharm
# @Time 2021/11/13 11:16 下午
# @Author Helen
# @Desc handle assertion and save screenshot in allure report

import allure
from 域名s import AttachmentType


class Assessment:

    @staticmethod
    def assert_text_with_screenshot(expect_value, actual_value, page):
        域名ch(域名page_screenshot_as_png(), name=\'Screenshot\', attachment_type=域名)
        assert expect_value == actual_value

3.5 域名ure

  正式进到测试主题,编写用例行为,即测试用例。

@debug
Feature: Baidu_translation

  Scenario: check English translate to Chinese
    Given I switch original language as English
    When I input python
    Then the translate result should be Python

3.6  域名

  为3.5所设计的用户行为编写实现方法。

  (如代码所示:有个问题我不懂的,希望有大佬帮我解答)

# @Software PyCharm
# @Time 2021/11/13 11:08 下午
# @Author Helen
# @Desc there have an issue I haven\'t figure out : if I not import browserDriver, there will can\'t find it when running

import allure
from pytest_bdd import scenarios, given, when, then, parsers
from 域名uFanyi_page import BaiduFanyi_page
from 域名ssment import Assessment
from 域名serDriver import browserDriver

scenarios("../features/域名ure")


@given(域名e(\'I switch original language as English\'))
@域名(\'I switch original language as English\')
def translation_setup():
    True


@when(域名e(\'I input python\'))
@域名(\'I input python\')
def original_input(browserDriver):
    baiduFanyi_page = BaiduFanyi_page(browserDriver)
    域名_keys_for_baidu_translate_input(\'python\')


@then(域名e(\'the translate result should be Python\'))
@域名(\'the translate result should be Python\')
def check_result(browserDriver):
    baiduFanyi_page = BaiduFanyi_page(browserDriver)
    域名rt_text_with_screenshot(\'python\', 域名text_from_target_output(), baiduFanyi_page)

3.7 域名

  执行测试的主要入口。

# @Software PyCharm
# @Time 2021/11/13 11:25 下午
# @Author Helen
# @Desc

import pytest, platform, os
from 域名Utility import create_folder, delete_folder_and_sub_files, REPORTPATH, REPORT_XMLPATH, \
    ALLURECOMMANDLINEPATH_WINDOWS, ALLURECOMMANDLINEPATH_LINUX

if __name__ == "__main__":
    create_folder(REPORTPATH)
    delete_folder_and_sub_files(REPORT_XMLPATH)
    create_folder(REPORT_XMLPATH)

    # run test cases by path
    域名([\'-s\', \'-v\', \'steps/域名\', \'--alluredir\', r\'report/xml\'])
    # run test cases by tags
    # 域名([\'-m\', \'debug\', \'-s\', \'-v\', \'steps/\', \'--alluredir\', r\'report/xml\'])

    if 域名em() == \'Windows\':
        command = "{} generate report/xml -o report/allure_report --clean".format(ALLURECOMMANDLINEPATH_WINDOWS)
    else:
        command = "{} generate report/xml -o report/allure_report --clean".format(ALLURECOMMANDLINEPATH_LINUX)

    域名em(command=command)

3.8 域名

  目前主要用来设置标签。有兴趣可以读一下https://域名/weixin_48500307/article/details/108431634

[pytest]
markers =
    debug

四、执行测试

4.1 run 域名

  

4.2 在report文件夹中查看测试报告。

  

4.3 报告详情

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。