Appium_py_切换首页分类

本文展示了一个使用Appium进行Android应用自动化测试的例子。通过设置desired_caps并利用XPath定位元素来实现界面操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • #coding=utf-8
    from appium import webdriver
    desired_caps = {}
    desired_caps[‘platformName’] = ‘Android’
    desired_caps[‘platformVersion’] = ‘4.4.2’
    desired_caps[‘deviceName’] = ‘24c151c8’
    desired_caps[‘appPackage’] = ‘com.aaa.bbb’
    desired_caps[‘appActivity’] = ‘.SplashActivity’
    driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
    driver.wait_activity(“.MainActivity”,6,2)
    fenlei_names = driver.find_elements_by_class_name(“android.widget.TextView”)
    print fenlei_names[1].text
    fenlei_names[1].click()
    fenlei_names[2].click()
    driver.find_element_by_xpath(“//android.widget.TextView[contains(@text,’男装’)]”).click()
    driver.find_element_by_xpath(“//android.widget.TextView[contains(@text,’数码家电’)]”).click()
    driver.quit()
class BaseTestCase(unittest.TestCase): def setUp(self): file_name = 'capabilities.yml' config = read_yaml_file(os.path.join(get_project_root_path(),'data',file_name)) # 获取 Appium 配置 appium_config = config['appium'] # 初始化 Appium 选项 options = UiAutomator2Options().load_capabilities(appium_config['capabilities']) # 初始化驱动 self.driver = webdriver.Remote(appium_config['appium_server_url'], options=options) def tearDown(self): if self.driver: self.driver.quit() 测试类:import unittest from time import sleep from appium.webdriver.common.appiumby import AppiumBy from basic.appium_driver import BaseTestCase class TestAppium(BaseTestCase): def test_find_battery(self): el = self.driver.find_element(by=AppiumBy.XPATH, value='(//android.view.View[@content-desc="播放"])[1]') el.click() sleep(4) self.driver.find_element(by=AppiumBy.XPATH,value='(//android.view.View[@content-desc="播放"])[2]').click() sleep(4) def test_find_soundrecorder(self): el = self.driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text("你好日产")') el.click() sleep(4) self.driver.back() self.driver.find_element(by=AppiumBy.XPATH, value='(//android.view.View[@content-desc="播放"])[2]').click() sleep(4) def test_switch_app(self): self.driver.activate_app('com.android.soundrecorder') sleep(3) self.driver.terminate_app("com.android.soundrecorder") # 需要指定包名 self.driver.activate_app('com.tencent.mobileqq') sleep(3) self.driver.press_keycode(3) # Android的HOME键 if __name__ == '__main__': unittest.main() 这两个测试代码合理吗?可以在测试结束正常用到tearDown吗?
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值