app自动化测试——特殊控件Toast

在这里插入图片描述


一、Toast弹窗是什么

一种消息框类型
无法被点击
Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失
是系统级别的控件,属于系统settings
Toast类的思想:
就是尽可能不引人注意,同时还向用户显示信息,希望他们看到

二、Toast 定位

appium使用uiautomator底层的机制来分析抓取toast,并且把toast放到控件树里面,但本身并不属于控件。
automationName: uiautomator2
getPageSource是无法找到的
必须使用xpath查找

方式一:

self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text,'popup menu')]")

方式二

self.driver.find_element(AppiumBy.XPATH,"//android.widget.Toast[@class='android.widget.Toast']")

三、案例

toast弹窗如下
在这里插入图片描述

使用uiautomatorviewer进行定位

在这里插入图片描述
自动打开UI Automator Viewer界面

在这里插入图片描述

caps["automationName"]='uiautomator2' #todo 安卓工作引擎

import time
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
class TestXueqiu:
    def setup(self):
        caps = {
    
    }
        caps["platformName"] = "Android"
        caps["deviceName"] = "127.0.0.1:7555 device"
        caps["appPackage"] = "io.appium.android.apis"
        caps["appActivity"] = "io.appium.android.apis.view.PopupMenu1"
        caps["noReset"] = "true"
        caps["ensureWebviewsHavePages"] = True
        caps["automationName"]='uiautomator2'   #todo 安卓工作引擎
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(8)

    def teardown(self):
        self.driver.quit()

    def test_toast(self):
        self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Make a Popup!").click()
        self.driver.find_element(AppiumBy.ID,"android:id/title").click()
        print(self.driver.page_source)
        #todo xpath
        #text=self.driver.find_element(AppiumBy.XPATH,"//android.widget.Toast[@class='android.widget.Toast']").text
        #print(text)
        #todo text
        text=self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text,'popup menu')]").text
        print(text)

1、特别注意

如果不想让脚本从头开始执行脚本,忽略之前的点击操作,直接进入目标页面,
可以获取当前页面的appActivity
使用命令行:adb shell dumpsys window | grep mCurrent获取

猜你喜欢

转载自blog.csdn.net/YZL40514131/article/details/129637799