ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

appium之toast处理

2019-05-14 11:53:30  阅读:197  来源: 互联网

标签:toast webdriver appium 处理 text driver exist import


注意

toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量。

toast定位

1.先看下toast长什么样,如下图,像这种弹出来的消息"再按一次退出",这种就是toast了。

2.想定位toast元素,这里一定要注意automationName的参数必须是Uiautomator2才能定位到。

'automationName': 'Uiautomator2'

# coding:utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

desired_caps = {
                'platformName': 'Android',
                'deviceName': '127.0.0.1:62001',
                'platformVersion': '4.4.2',
                'appPackage': 'com.baidu.yuedu',
                'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
                'noReset': 'true',
                'automationName': 'Uiautomator2'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

# 等主页面activity出现
driver.wait_activity(".base.ui.MainActivity", 10)

driver.back()   # 点返回

# 定位toast元素
toast_loc = ("xpath", ".//*[contains(@text,'再按一次退出')]")
t = WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(toast_loc))   #注意等待必须是元素存在,元素可见会报错
print t

3.打印出来的结果,出现如下信息,说明定位到toast了

<appium.webdriver.webelement.WebElement (session="02813cce-9aaf-4754-a532-07ef7aebeb88", element="339f72c4-d2e0-4d98-8db0-69be741a3d1b")>

封装toast判断

1.单独写一个函数来封装判断是否存在toast消息,存在返回True,不存在返回False

def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):
    '''is toast exist, return True or False
    :Agrs:
     - driver - 传driver
     - text   - 页面上看到的文本内容
     - timeout - 最大超时时间,默认30s
     - poll_frequency  - 间隔查询时间,默认0.5s查询一次
    :Usage:
     is_toast_exist(driver, "看到的内容")
    '''
    try:
        toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
        return True
    except:
        return False

参考代码

# coding:utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {
                'platformName': 'Android',
                'deviceName': '127.0.0.1:62001',
                'platformVersion': '4.4.2',
                'appPackage': 'com.baidu.yuedu',
                'appActivity': 'com.baidu.yuedu.splash.SplashActivity',
                'noReset': 'true',
                'automationName': 'Uiautomator2'
                }

def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):
    '''is toast exist, return True or False
    :Agrs:
     - driver - 传driver
     - text   - 页面上看到的文本内容
     - timeout - 最大超时时间,默认30s
     - poll_frequency  - 间隔查询时间,默认0.5s查询一次
    :Usage:
     is_toast_exist(driver, "看到的内容")
    '''
    try:
        toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
        return True
    except:
        return False

if __name__ == "__main__":
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    # 等主页面activity出现
    driver.wait_activity(".base.ui.MainActivity", 10)

    driver.back()   # 点返回

    # 判断是否存在toast-'再按一次退出'
    print is_toast_exist(driver, "再按一次退出")

标签:toast,webdriver,appium,处理,text,driver,exist,import
来源: https://blog.csdn.net/qq_42293590/article/details/90202851

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有