appium demo 定位 智能等待及滑动元素

本文介绍使用Appium结合Selenium进行移动应用自动化测试的方法,通过具体实例演示如何实现元素定位、滑动操作及输入验证等功能。

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

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
import unittest
from appium import webdriver


def element_to_be_clickable(driver, location=None, selector="xpath"):
    wait = WebDriverWait(driver, 20, 1)
    if selector == "css":
        element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, location)))
    else:
        element = wait.until(EC.element_to_be_clickable((By.XPATH, location)))
    return element


def ele_to_located(driver, location=None, selector="xpath"):
    wait = WebDriverWait(driver, 20, 1)
    if selector == "css":
        element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, location)))
    else:
        element = wait.until(EC.visibility_of_element_located((By.XPATH, location)))
    return element


# 向下滑动。x轴保持不变,y轴:由小变大
def swipe_down(driver, x, y, y1, duration=2000):
    driver.swipe(x, y, x, y1, duration)


class SearchPage(unittest.TestCase):
    def setUp(self):
        desired_cups = {}
        desired_cups['platformName'] = 'Android'
        desired_cups['platformVersion'] = '7.1.2'
        desired_cups['deviceName'] = 'Android Emulator'
        desired_cups['automationName'] = 'uiautomator2'
        desired_cups['noReset'] = 'false'
        desired_cups['autoGrantPermissions'] = 'true'
        desired_cups['appPackage'] = 'com.hpbr.bosszhipin'
        desired_cups['appActivity'] = 'com.hpbr.bosszhipin.module.launcher.WelcomeActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_cups)

    def test_search(self):
        accept = element_to_be_clickable(self.driver, "//*[@resource-id='com.hpbr.bosszhipin:id/tv_positive']")
        accept.click()
        find_job = element_to_be_clickable(self.driver, "//*[@resource-id='com.hpbr.bosszhipin:id/btn_enter_geek']")
        find_job.click()
        prefix = element_to_be_clickable(self.driver,
                                         "//*[@resource-id='com.hpbr.bosszhipin:id/tv_country_phone_code']")
        prefix.click()
        x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        y = self.driver.get_window_size()['height']
        ele_to_located(self.driver, "//*[@resource-id='com.hpbr.bosszhipin:id/tv_country']")
        #设置 滑动位置
        self.driver.swipe(326, 1058, 326, 581, 1000)
        self.driver.swipe(326, 581, 326, 1058, 1000)
        china = element_to_be_clickable(self.driver,
                                        "//*[contains(@text,'中国大陆')]")
        china.click()
        iphone = ele_to_located(self.driver, "//*[contains(@text,'手机号码')]")
        iphone.clear()
        iphone.send_keys("13764000000")
        we_chat = ele_to_located(self.driver, "//*[contains(@text,'微信登录')]")
        we_chat.click()
        authorization = ele_to_located(self.driver, "//*[contains(@text,'同意')]")
        authorization.click()

if __name__ == '__main__':
    unittest.main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值