appium+python自动化29-toast


注意

toast要appium1.6.3以上版本才支持,appium1.4的版本就别浪费时间了

Supported Platforms

1.查看appium v1.7版本官方文档

Supported Platforms

Appium supports app automation across a variety of platforms, like iOS, Android, and Windows. Each platform is supported by one or more "drivers", which know how to automate that particular platform. Choose a driver below for specific information about how that driver works and how to set it up:

2.从上面的信息可以看出目前1.7的android版可以支持:Espresso、UiAutomator2、UiAutomator、Selendroid四种驱动模式,后面两个不推荐用了,太老了,Espresso这个是最新支持的处于beta阶段,UiAutomator2是目前最稳的。

3.appium最新版本还能支持windows和mac的桌面app程序了,这个是否稳定,拭目以待!

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, "再按一次退出")

在学习过程中有遇到疑问的,可以appium+python QQ群交流:330467341

优质内容筛选与推荐>>
1、详解C/C++函数指针声明
2、Java 静态代码块和非静态代码块
3、如何配置才能让tomcat启动后出现jsp,html这些文件列表呢
4、SEO 如何优化单个页面16招
5、The 9th Zhejiang Provincial Collegiate Programming Contest->Problem :K-Yet Another Story of Rock-pap


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号