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

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

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

边栏推荐
- 装饰模式(Decorator)
- 桥接模式(Bridge)
- Regular verification of mobile phone number and email [easy to understand]
- Unity AssetBundle资源打包与资源加载
- 六月集训(第28天) —— 动态规划
- 自定义异常类及练习
- Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
- This article explains in detail the difficult problems and solutions faced by 3D cameras
- 虚拟机14安装win7(图教程)
- PMP Exam key summary IX - closing
猜你喜欢

代理模式(Proxy)

Caffeine cache, the king of cache, has stronger performance than guava

桥接模式(Bridge)

Unity AssetBundle资源打包与资源加载
![[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)](/img/04/454bede0944f56ba69cddf6b237392.jpg)
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)

Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app

Proxy mode (proxy)

Bron filter Course Research Report

Dolphin scheduler uses system time

P2394 yyy loves Chemistry I
随机推荐
How to reduce the risk of project communication?
bad zipfile offset (local header sig)
Key summary VII of PMP examination - monitoring process group (1)
Key summary IV of PMP examination - planning process group (2)
JDBC connection database (MySQL) steps
Regular verification of mobile phone number and email [easy to understand]
File operations in QT
线程和进程
再见!IE浏览器,这条路由Edge替IE继续走下去
For the development of short video app, the elder warned me to choose the open source code
DolphinScheduler使用系统时间
Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
On the influence of small program on the digitalization of media industry
01 distributed system overview
全局异常处理器与统一返回结果
1182: effets de la photo de groupe
在本类私有属性直接使用?new()在使用!!!
再見!IE瀏覽器,這條路由Edge替IE繼續走下去
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
PMP考试重点总结八——监控过程组(2)