Automated Testing on Android Phones-part 2
By reading this tutorial, you will learn:
- How to use Poco to test Android native apps
Poco supports directly identifying the UI hierarchy of any Android native application (non-game engine, non-webview), and its usage is exactly the same as Poco on other platforms.
Note: webview-based applications are special (such as WeChat applets or browsers). For details, see the document How does Poco support WebView inspection。
Before you start¶
After selecting Android mode in AirtestIDE's
Poco Assistant panel, AirtestIDE will automatically install
Pocoservice-test.apk into the mobile phone. Some models of mobile phones require manual confirmation to install successfully.
After the installation is complete, AirtestIDE will automatically start
PocoService. It will periodically grab the hierarchy information of the interface on Android, and you can see the UI hierarchy tree of the device interface in AirtestIDE for a while.
Click any node on the UI tree, you can see all the attributes of the node in the Log panel. At the same time, a box will appear on the screen of the device, and the corresponding position box will be selected to facilitate node positioning.
Pocoservice fails to start, it will cause
Pocoservice.apk to be reinstalled repeatedly. At this time, you can check the following aspects:
- Is the Android version too low? Poco supports
Android SDK APIversion greater than or equal to 19, that is, Android 4.4 and above
- Please turn off the network proxy connected to your PC or mobile phone, otherwise Poco may not be connected
- You can try to uninstall 2 Pocoservice related APKs on your phone and reinstall them manually. You can find these two APKs in this directory
- Some vivo and oppo phones need to set the
Yosemiteinput method as the default input method and the current input method in the
phone settings-input method settings
An example using a calculator¶
Here we provide an example of writing code for a calculator application using Poco. Click here to download Sample App (calculator) and install this App on your phone beforehand.
As mentioned above, after selecting
Android in the Poco auxiliary window drop-down menu in AirtestIDE, AirtestIDE will automatically start
Pocoservice, displaying the current UI control hierarchy tree. After that, you can write and record Poco statements in AirtestIDE.
The following code example will demonstrate a simple function: click the calculator interface to implement a operation 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 ^^')
In this code, we initialize a poco object with
poco = AndroidUiautomationPoco (). Then we selected the
1 buttons for click operations. Then we used the
get_text interface to get the value 2 of the result control , and finally we used the assertion statement to verify the result.
This example is very simple. For more poco usage and examples, see poco tutorial。
Connect multiple phones simultaneously and use Poco¶
In the previous tutorial (Multi-machine collaboration), we mentioned that a script can connect multiple Android phones, and use the
set_current interface to switch between phones:
from airtest.core.api import connect_device dev1 = connect_device("Android://127.0.0.1:5037/serialno1") # Connect to your first phone dev2 = connect_device("Android://127.0.0.1:5037/serialno2") # Connect to your second phone set_current(1)
If we want to use poco to get controls and click on two different phones after connecting to the 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 to your first phone poco1 = AndroidUiautomationPoco(dev1) dev2 = connect_device("Android://127.0.0.1:5037/serialno2") # Connect to your second phone poco2 = AndroidUiautomationPoco(dev2) set_current(1) # Switch to the second phone poco2('com.google.android.calculator:id/digit_1').click()
Similarly, if you do not use the
connect_device interface to connect the phone, but automatically connect the phone by passing
--device directly on the command line, you do not need to repeatedly execute
connect_device in the code.At this point, you only need to obtain the device object separately and initialize the poco with the device object:
from airtest.core.api import G print(G.DEVICE_LIST) # Suppose there are currently 2 mobile phones poco1 = AndroidUiautomationPoco(G.DEVICE_LIST) poco2 = AndroidUiautomationPoco(G.DEVICE_LIST)