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

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

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

边栏推荐
- Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
- The constructor is never executed immediately after new()!!!!!
- HDI blind hole design, have you noticed this detail?
- Explain final, finally, and finalize
- Static page of pinyougou mall
- Regular verification of mobile phone number and email [easy to understand]
- Unity 从服务器加载AssetBundle资源写入本地内存,并将下载保存的AB资源从本地内存加载至场景
- PMP考试重点总结四——规划过程组(2)
- Huawei OSPF single region
- 01 distributed system overview
猜你喜欢

1181: integer parity sort

全局异常处理器与统一返回结果

通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别

Thread lifecycle

Au revoir! Navigateur ie, cette route Edge continue pour IE

Dolphin scheduler uses system time

代理模式(Proxy)

Unity AssetBundle资源打包与资源加载

Linux下安装redis 、Windows下安装redis(超详细图文教程)

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
随机推荐
On the influence of small program on the digitalization of media industry
==And eqauls()
Summary of PMP learning experience
Machine virtuelle 14 installer win7 (tutoriel)
Regular verification of mobile phone number and email [easy to understand]
Linux下安装redis 、Windows下安装redis(超详细图文教程)
Installing redis under Linux and windows (ultra detailed graphic tutorial)
Differences between task parameter types inout and ref
桥接模式(Bridge)
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
4 methods for exception handling
[ybtoj advanced training guidance] class roll call [string hash]
什么是在线开户?现在网上开户安全么?
详解final、finally和finalize
Write a simple timeline
Comprehensive evaluation of outline note taking software workflow: advantages, disadvantages and evaluation
DolphinScheduler使用系统时间
File operations in QT
自动转换之-面试题