This article from the official public project number: AirtestProject the original address: airtest.doc.io.netease.com/tutorial/5_… Copyright notice: permission to reprint, but reprint must retain the original link

preface

As you read through this tutorial, you will learn:

  • How do I use Poco to test native Android apps

Poco supports UI hierarchy recognition directly for any Android native application (not a game engine, not a Webview), just as Poco does for other platforms.

Note: WebView-based applications are special (such as wechat applets or browsers), please see the documentation on how Poco supports WebView view.

Preparation before starting

After selecting Android mode in the Poco Assistant panel of the AirtestIDE, the AirtestIDE will automatically install pocoService-apk and Pocoservice-test.apk into your phone. Some mobile phones need to be manually clicked to confirm installation.

After the installation is complete, the AirtestIDE will automatically start the PocoService and grab the Android interface hierarchy information periodically. You can see the UI hierarchy tree of the current interface in the AirtestIDE interface.

Click any node in the UI tree, and you can see all the attributes of the node in the Log panel. At the same time, a box will appear on the device screen, and the corresponding position box will be selected to facilitate node positioning.

If Pocoservice fails to start, pocoService. apk will be reinstalled repeatedly. In this case, check the following:

  • Whether the Android version is too early, Poco supportAndroid SDK API≥ 19, that is, Android 4.4 or above
  • Disable the network Proxy connected to the PC or mobile phone. Otherwise, the Poco may fail to be connected
  • You can try to uninstall the APK related to the 2 Pocoservice in the phone and manually reinstall it againpoco\poco\drivers\android\libThe two APKs can be found in the directory
  • Some Vivo and Oppo phones need to put the phone Settings – input method Settings, putYosemiteInput method Set this parameter to the default input method and the current input method

An example of using a calculator

Here we provide an example of writing code for a calculator application using Poco. Click here to download the sample App and install it on your phone beforehand.

As mentioned above, by selecting Android from the DROP-DOWN menu of the Poco assist window in the AirtestIDE, the AirtestIDE will automatically launch the Pocoservice and display the current UI control hierarchy tree. You can then write and record a Poco statement in the AirtestIDE.

Code sample

The following code example demonstrates a simple function: Click on the Calculator screen to verify 1+1=2.

from poco.drivers.android.uiautomation import AndroidUiautomationPoco

poco = AndroidUiautomationPoco()

poco('com.google.android.calculator:id/digit_1').click()
poco('com.google.android.calculator:id/op_add').click()
poco('com.google.android.calculator:id/digit_1').click()
poco('com.google.android.calculator:id/eq').click()

result = poco('com.google.android.calculator:id/formula').get_text()
assert_equal(result, '2'.'1 + 1 = 2 ^ ^')
Copy the code

In this code, we use poCO = AndroidUiautomationPoco() to initialize a POCO object, and then select the 1 + 1 button to click on it, using the get_text interface very easily get the value of the resulting control 2. And use assertion statements to verify the results.

This example is very simple, and more poCO usage and examples are in the POCO Tutorial.

Connect multiple phones at the same time to use Poco

In the last tutorial (how to test on An Android phone (PART 1) – Multi-phone Collaboration), we mentioned that a script can connect to multiple Android phones and switch between them using the set_current interface:

from airtest.core.api import connect_device
dev1 = connect_device("Android: / / 127.0.0.1:5037 / serialno1")  # Connect your first phone
dev2 = connect_device("Android: / / 127.0.0.1:5037 / serialno2")  # Second phone
set_current(1)
Copy the code

If we want to use poCo to retrieve controls and click on two different phones, we need to initialize the two POCo separately, like this:

from airtest.core.api import connect_device
dev1 = connect_device("Android: / / 127.0.0.1:5037 / serialno1")  # Connect your first phone
poco1 = AndroidUiautomationPoco(dev1)
dev2 = connect_device("Android: / / 127.0.0.1:5037 / serialno2")  # Second phone
poco2 = AndroidUiautomationPoco(dev2)

set_current(1)  # Cut to the second phone
poco2('com.google.android.calculator:id/digit_1').click()
Copy the code

Similarly, if instead of using the CONNECt_device interface to connect to the phone, you automatically connect to the phone by passing –device directly on the command line, you don’t need to repeat connect_device in your code. We need to get the device objects and initialize the POCO using the device objects:

from airtest.core.api import G

print(G.DEVICE_LIST)  # Assume that there are currently two mobile phones in total
poco1 = AndroidUiautomationPoco(G.DEVICE_LIST[0])
poco2 = AndroidUiautomationPoco(G.DEVICE_LIST[1])
Copy the code

For more information about the tutorials and the project, please visit our official account AirtestProject to check out the previous tutorials: