Dry goods APP automation Android special control Toast recognition

Posted by audiomove on Mon, 03 Jan 2022 15:11:11 +0100

This article is excerpted from the internal textbook of Hogwarts Testing Institute

Toast is a message box type in the Android system. It is a lightweight message prompt. It often appears in the form of a small pop-up box, usually 1 to 2
Seconds will disappear automatically and can appear anywhere on the screen. Unlike Dialog, it has no focus. Toast
The design idea is not to attract attention as much as possible, but also to display information to users, hoping they can see it.

Test APP download address:


First, download the apk package at the above address to the local and install it into the simulator; Open API Demos in the simulator and click "Views" - "Popup Menu" - "Make" successively
A popup - "search", a message prompt box will pop up, as shown in the figure:

In the above figure, "Clicked Popup menu item Search" is a Toast, but it usually stays on the page for only about 2 seconds through Appium
It is generally not easy for inspectors to obtain this element.

Get Toast

Open the API Demos application in the simulator and click "Views" - "Popup Menu" - "Make a Popup"-"Search" to view the page
Toast element.

Set capabilities

caps = {}caps["platformName"] = "android"caps["deviceName"] = "TestToast"caps["appPackage"] = "io.appium.android.apis"caps["appActivity"] = ".ApiDemos"#You must use the uiautomator2 framework caps["automationName"] = "uiautomator2"caps["udid"] = ""  
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

Write test case code

# click Viewsdriver.find_element_by_accessibility_id("Views").click()time.sleep(2)# Slide page TouchAction(driver).long_press(380, 1150).move_to(380, 150).release().perform()# click `Popup Menu` project driver.find_element_by_xpath(  "//*The content of the content has taken a descdesc = 'popup menu') ')').Clip () is a way to take the rest (2) of the past '' '''content content content content ''descdescducducducducduccontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent() () is a way to take the rest of the rest' '' '' '' '' '' '' '' '' '''s way way to take a pop in making making make it a way to make it a way to make it a way to make a point where where where content content content content has taken a descdescdescdescdescdescdescdesctaken taken taken to make making a pop pop pop pop in making a pop pop pop pop pop pop pop up itself itself itself itself way way way way way way way way way way of the way way of the way way way way way way of the way way way way of the content content u by_xpath (toastXPath)) # print the textprint (driver.find_element_by_xpath (toastXPath) obtained by toastXPath text)

Here, the Xpath expression is used to locate Toast because the class of Toast
Attribute is special. The element of class="android.widget.Toast" usually appears on the current page, so Xpath is used
Positioning mode, it is easy to locate.

View execution results

The message prompts that the Toast acquisition method is over. Have you learned it? In the next issue, we will explain attribute acquisition and assertion in app automation. Interested partners can pay attention to it!

** _
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information