This is the third day of my participation in the First Challenge 2022

There are many mobile app automation frameworks, but some have not been fully popular because they do not support mixed application testing. A typical example is the classic Python framework UIAutomator2. This framework is simple and easy to use, without API calls as complex as Appium, which is favored by many Python automation engineers.

This article provides a very simple way to make UIAutomator2 support mixed application testing with just four more lines of code.

What are hybrid apps

There are two typical development methods for mobile terminal applications, one is native APP and the other is Web app based on web development technology. Native apps have a better experience, but if you want to develop both Android and ios apps, you need different native development techniques. Web apps can easily be developed across Android and ios platforms, but the experience is a little less smooth than native apps.

Hybrid App is a development mode between Web App and native App. It can nest web pages in the native interface, so it can have both experience and cross-platform capability.

At present, the mainstream mobile app testing framework Appium has the ability of mixed application testing, but this framework is difficult to build and use, and its encapsulation method is not pythonic, so many companies do not want to use it. They prefer the simple and elegant Python UIAutomator2 framework. Unfortunately, the framework does not currently support mixed application testing.

Mix application test steps

  • The first step, through the native operation into the Webview web page;
  • Step 2, use Selenium and other web testing tools to enter the web page;
  • Third, test using a web testing tool like Selenium.

The key step in this process is how to use Selenium. If you open a new Selenium session directly, a new page will be opened, separate from the WebView in your app, so you cannot measure the nested web page. Selenium has to have some kind of relationship with the APP that binds them together, and working with Selenium means working directly with the web pages in the App.

Enter the WebView through UIAutomator2

Here is the most basic UIAutomator2 operation, detailed operation can be viewed in the official document, the app used here is Android Bootstrap, you can directly click download.

d = u2.connect()
d.app_start('com.github.android_app_bootstrap')
d(text='Login').click()
d(text='Baidu').click()
Copy the code

The selenium connection webview

The third menu of APP can directly open baidu web page, which leads to WebView web page. If you use UIAutomator2 directly to locate elements in a web page without using selenium connections, an error will be reported. How do YOU connect to webView via Selenium?

  • First, device D obtains the app of the current operation to get the package name
  • Then, initialize a Chrome browser object
  • Note that the Chrome object needs to add additional parameters, specifying the device to connect to, the webView binding to sign up for, and the configuration to use the Android browser in the app.

After that, there is no difference between Selenium web automation testing and clicking on a web page:

driver = webdriver.Chrome('chromedriver_68.exe', options=options)
driver.implicitly_wait(8)

driver.find_element('id'.'index-kw').send_keys('hello')
Copy the code

Where did I learn this code

Curious, how did I know I had to write code like this to connect to the WebView and test it? In fact, there are not so many mysterious learning methods in the world. As long as you are willing to pay attention, you must be able to find the path of learning. This code is clearly in the official Chrome WebDriver documentation, but you probably won’t be able to open it. Take a screenshot:

You can also view the configuration of other WebDriversMDN web docs.

Display effect:

If you need the complete code and toolkit, feel free to reply to [U2 Webview] in the comments section.