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实现简单的地图绘制与标记20250705
python
喜欢吃豆34 分钟前
目前最火的agent方向-A2A快速实战构建(二): AutoGen模型集成指南:从OpenAI到本地部署的全场景LLM解决方案
后端·python·深度学习·flask·大模型
好开心啊没烦恼1 小时前
Python 数据分析:DataFrame,生成,用字典创建 DataFrame ,键值对数量不一样怎么办?
开发语言·python·数据挖掘·数据分析
周树皮不皮2 小时前
20250704【翻转&二叉树】|Leetcodehot100之226【pass】&今天计划
python
魔芋红茶2 小时前
spring-initializer
python·学习·spring
喜欢吃豆2 小时前
快速手搓一个MCP服务指南(九): FastMCP 服务器组合技术:构建模块化AI应用的终极方案
服务器·人工智能·python·深度学习·大模型·github·fastmcp
一个天蝎座 白勺 程序猿2 小时前
Python(28)Python循环语句指南:从语法糖到CPython字节码的底层探秘
开发语言·python
3gying3 小时前
chromedriver
python
DES 仿真实践家4 小时前
【Day 11-N22】Python类(3)——Python的继承性、多继承、方法重写
开发语言·笔记·python
云泽野8 小时前
【Java|集合类】list遍历的6种方式
java·python·list