当前位置:网站首页>老板叫我写个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文件格式出来:

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

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

边栏推荐
猜你喜欢

TCP实战案例之即时通信、BS架构模拟

PMP考试重点总结八——监控过程组(2)

适配器模式(Adapter)
![[ybtoj advanced training guide] maximum separation [hash] [Floyd]](/img/86/542ab1728a2ddbc01592b2fa83491a.jpg)
[ybtoj advanced training guide] maximum separation [hash] [Floyd]

HDI的盲孔设计,你注意到这个细节了吗?

用 Compose 实现个空调,为你的夏日带去清凉

Installing redis under Linux and windows (ultra detailed graphic tutorial)

Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)

JDBC connection database (MySQL) steps

再見!IE瀏覽器,這條路由Edge替IE繼續走下去
随机推荐
标识符的命名规则和规范
HDI blind hole design, have you noticed this detail?
云服务器MYSQL查询速度慢
Automatic conversion - interview questions
PMP Exam key summary VI - chart arrangement
适配器模式(Adapter)
Write a simple timeline
1181: integer parity sort
Full link service tracking implementation scheme
Caffeine cache, the king of cache, has stronger performance than guava
Unity AssetBundle资源打包与资源加载
This article explains in detail the difficult problems and solutions faced by 3D cameras
栈的弹出压入序列<难度系数>
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
全链路业务追踪落地实践方案
PMP考试重点总结七——监控过程组(1)
JVM系列(2)——垃圾回收
Divide and rule classic Hanoi
JVM family (2) - garbage collection
桥接模式(Bridge)