建设网站实训,金乡网站建设哪家好,网站重做,怎么查网站死链接本文主要介绍了AppiumPythonpytest自动化测试框架的实战#xff0c;文中通过示例代码介绍的非常详细#xff0c;具有一定的参考价值#xff0c;感兴趣的小伙伴们可以参考一下
先简单介绍一下目录#xff0c;再贴一些代码#xff0c;代码里有注释
Basic目录下写的是一些公…本文主要介绍了AppiumPythonpytest自动化测试框架的实战文中通过示例代码介绍的非常详细具有一定的参考价值感兴趣的小伙伴们可以参考一下先简单介绍一下目录再贴一些代码代码里有注释Basic目录下写的是一些公共的方法Data目录下写的是测试数据image存的是测试失败截图Log日志文件Page测试的定位元素report测试报告Test测试用例pytest.ini是pytest启动配置文件requirements.txt需要安装的py模块run.py运行文件Basic/base.py里面封装了 一些方法元素的点击输入查找还有一些自己需要的公共方法也封装在里面如果你们有别的需要可以自己封装调用Basic/deiver.pyAPP启动的前置条件一个是普通的app一个是微信公众号配置微信公众号自动化测试和一般的APP是有点区别的微信需要切换webview才能定位到公众号from appium import webdriver def init_driver(): desired_caps {} # 手机 系统信息 desired_caps[platformName] Android desired_caps[platformVersion] 9 # 设备号 desired_caps[deviceName] emulator-5554 # 包名 desired_caps[appPackage] # 启动名 desired_caps[appActivity] desired_caps[automationName] Uiautomator2 # 允许输入中文 desired_caps[unicodeKeyboard] True desired_caps[resetKeyboard] True desired_caps[autoGrantPermissions] True desired_caps[noReset] False # 手机驱动对象 driver webdriver.Remote(http://127.0.0.1:4723/wd/hub, desired_caps) return driver def driver_weixin(): desired_caps {} # 手机 系统信息 desired_caps[platformName] Android desired_caps[platformVersion] 9 # 设备号 desired_caps[deviceName] # 包名 desired_caps[appPackage] com.tencent.mm # 启动名 desired_caps[appActivity] .ui.LauncherUI # desired_caps[automationName] Uiautomator2 # 允许输入中文 desired_caps[unicodeKeyboard] True desired_caps[resetKeyboard] True desired_caps[noReset] True # desired_caps[newCommandTimeout] 30 # desired_caps[fullReset] false # desired_caps[newCommandTimeout] 10 # desired_caps[recreateChromeDriverSessions] True desired_caps[chromeOptions] {androidProcess: com.tencent.mm:tools} # 手机驱动对象 driver webdriver.Remote(http://127.0.0.1:4723/wd/hub, desired_caps) return driverBasic/get_data.py这是获取测试数据的方法import os import yaml def getData(funcname, file): PATH os.getcwd() os.sep with open(PATH Data/ file .yaml, r, encodingutf8) as f: data yaml.load(f, Loaderyaml.FullLoader) # 1 先将我们获取到的所有数据都存放在一个变量当中 tmpdata data[funcname] # 2 所以此时我们需要使用循环走进它的内心。 res_arr list() for value in tmpdata.values(): tmp_arr list() for j in value.values(): tmp_arr.append(j) res_arr.append(tmp_arr) return res_arrBasic/Log.py日志文件不多介绍# -*- coding: utf-8 -*- 封装log方法 import logging import os import time LEVELS { debug: logging.DEBUG, info: logging.INFO, warning: logging.WARNING, error: logging.ERROR, critical: logging.CRITICAL } logger logging.getLogger() level default def create_file(filename): path filename[0:filename.rfind(/)] if not os.path.isdir(path): os.makedirs(path) if not os.path.isfile(filename): fd open(filename, modew, encodingutf-8) fd.close() else: pass def set_handler(levels): if levels error: logger.addHandler(MyLog.err_handler) logger.addHandler(MyLog.handler) def remove_handler(levels): if levels error: logger.removeHandler(MyLog.err_handler) logger.removeHandler(MyLog.handler) def get_current_time(): return time.strftime(MyLog.date, time.localtime(time.time())) class MyLog: path os.path.dirname(os.path.dirname(os.path.abspath(__file__))) log_file path/Log/log.log err_file path/Log/err.log logger.setLevel(LEVELS.get(level, logging.NOTSET)) create_file(log_file) create_file(err_file) date %Y-%m-%d %H:%M:%S handler logging.FileHandler(log_file, encodingutf-8) err_handler logging.FileHandler(err_file, encodingutf-8) staticmethod def debug(log_meg): set_handler(debug) logger.debug([DEBUG get_current_time() ] log_meg) remove_handler(debug) staticmethod def info(log_meg): set_handler(info) logger.info([INFO get_current_time() ] log_meg) remove_handler(info) staticmethod def warning(log_meg): set_handler(warning) logger.warning([WARNING get_current_time() ] log_meg) remove_handler(warning) staticmethod def error(log_meg): set_handler(error) logger.error([ERROR get_current_time() ] log_meg) remove_handler(error) staticmethod def critical(log_meg): set_handler(critical) logger.error([CRITICAL get_current_time() ] log_meg) remove_handler(critical) if __name__ __main__: MyLog.debug(This is debug message) MyLog.info(This is info message) MyLog.warning(This is warning message) MyLog.error(This is error) MyLog.critical(This is critical message)AI写代码bashBasic/Shell.py执行shell语句方法# -*- coding: utf-8 -*- # Time : 2018/8/1 下午2:54 # Author : WangJuan # File : Shell.py 封装执行shell语句方法 import subprocess class Shell: staticmethod def invoke(cmd): output, errors subprocess.Popen(cmd, shellTrue, stdoutsubprocess.PIPE, stderrsubprocess.PIPE).communicate() o output.decode(utf-8) return oPage/page.pyclass Page: def __init__(self, driver): self.driver driver property def initloginpage(self): return Login_Page(self.driver)Test/test_login.py登陆的测试用我贴一条使用数据文件的用例class Test_login: pytest.mark.parametrize(args, getData(test_login_error, data_error_login)) def test_error_login(self, args): 错误登陆 self.page.initloginpage.input_user(args[0]) self.page.initloginpage.input_pwd(args[1]) self.page.initloginpage.click_login() toast_status self.page.initloginpage.is_toast_exist(args[2]) if toast_status False: self.page.initpatientpage.take_screenShot() assert Falsepytest.inipytest配置文件注释的是启动失败重试3次因为appium会因为一些不可控的原因失败所有正式运行脚本的时候需要加上这个[pytest] ;addopts -s --htmlreport/report.html --reruns 3 addopts -s --htmlreport/report.html testpaths ./Test python_files test_*.py python_classes Test* python_functions test_add_prescription_list requirements.txt 框架中需要的患教直接pip install -r requirements.txt 安装就可以了可能会失败多试几次 python adbutils0.3.4 allure-pytest2.7.0 allure-python-commons2.7.0 Appium-Python-Client0.46 atomicwrites1.3.0 attrs19.1.0 certifi2019.6.16 chardet3.0.4 colorama0.4.1 coverage4.5.3 decorator4.4.0 deprecation2.0.6 docopt0.6.2 enum341.1.6 facebook-wda0.3.4 fire0.1.3 humanize0.5.1 idna2.8 importlib-metadata0.18 logzero1.5.0 lxml4.3.4 more-itertools7.1.0 namedlist1.7 packaging19.0 Pillow6.1.0 pluggy0.12.0 progress1.5 py1.8.0 PyMySQL0.9.3 pyparsing2.4.0 pytest5.0.0 pytest-cov2.7.1 pytest-html1.21.1 pytest-metadata1.8.0 pytest-repeat0.8.0 pytest-rerunfailures7.0 PyYAML5.1.1 requests2.22.0 retry0.9.2 selenium3.141.0 six1.12.0 tornado6.0.3 uiautomator20.3.3 urllib31.25.3 wcwidth0.1.7 weditor0.2.3 whichcraft0.6.0 zipp0.5.1到此这篇关于AppiumPythonpytest自动化测试框架的实战的文章就介绍到这了总结感谢每一个认真阅读我文章的人作为一位过来人也是希望大家少走一些弯路如果你不想再体验一次学习时找不到资料没人解答问题坚持几天便放弃的感受的话在这里我给大家分享一些自动化测试的学习资源希望能给你前进的路上带来帮助。软件测试面试文档我们学习必然是为了找到高薪的工作下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料并且有字节大佬给出了权威的解答刷完这一套面试资料相信大家都能找到满意的工作。视频文档获取方式这份文档和视频资料对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你以上均可以分享点下方小卡片即可自行领取。