python+Appium自动化:toast定位

 

Toast简介

Toast是一种简易的消息提示框。

当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。

用户将可能是在中间键入别的东西。Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。

而且Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失。

举个例子:下方图片就是淘宝退出app时出现的toast信息

 

####

查看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:

iOS
    The XCUITest Driver
    (DEPRECATED) The UIAutomation Driver
Android
    (BETA) The Espresso Driver
    The UiAutomator2 Driver
    (DEPRECATED) The UiAutomator Driver
    (DEPRECATED) The Selendroid Driver
The Windows Driver (for Windows Desktop apps)
The Mac Driver (for Mac Desktop apps)



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

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

###

toast定位

 

如果用 UI Automation Viewer这个工具是无法定位到的,那么如何进行定位呢?

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

这个主要是基于UiAutomator2,因此需要在Capablity配置如下参数:

'automationName''uiautomator2'

###

具体代码

from appium import webdriver
import unittest
import time
from selenium.webdriver.support.ui import WebDriverWait


class Test_Demo(unittest.TestCase):

    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '8.1.0'
        desired_caps['deviceName'] = '84B7N18130000106'
        desired_caps['noReset'] = 'true'  # 使用这个,就会记住上一次你的点击记录,
        desired_caps['appPackage'] = 'com.tencent.news'
        desired_caps['appActivity'] = 'com.tencent.news.activity.SplashActivity'
        desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['automationName'] = 'Uiautomator2'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

        time.sleep(8)

    def tearDown(self):
        pass
        # driver.quit()  #退出app

    def test_search_demo(self):
        time.sleep(1)
        # 点击返回按钮
        self.driver.back()

        # 用xpath定位
        toast_message = "再按一次退出腾讯新闻"
        message = '//*[@text=\'{}\']'.format(toast_message)

        # 显示等待检测元素
        toast_element = WebDriverWait(self.driver, 5).until(lambda x: x.find_element_by_xpath(message))
        print("toast_element.text", toast_element.text)
        # 结果进行比较
        assert toast_element.text == "再按一次退出腾讯新闻"


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

####

 
 
 
 
 
#####
posted @ 2021-12-01 03:30  技术改变命运Andy  阅读(150)  评论(0编辑  收藏  举报