一、项目结构

--driverAction

----Assessement.py

----basicPageAction.py

----BrowserDriver.py

--drivers

----chromedriver.md

--features

----BaiduFanyi.feature

--libraries

----allure-commandline

--pages

----BaiduFayi_page.py

----Indexpage.py

--steps

----Test_BaiduFanyi_steps.py

--utilities

----PathUtility.py

--.gitignore

--main.py

--pytest.ini

--requirements.txt

二、下载内容

2.1 WebDriver

  chromedriver: http://chromedriver.storage.googleapis.com/index.html

  iedriver:http://selenium-release.storage.googleapis.com/index.html

2.2 allure-commandline

  allure 命令行工具,下载地址:https://github.com/allure-framework/allure2/releases

三、代码介绍

3.1 PathUtility.py

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

# @Software PyCharm
# @Time 2021/11/13 9:53 下午
# @Author Helen
# @Desc handle all folder path things
import os
import shutil BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BROWSER_CHROME = os.path.join(BASEDIR, 'drivers/chromedriver')
REPORTPATH = os.path.join(BASEDIR, 'report')
ALLURECOMMANDLINEPATH_LINUX = os.path.join(BASEDIR, 'libraries/allure-commandline/dist/bin/allure')
ALLURECOMMANDLINEPATH_WINDOWS = os.path.join(BASEDIR, 'libraries/allure-commandline/dist/bin/allure.bat')
REPORT_XMLPATH = os.path.join(REPORTPATH, 'xml') def create_folder(path):
if not os.path.exists(path):
os.mkdir(path) def delete_folder_and_sub_files(path):
if os.path.exists(path):
shutil.rmtree(path)

3.2 BrowserDriver.py

  浏览器驱动处理.

# @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://www.jb51.net/article/184205.htm import pytest
from selenium import webdriver
from selenium.webdriver import Chrome
from utilities.PathUtility import BROWSER_CHROME
from driverAction.basicPageAction import basicPageAction @pytest.fixture(scope='module')
def browserDriver():
driver_file_path = BROWSER_CHROME
options = webdriver.ChromeOptions()
driver = Chrome(options=options, executable_path=driver_file_path) driver.maximize_window()
driver.get('https://fanyi.baidu.com/?aldtype=16047#en/zh/') # entrance URL action_object = basicPageAction(driver) yield action_object
driver.close()
driver.quit()

3.3 BasicPageAction.py

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

# @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 selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait class basicPageAction:
def __init__(self, driver):
self.driver = driver
self.timeout = 15
self.driver.set_script_timeout(10) def try_click_element(self, loc):
element = self.get_element_until_visibility(loc)
if element is not None:
element.click()
else:
return False def try_get_element_text(self, loc):
element = self.get_element_until_visibility(loc)
if element is not None:
return element.text
else:
return False def try_send_keys(self, loc, text):
element = self.get_element_until_visibility(loc)
if element is not None:
element.clear()
element.send_keys(text) def get_element_until_visibility(self, loc):
try:
return WebDriverWait(self.driver, self.timeout).until(EC.visibility_of_element_located(loc))
except TimeoutException as e:
return None def try_get_screenshot_as_png(self):
return self.driver.get_screenshot_as_png()

3.4 Asscessment.py

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

# @Software PyCharm
# @Time 2021/11/13 11:16 下午
# @Author Helen
# @Desc handle assertion and save screenshot in allure report import allure
from allure_commons.types import AttachmentType class Assessment: @staticmethod
def assert_text_with_screenshot(expect_value, actual_value, page):
allure.attach(page.get_page_screenshot_as_png(), name='Screenshot', attachment_type=AttachmentType.PNG)
assert expect_value == actual_value

3.5 BaiduFanyi.feature

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

@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  Test_BaiduFanyi_steps.py

  为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 pages.BaiduFanyi_page import BaiduFanyi_page
from driverAction.Assessment import Assessment
from driverAction.BrowserDriver import browserDriver scenarios("../features/BaiduFanyi.feature") @given(parsers.parse('I switch original language as English'))
@allure.step('I switch original language as English')
def translation_setup():
True @when(parsers.parse('I input python'))
@allure.step('I input python')
def original_input(browserDriver):
baiduFanyi_page = BaiduFanyi_page(browserDriver)
baiduFanyi_page.send_keys_for_baidu_translate_input('python') @then(parsers.parse('the translate result should be Python'))
@allure.step('the translate result should be Python')
def check_result(browserDriver):
baiduFanyi_page = BaiduFanyi_page(browserDriver)
Assessment.assert_text_with_screenshot('python', baiduFanyi_page.get_text_from_target_output(), baiduFanyi_page)

3.7 Main.py

  执行测试的主要入口。

# @Software PyCharm
# @Time 2021/11/13 11:25 下午
# @Author Helen
# @Desc import pytest, platform, os
from utilities.PathUtility 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
pytest.main(['-s', '-v', 'steps/Test_BaiduFanyi_steps.py', '--alluredir', r'report/xml'])
# run test cases by tags
# pytest.main(['-m', 'debug', '-s', '-v', 'steps/', '--alluredir', r'report/xml']) if platform.system() == '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) os.system(command=command)

3.8 python.ini

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

[pytest]
markers =
debug

四、执行测试

4.1 run main.py

  

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

  

4.3 报告详情

pytest_BDD + allure 自动化测试框架的更多相关文章

  1. 【转】自动化测试框架: pytest&allure ,提高自动化健壮性和稳定性

    序 在之前,我写过一个系列“从零开始搭建一个简单的ui自动化测试框架(pytest+selenium+allure)”,在这个系列里,主要介绍了如何从零开始去搭建一个可用的自动化工程框架,但是还缺乏了 ...

  2. Maven+TestNG+ReportNG/Allure接口自动化测试框架初探(上)

    转载:http://www.51testing.com/html/58/n-3721258.html 由于一直忙于功能和性能测试,接口自动化测试框架改造的工作被耽搁了好久.近期闲暇一些,可以来做点有意 ...

  3. Android 手机端自动化测试框架

    前言: 大概有4个月没有更新了,因项目和工作原因,忙的手忙脚乱,趁十一假期好好休息一下,年龄大了身体还是扛不住啊,哈哈.这次更新Android端自动化测试框架,也想开源到github,这样有人使用才能 ...

  4. Appium+python自动化(四十二)-Appium自动化测试框架综合实践- 寿终正寝完结篇(超详解)

    1.简介 按照上一篇的计划,今天给小伙伴们分享执行测试用例,生成测试报告,以及自动化平台.今天这篇分享讲解完.Appium自动化测试框架就要告一段落了. 2.执行测试用例&报告生成 测试报告, ...

  5. 广深小龙-基于unittest、pytest自动化测试框架之demo来学习啦!!!

    基于unittest.pytest自动化测试框架之demo,赶紧用起来,一起学习吧! demo分为两个框架:①pytest    ②unittest demo 中 包含 web.api 自动化测试框架 ...

  6. 【接口自动化】Python+Requests接口自动化测试框架搭建【三】

    经过上两篇文章的讲解,我们已经完成接口自动化的基础框架,现在开始根据实际项目丰满起来. 在PyCharm中新建项目,项目工程结构如下: config:配置文件夹,可以将一些全局变量放于配置文件中,方便 ...

  7. ApiTesting全链路自动化测试框架 - 初版发布(一)

    简介 此框架是基于Python+Pytest+Requests+Allure+Yaml+Json实现全链路接口自动化测试. 主要流程:解析接口数据包 ->生成接口基础配置(yml) ->生 ...

  8. ApiTesting全链路接口自动化测试框架 - 新增数据库校验(二)

    在这之前我完成了对于接口上的自动化测试:ApiTesting全链路接口自动化测试框架 - 初版(一) 但是对于很多公司而言,数据库的数据校验也尤为重要,另外也有小伙伴给我反馈希望支持. 所以最近几天我 ...

  9. 【自动化测试框架】pytest和unitttest你知道多少?区别在哪?该用哪个?

    一.大家熟知的自动化测试框架 Java JUnit.TestNG等等. python PyUnit(unittest).Pytest.Robot Framework等等 二.Pytest介绍 pyte ...

随机推荐

  1. Java 中控制执行流程

    if-else 非常常用的流程控制非 if-else 莫属了,其中 else 是可选的,if 有两种使用方式 其一: if (Boolean-expression) { statement; } 其二 ...

  2. C#长程序(留着看)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. [cf1392I]Kevin and Grid

    令$v$为点数(有公共点的格子中存在红/蓝色).$e$为边数(有公共边的格子中存在红/蓝色).$f$为以此法(即仅考虑这些点和边)所分割出的区域数(包括外面).$s$为连通块个数,将欧拉定理简单扩展, ...

  4. Kubernetes Deployment 最佳实践

    零.示例 首先给出一个 Deployment+HPA+ PodDisruptionBudget 的完整 demo,后面再详细介绍其中的每一个部分: apiVersion: apps/v1 kind: ...

  5. GIT Bash 简单讲解-git如何推/拉代码

    GIT Bash 简单讲解 一.            注册/登录GIT账号 注册(或者登录) GitHub地址:https://github.com/ 注册不做详细的讲解,按照注册指示进行注册就可以 ...

  6. 自然溢出哈希 hack 方法

    今天不知道在什么地方看到这个东西,感觉挺有意思的,故作文以记之( 当 \(base\) 为偶数时,随便造一个长度 \(>64\) 的字符串,只要它们后 \(64\) 位相同那么俩字符串的哈希值就 ...

  7. R shinydashboard ——1. 基本用法

    shiny和shinydashboard使用虽然简单,但控件众多,需及时总结归纳. install.packages("shinydashboard") shinydashboar ...

  8. 8.Maximum Depth of Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. Scrapy爬虫框架的安装和使用

    Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...

  10. lxml解析库的安装和使用

    一.lxml的安装lxml是Python的一个解析库,支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高.本节中,我们了解一下lxml的安装方式,这主要从Windows.Linux ...