appium用例参数化

参数化后

python 复制代码
@pytest.mark.parametrize('searchkey, type, expect_price', [
        ('alibab', 'BABA', '180'),
        ('xiaomi', '01810', '180')
    ])
    def test_search(self, searchkey, type, expect_price):
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/tv_search').click()
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/search_imput_text').send_keys(searchkey)
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/name').click()
        price_element = self.driver.find_element(AppiumBy.XPATH,
            f"//*[@text='{type}']/../..//*[@resource-id='com.xueqiu']").send_keys(searchkey)
        current_price = float(price_element.text)
        assert_that(current_price, close_to(expect_price, expect_price * 0.2))

参数化前

python 复制代码
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from hamcrest import *


class TestAttr:
    def setup(self):
        desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:7555",
            "appPackage": "com.xueqiu.android",
            "appActivity": ".view.WelcomeActivityAlias",
            "noRest": "true",  # 不清除缓存信息
            "dontStopAppOnReset": True,  # 使用当前停留的页面进行元素操作,不会重新启动app加载页面
            "skipDeviceInitialization": True  # 跳过安装权限设置等操作
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.back()  # 返回到上一个页面
        self.driver.quit()  # 退出driver

    def test_search_ali(self):
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/tv_search').click()
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/search_imput_text').send_keys("alibab")
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/name').click()
        current_price = self.driver.find_element(AppiumBy.XPATH,
                                                 "//*[@text='BABA']/../..//*[@resource-id='com.xueqiu']").send_keys(
            "alibab")
        expect_price = 180
        assert_that(current_price, close_to(expect_price, expect_price * 0.2))

    def test_search_xiaomi(self):
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/tv_search').click()
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/search_imput_text').send_keys("alibab")
        self.driver.find_element(AppiumBy.ID, 'com.xueqiu.android:id/name').click()
        current_price = self.driver.find_element(AppiumBy.XPATH,
                                                 "//*[@text='BABA']/../..//*[@resource-id='com.xueqiu']").send_keys(
            "alibab")
        expect_price = 180
        assert_that(current_price, close_to(expect_price, expect_price * 0.2))
相关推荐
之歆12 分钟前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
天天爱吃肉82181 小时前
ZigBee通信技术全解析:从协议栈到底层实现,全方位解读物联网核心无线技术
python·嵌入式硬件·物联网·servlet
Allen_LVyingbo2 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
智能砖头2 小时前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
风逸hhh3 小时前
python打卡day58@浙大疏锦行
开发语言·python
烛阴4 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook4 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Gyoku Mint5 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
郭庆汝11 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
思则变14 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest