框架综合实践(1)-driver的封装(capability)
发布日期:2021-05-10 11:27:20 浏览次数:23 分类:精选文章

本文共 1881 字,大约阅读时间需要 6 分钟。

读取yaml配置文件

在Python脚本开发中,读取yaml配置文件是非常常见的操作。以下将详细介绍两种常用的读取方法:file = openwith open

方法一:file = open 使用方式

file = open('../config/WiFibanlv_caps.yaml', 'r')data = yaml.load(file)file.close()  # 确保使用close(),否则可能会占用文件资源

方法二:with open 使用方式

with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file:    data = yaml.load(file)

安装包的相对路径使用方法

要获取安装包的相对路径,可以按照以下步骤操作:

  • 导入os模块:
  • import os
    1. 获取当前文件的目录:
    2. current_dir = os.path.dirname(__file__)
      1. 根据需要拼接上一级目录:
      2. base_dir = os.path.dirname(current_dir)
        1. 定义安装包路径:
        2. app_path = os.path.join(base_dir, 'app', data['appname'])

          完整脚本

          完整脚本如下:

          import yamlimport loggingimport logging.configimport os# 日志配置文件路径CON_LOG = '../config/log.conf'logging.config.fileConfig(CON_LOG)logger = logging.getLogger()def appium_desired():    # 读取配置文件数据    with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file:        data = yaml.load(file)        logger.info("初始化APP...")        desired_caps = {}    desired_caps['platformName'] = data['platformName']    desired_caps['platformVersion'] = data['platformVersion']    desired_caps['deviceName'] = data['deviceName']        # 定义安装包的路径    current_dir = os.path.dirname(__file__)    base_dir = os.path.dirname(current_dir)    app_path = os.path.join(base_dir, 'app', data['appname'])    desired_caps['app'] = app_path    desired_caps['packageName'] = data['packageName']    desired_caps['appActivity'] = data['appActivity']    desired_caps['noReset'] = data['noReset']    desired_caps['unicodekeyboard'] = data['unicodekeyboard']    desired_caps['resetkeyboard'] = data['resetkeyboard']    desired_caps['uiautomationName'] = data['uiautomationName']        logger.info("启动APP...")    driver = webdriver.Remote(f'http://{data["ip"]}:{data["port"]}/wd/hub', desired_caps)    driver.implicitly_wait(8)    return driverif __name__ == '__main__':    appium_desired()

          每次封装一个模块时,都需要在开头检查脚本是否正常运行。封装后运行结果如下:

    上一篇:框架综合实践(2)-公共模块Common的封装
    下一篇:Appium自动化测试框架

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年05月15日 19时12分37秒