【移动自动化】【八】数据驱动

数据驱动

  1. 基于数据流完成流程调度,数据来源于外部,可以从execl,yaml,数据库中获取
  2. 使用yaml, json等读取数据
  3. 流程: 读取外部文件 -> 动态创建测试用例

yaml

  1. pip install pyyaml
  2. yaml文件操作https://github.com/yaml/pyyaml?spm=a2c6h.12873639.0.0.70ab3ab4BvVbOE
  3. 简单的步骤驱动例子
  4. yaml文件: ch2/case.yaml
# 简单的case_yaml模板
- Descripton: 进入搜索页面
  Methods: id
  Value: ivSearch
  Action: click
- Descripton: 搜索测试数据
  Methods: id
  Value: etSearch
  Action: send_keys
  data: 测试
- Descripton: 获取搜索值
  Methods: id
  Value: tvSearchResult
  Action: text
  1. 测试步骤驱动 ch2/test_yaml.py
    1. 读取yaml文件并且以dict格式输出
    2. 定义drver: WebDriver (等同于把之前的self.driver传给数据驱动方法)
    3. 循环每个字典,执行不同的测试步骤
    4. method函数: 定位有不同方式,这边单独封装,主要涉及的有id,xpath,accessibility_id
    5. 测试驱动,直接调用self.method函数
class TestCaseTemplate:
    def __init__(self, path):
        with open(path, 'r', encoding="utf-8") as file:
            self.steps = yaml.safe_load(file)

    def method(self, driver: WebDriver, method, value):
        ele = None
        if method == 'id':
            ele = driver.find_element_by_id(value)
        elif method =='xpath':
            ele = driver.find_element_by_xpath(value)
        elif method == 'accessibility':
            ele = driver.find_element_by_accessibility_id(value)
        else:
            return 'No element'
        return ele

    def run(self, driver: WebDriver):
        for step in self.steps:
            elemet = None
            if isinstance(step, dict):
                if 'Methods' in step.keys() and 'Value' in step.keys():
                    elemet = self.method(driver, step['Methods'], step['Value'])
                else:
                    print(step.keys())
                if 'Action' in step.keys():
                    if 'click' in step['Action']:
                        elemet.click()
                    elif 'send_keys' in step['Action']:
                        if 'data' in step.keys():
                            elemet.send_keys(step['data'])
                        else:
                            print('没有输入的测试数据')
                    elif 'text' in step['Action']:
                        data = elemet.text
                        print(data)

github

https://github.com/wangxiao9/appium_demo.git

posted @ 2020-08-09 16:20  WANG-X  阅读(97)  评论(0编辑  收藏  举报