Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with all kinds of pop-up windows in Android automated testing

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to deal with all kinds of pop-up windows in Android automation testing. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Types of pop-up windows:

There are generally two kinds of system pop-up windows when installing APP. One is that some APP needs to be installed for initializing the automated test box. For example, uiautomator2 will install atx-agent and com.github.uiautomator. These pop-up windows can be manually clicked when initializing the environment, and you don't need to pay attention to them in case. The other is to install our tested app, like the following

We have to deal with it, otherwise, automation will not be automatic. Permission pop-up window when APP starts

This kind of pop-up window is that APP will apply for some basic permissions when it starts.

Service pop-up window in APP

Pop-up window processing

This article uses the uiautomator2 automation framework, which provides a watcher object that can be used to configure the elements to be monitored. Here we configure to monitor the pop-up window on the page. Let's see how to do it.

The use of watcher

# commonly used words, register anonymous monitoring d.watcher.when ("install"). Click () # register a monitor named ANR, when ANR and Force Close appear Click Force Closed.watcher ("ANR") .when (xpath= "ANR"). When ("Force Close"). Click () # other callback examples d.watcher.when ("grab red packets"). Press ("back") d.watcher.when ("/ / * [@ text =" Out of memory "]") .call (lambda d: d.shell ("am force-stop com.im.qq")) # callback description def click_callback (d: u2.Device) : d.xpath ("OK") .click () # calling in the callback does not trigger watcherd.xpath ("continue") .click () # when using d.xpath to check the element Will trigger watcher (currently triggers up to 5 times # remove ANR's monitoring d.watcher.remove ("ANR") # remove all monitoring d.watcher.remove () # start background monitoring d.watcher.start () d.watcher.start (2.0) # default monitoring interval 2.0s# forces all monitoring d.watcher.run () # stop monitoring d.watcher.stop () # stop and remove all monitoring Commonly used to initialize d.watcher.reset ()

Above are some common api and explanations of watcher, derived from github. Hee hee, I'm too lazy to write.

Actual combat case

Let's take bilibili apk as an example to deal with a series of pop-up windows from installation to login.

Import uiautomator2 as u2import osimport timebase_dir = os.path.dirname (_ _ file__) apk_path = os.path.join (base_dir, "apks/bilibili.apk") d = u2.connect_usb (serial= "MDX0220924018819") # the pop-up window that may appear after installation and login is successful, register here This is the pop-up window type d.watcher.when ("continue installation"). Click () d.watcher.when ("complete"). Click () d.watcher.when ("agree and continue"). Click () d.watcher.when ("I know"). Click () d.watcher.start () d.app_install (apk_path) d.app_start ("tv.danmaku.bili") d (text= "my") .click () time.sleep (3) if d (resourceId= "tv.danmaku.bili:id/btn_change_account"). Exists: d (resourceId= "tv.danmaku.bili:id/btn_change_account"). Click () else: d (resourceId= "tv.danmaku.bili:id/tv_login"). Click () time.sleep (3) d (resourceId= "tv.danmaku.bili:id/username"). Set_text ("xxxxxxxxx") d (resourceId= "tv.danmaku") .bili: id/userpwd ") .set_text (" xxxxxxxx ") d (resourceId=" tv.danmaku.bili:id/log_reg_checkbox "). Click () time.sleep (2) d (resourceId=" tv.danmaku.bili:id/btn_login ") .click () d (text= homepage) .click ()

The core idea of pop-up window processing is to start a thread, constantly listen to whether there is a pop-up window on the page, click when it appears, or click cancel or click to confirm, and so on.

The core idea of dealing with pop-up window by uiautomator2

The method of running a thread in the background (relying on the threading library) is adopted, and then the hierarchy is dump at regular intervals, and the corresponding operation is performed after matching the element.

Class Watcher (): def _ _ init__ (self D: "uiautomator2.Device"): self._d = d self._watchers = [] self._watch_stop_event = threading.Event () self._watch_stopped = threading.Event () self._watching = False # func start is calling self._triggering = False self.logger = setup_logger () self.logger.setLevel (logging.INFO) def when (self, xpath=None): return XPathWatcher (self Xpath)

Watcher object has self._watchers attributes to maintain all the elements to be monitored. D.watcher.when ("continue installation") when we call the when method, we return a XPathWatcher object, and then call the object's click method to operate on the monitoring element.

Class XPathWatcher (): def _ init__ (self, parent: Watcher, xpath: str, name: str = ""): self._name = name self._parent = parent self._xpath_list = [xpath] if xpath else [] def when (self, xpath=None): self._xpath_list.append (xpath) return self def call (self, func): "" func accept argument, key (d) El) d=self._d, el=element "" self._parent._watchers.append ({"name": self._name, "xpaths": self._xpath_list, "callback": func }) def click (self): def _ inner_click (selector): selector.get_last_match () .click () self.call (_ inner_click)

The click method simply puts the clicked action into the callback function and then calls the call method of the XPathWatcher object, which generates a monitoring rule and places the monitoring rule on the self._watchers property of the Watcher object we mentioned earlier.

Def start (self, interval: float = 2.0): "stop watcher"if self._watching: self.logger.warning (" already started ") return self._watching = True th = threading.Thread (name=" watcher ", target=self._watch_forever, args= (interval,)) th.daemon = True th.start () return th

Then call the start method of the Watcher object, start a thread, dump the information from the page at the specified interval, see if there are any elements to be monitored, and call the callback function after finding it.

These are all the contents of the article "how to handle various pop-up windows in Android automated testing". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report