当前位置:网站首页>老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
2022-06-28 09:37:00 【小码哥说测试】
前言
这里是小码哥说测试,前段时间停更了很久,主要是在是自动化框架,也就是本文所述的这些东西。框架已经写完了,剩下的就是慢慢分解,写成文章的形式呈现出来。
readyaml
本章还是说上一章所没有讲到的内容。读取yaml文件。我们是以yaml文件作为自动化用例的基础的。所以读取它必不可少。即使是excel也是一样的。
看代码
# encoding=utf-8
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
import yaml
class YamlRead:
def __init__(self, yamlPath):
'''如果是第一次调用,读取yaml文件,否则直接返回之前保存的数据'''
if os.path.exists(yamlPath):
self.yamlPath = yamlPath
else:
raise FileNotFoundError('yaml文件不存在')
self._data = None # 保存yaml的数据
@property # 把一个方法变成属性来调用,
def getData(self):
if not self._data:
with open(self.yamlPath, mode='rb') as f:
self._data = yaml.load(f, Loader=yaml.FullLoader)
return self._data
此处有一定注释,简单的理解就是传入了一个yaml文件路径,做了一系列的判断,最后获取其中的内容,并返回结果值。其他的代码都是做的校验这么一个操作,处理异常情况的。
这里为了后面调用不打(),所以这里加了一个装饰器,将getData当作属性来调用。如果不懂或者不理解的,可以看看前面的python文章。
如何运用
结合上节所说的configs类,在case中写一个yaml文件。上代码:
# encoding=utf-8
# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
import os
class Config:
''' 项目下所有文件的相对路径'''
Base_Path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/..')
Base_yaml = Base_Path + r'\case\login.yaml'
re = Config()
随后调用打印一下就能看到,路径拼接已经OK了。那么这个是不是万能的呢?答案是,不是的。这只是方便某些文件的管理,并不是万能的方法。
写完这里,我们随后直接在上述的YamlRead中调用一下
from configs.readconfig import re
r = YamlRead(re.Base_YAML).getData
print(r)
上述例子中我省略了其中的代码,重复复制太麻烦了,调用后打印就能得到如下结果:
[{'mode': 'time', 'time': 5}, {'until': 'presence_located', 'mode': 'inputs_', 'loc': 'id', 'ele': 'com.mxchip.project352:id/etPhone', 'value': '123456', 'time': 10}, {'until': 'presence_located', 'mode': 'inputs_', 'loc': 'class name', 'ele': 'android.widget.EditText', 'value': 'jx123456', 'num': 1}, {'until': 'visibility_located', 'mode': 'clicks_', 'loc': 'id', 'ele': 'com.mxchip.project352:id/cbAgree'}, {'mode': 'time', 'time': 5}, {'until': 'text_element', 'mode': 'clicks_', 'loc': 'xpath', 'text': '登录', 'ele': '//*[@text="登"]'}]
最后,贴出我的yaml文件格式出来:

总结
如果你成功读出后,结果跟我一致,那么恭喜你,本章内容成功学完。
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于想进阶【自动化测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

边栏推荐
- Write a simple timeline
- 两道面试小Demo
- Explain final, finally, and finalize
- 剑指Offer | 链表转置
- Adapter mode
- This article explains in detail the difficult problems and solutions faced by 3D cameras
- 栈的弹出压入序列<难度系数>
- PHP curl forged IP address and header information code instance - Alibaba cloud
- Installing redis under Linux and windows (ultra detailed graphic tutorial)
- HDI的盲孔设计,你注意到这个细节了吗?
猜你喜欢

线程的生命周期

Screen settings in the source code of OBS Live Room

再见!IE浏览器,这条路由Edge替IE继续走下去

Redis sentinel cluster main database failure data recovery ideas # yyds dry goods inventory #

JVM family (2) - garbage collection

For the development of short video app, the elder warned me to choose the open source code

Thread lifecycle

The concept of "tree structure" perfectly interprets the primary and secondary of things

Matplotlib属性及注解

File operations in QT
随机推荐
Automatic conversion - interview questions
Virtual machine 14 installing win7 (Figure tutorial)
云服务器MYSQL查询速度慢
PMP Exam key summary IX - closing
Looking at jBPM from jbm3 to jbm5 and activiti
JVM family (2) - garbage collection
标识符的命名规则和规范
适配器模式(Adapter)
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Which occupational groups are suitable for the examination
Function sub file writing
Linux下安装redis 、Windows下安装redis(超详细图文教程)
自动转换之-面试题
PMP考试重点总结九——收尾
分而治之之经典Hanoi
PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)
Edit the live broadcast source code with me. How to write the live broadcast app code
Inventory of excellent note taking software: good-looking and powerful visual note taking software, knowledge map tools heptabase, hydrogen map, walling, reflect, infranodus, tiddlywiki
Stutter participle_ Principle of word breaker
优秀笔记软件盘点:好看且强大的可视化笔记软件、知识图谱工具Heptabase、氢图、Walling、Reflect、InfraNodus、TiddlyWiki