网络知识 娱乐 技术分享 | app自动化测试(Android)-- 特殊控件 Toast 识别

技术分享 | app自动化测试(Android)-- 特殊控件 Toast 识别

{"data":{"title":"技术分享 | app自动化测试(Android)-- 特殊控件 Toast 识别","abstract":"本文节选自霍格沃兹测试开发学社内部教材Toast 是 Android 系统中的一种消息框类型,它属于一种轻量级的消息提示,常常以小弹框的形式出现,一般出现 1 到 2 秒会自动消失,可以出现在屏幕上中下任意位置。它不同于 Dialog,它没有焦点。","cover":"https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/233311a77437481fa263163957bac034","articleType":"article","itemId":"7112664201323561512","groupId":"7112664201323561512","groupSource":2,"isOriginal":false,"banComment":false,"publishTime":"2022-06-24 12:57","source":"CeshirenTester","tag":"technique","mediaSite":null,"pathname":"/article/7112664201323561512/","loginUserInfo":null,"favorite":false,"relation":{"isFollowing":false,"isFollowed":false},"likeData":{"userLikeStatus":0,"count":0},"isSelf":false,"content":"

本文节选自霍格沃兹测试开发学社内部教材

Toast 是 Android 系统中的一种消息框类型,它属于一种轻量级的消息提示,常常以小弹框的形式出现,一般出现 1 到 2 秒会自动消失,可以出现在屏幕上中下任意位置。它不同于 Dialog,它没有焦点。Toast 的设计思想是尽可能的不引人注意,同时还向用户显示信息希望他们看到。

测试 APP 下载地址:

https://github.com/appium/sample-code/raw/master/sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk 1

首先将上面地址的 apk 包下载到本地,并安装到模拟器中;在模拟器中打开 API Demos,依次点击“Views”-“Popup Menu”-“Make a Popup”-“Search”,就会弹出消息提示框,如图:

技术分享

上图中 “Clicked popup menu item Search” 就是 Toast,但它通常在页面上停留的时间只有 2 秒左右,通过 Appium Inspector 一般不容易获取到这个元素。

获取Toast

在模拟器中打开 API Demos 应用,依次点击 “Views”-“Popup Menu”-“Make a Popup”-“Search”,查看页面 Toast 元素。

示例代码如下:

# 设置 capabilitiesncaps = {}ncaps["platformName"] = "Android"ncaps["appPackage"] = "io.appium.android.apis"ncaps["appActivity"] = ".ApiDemos"n#必须使用uiautomator2框架ncaps["automationName"] = "uiautomator2"ncaps["deviceName"] = "hogwarts"n# 与Appium Server 建立连接ndriver = webdriver.Remote("http://localhost:4723/wd/hub", caps)n# 设置隐式等待ndriver.implicitly_wait(5)nn# 点击 Viewsndriver.find_element_by_accessibility_id("Views").click()n# 滑动页面nTouchAction(driver).press(380, 1150)n .move_to(380, 150).release().perform()n# 点击 `Popup Menu` 项目ndriver.find_element_by_xpath(n "//*[@content-desc='Popup Menu']").click()n# 点击 `Make a Popup`ndriver.find_element_by_xpath(n "//*[@content-desc='Make a Popup!']").click()n# 点击 'Search'ndriver.find_element_by_xpath("//*[contains(@text,'Search')]").click()ntoastXPath = "//*[@class='android.widget.Toast']"n#打印 toastXPathnprint(driver.find_element_by_xpath(toastXPath))n#打印 toastXPath 获取的 textnprint(driver.find_element_by_xpath(toastXPath).text)nn

@BeforeAllnpublic static void setUp() throws MalformedURLException {n DesiredCapabilities desiredCapabilities = new DesiredCapabilities();n desiredCapabilities.setCapability("platformName", "Android");n desiredCapabilities.setCapability("appPackage", "io.appium.android.apis");n desiredCapabilities.setCapability("appActivity", ".ApiDemos");n desiredCapabilities.setCapability("automationName", "uiautomator2");n desiredCapabilities.setCapability("deviceName", "hogwarts");nn URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");n driver = new AndroidDriver(remoteUrl, desiredCapabilities);n driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);n}nn@Testnpublic void toastTest() {n //点击 Viewsn driver.findElement(MobileBy.AccessibilityId("Views")).click();n //滑动页面n TouchAction action = new TouchAction(driver);n PointOption pressPointOne = PointOption.point(380, 1150);n PointOption movePointOne = PointOption.point(380, 150);n action.press(pressPointOne).moveTo(movePointOne).release();n //点击 `Popup Menu` 项目n driver.findElement(By.xpath("//*[@content-desc='Popup Menu']")).click();n //点击 `Make a Popup`n driver.findElement(By.xpath("//*[@content-desc='Make a Popup!']")).click();n //点击 'Search'n driver.findElement(By.xpath("//*[contains(@text,'Search')]")).click();n By toastXPath = By.xpath("//*[@class='android.widget.Toast']");n //打印 toastXPathn System.out.println(driver.findElement(toastXPath));n //打印 toastXPath 获取的 textn System.out.println(driver.findElement(toastXPath).getText());n}nn

这里定位 Toast 使用了 Xpath 表达式进行定位,因为 Toast 的 class 属性比较特殊,在当前页面上一般会出现一次 class=“android.widget.Toast” 的元素,所以使用 Xpath 定位方式搭配隐式等待就可以很轻松的可以定位到。

查看执行结果

技术分享

获取更多相关资料戳:https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=jianshu&timestamp=11656046471&author=wuyue

","imageList":["https://p26.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/adc068b6aaae42a0bbc2cac60d932a75?from=pc","https://p26.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/40a4d65fdcc741d0aebc2b6b6cb3158e?from=pc"],"mediaInfo":{"userId":"MS4wLjABAAAArmAijQN5Bzl6TZpizuExYrxrMCOyo9WweQIRx1SPZLE7aUfry3RumkNNAitBOGGj","unsafeUserId":"1693696545000733","name":"CeshirenTester","avatarUrl":"https://p26.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/4d74b73c1c104cd1bfab54d3a94270d0","description":"","userVerified":1},"seoTDK":{"title":"技术分享 - app自动化测试(Android)- 特殊控件 Toast 识别-今日头条","description":"本文节选自霍格沃兹测试开发学社内部教材Toast 是 Android 系统中的一种消息框类型,它属于一种轻量级的消息提示,常常以小弹框的形式出现","keywords":"Android,技术,DIALOG,设计","publishTimestamp":"1656046641","modifiedTimestamp":"1656047853"},"logId":"20220624135013010129097147248437FA","sylpageConfig":{"card":{"id":""}},"identity":{"web_id":"7111199547455817229","user_is_login":false},"abtestInfo":{"rsp_type":5,"version_name":"4252807,4164637","parameters":{"feat_repost_type":{"new":true},"home_nav_conf":{"dcd_out":1},"local_filter":{"core_filter":{"filter_list":{"ms::TicaiFilter":true}}},"page_upgrade":{"new_profile":true,"video_double_column":true},"sati":{"enable_ad_prime":true,"enable_sorter_optimus":true,"prime_rule_rank_version":"toutiao_web","use_toutiao_web_feed":true,"format_max_consecutive_middle":2,"format_max_consecutive_nogroups":3,"enable_reduce_nogroup":true},"seraph":{"score_rule":{"default":{"replace":{"group_util":"_CTR - 1000*dislike"}},"new_user":{"replace":{"group_util":"_CTR - 1000*dislike"}}}},"sort":{"allowed_ticai":["forum_post","pgc_text","pgc_video"]},"video_detail_page_upgrade":{"new_page":true}},"env_flag":0},"localCityInfo":{"name":"北京","code":"110000","channelId":3202164529},"showResearch":false}}