(测试步骤读取yaml文件)appium+python参数化数据传输

 yaml安装

一、参数化数据读取YAML文件

多组数据读取内部文件

import pytest
#
传多组数据,有几组数据,就遍历几次 @pytest.mark.parametrize("usname,pswd", [("xiaoming","123456"), ("xiaohong","123678")]) def test_login(self,usname, pswd): login_page=LoginPage(driver=self.driver) login_page.login(usname, pswd)

注意:noReset要设置为False,要不然第一次登录如果成功后,后面的就不再继续登录了

读取外部文件YAML

将测试数据存放在YAML文件中,然后进行调用。

以登录QQ为例

logindata_yaml.yaml

- [ 'xiaoming', '123456']
- [ 'xiaohong', '123678']

用例如下:

import pytest
import yaml
from config.globalparameter import yaml_path

logindata = yaml.safe_load(open(yaml_path + "\logindata_yaml.yaml", "r"))  # 读取数据


@pytest.mark.parametrize("keyword, expected_price", logindata)  # yaml中几组数据就遍历几次
def test_login(keyword, expected_price):
    aa = driver.find_element(by.ACCESSIBILITY_ID, "请输入QQ号码或手机或邮箱")
    aa.send_keys(keyword)
    bb = driver.find_element(by.ID, "com.tencent.mobileqq:id/password")
    bb.send_keys(expected_price)
    driver.implicitly_wait(50)

 

二、测试步骤读取YAML文件

testlogin.yaml(yaml是操作步骤,按需要的操作步骤填写即可,Yaml中不要有中文,)

- id: com.tencent.mobileqq:id/password
  input: ceshi
- id: com.tencent.mobileqq:id/btn_login

 

用例代码(测试QQ登录页面的“登录”按钮)

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver

loginpath = yaml.safe_load(open(yaml_path + "testlogin.yaml", "r"))


# 数据驱动:测试步骤的驱动
def test_001(self):
    TestCase(loginpath).run(self.driver)


class TestCase:  # 测试步骤的数据驱动
    def __init__(self, path):
        file = open(path, "r")
        self.steps = yaml.safe_load(file)

    def run(self, driver: WebDriver):
        for step in self.steps:
            element = None
            print(step)
            if isinstance(step, dict):
                if "id" in step.keys():
                    element = driver.find_element(AppiumBy.ID, step["id"])
                elif "xpath" in step.keys():
                    element = driver.find_element(AppiumBy.XPATH, step["xpath"])
                else:
                    print(step.keys())
                if "input" in step.keys():
                    element.send_keys(step["input"])
                else:
                    element.click()
                if "get" in step.keys():
                    text = element.get_attribute(step["get"])
                    print(text)

 

三、读取YAML文件中的某个值

loginaccount.yaml

login_caps:
  ACCESSIBILITY_ID: 11111111111
  id: com.tencent.mobileqq:id/password

读取数据

def readelement(path):
    data = yaml.safe_load(open(path, "r"))
    print(data)
    return data

调用数据,比如调用yaml文件中的 id 数据

data = readelement(element_path+"loginaccount.yaml")
id_text = data["login_caps"]["id"]
print(id_text)

运行结果为输出

com.tencent.mobileqq:id/password

 四、测试步骤和测试数据来自不同的YAML文件

测试步骤文件
login_step.yaml
- id: com.tencent.mobileqq:id/fcb
  input: 13860888888
- id: com.tencent.mobileqq:id/ail
- id: com.tencent.mobileqq:id/dialogLeftBtn

测试数据文件

account_data.yaml

- '13660888888'
- '13860999666'

login_yaml.py
# from appium.webdriver.common.appiumby import AppiumBy
import yaml
from config.driver_configure import DriverClinet
from util.find_elementUtil import ElementClass
# from util.loggerUtil import Logger


class TestCase:  # 测试步骤的数据驱动
    def __init__(self, path):
        file = open(path, "r")
        self.steps = yaml.safe_load(file)

    def run(self, text=None):
        driver = DriverClinet().get_driver()
        class_driver = ElementClass(driver)
        for step in self.steps:
            element = None
            print(step)
            if isinstance(step, dict):
                if "id" in step.keys():
                    element = class_driver.find_element('ID', step["id"])
                    # element = driver.find_element(AppiumBy.ID, step["id"])
                elif "xpath" in step.keys():
                    element = class_driver.find_element('XPATH', step["xpath"])
                    # element = driver.find_element(AppiumBy.XPATH, step["xpath"])
                else:
                    print(step.keys())
                if "input" in step.keys():
                    if text is not None:  # 可能调取外部的数据
                        element.send_keys(text)
                    else:
                        element.send_keys(step["input"])
                else:
                    element.click()
                if "get" in step.keys():
                    text = element.get_attribute(step["get"])
                    print(text)
用例:
# import time
import allure
import pytest
from action.login_yaml import TestCase
# import yaml
from config.all_path import yamlP,yamlD
from config import read_yaml
import os


path = os.path.join(yamlP, "loginstep.yaml")
path_y = os.path.join(yamlD, "account_data.yaml")
a_data = read_yaml.YamlUtil(path_y).read_yaml()


class TestYaml:
    # 数据驱动:测试步骤的驱动

    @pytest.mark.parametrize('data_yy', a_data)
    def test_001(self, data_yy):
        TestCase(path).run(data_yy)

 

 

 

posted @ 2023-09-11 19:46  yimu-yimu  阅读(74)  评论(0编辑  收藏  举报