Automated Testing on Android Phones-part 2
Foreword
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 installPocoservice.apk
and 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.
If Pocoservice
fails to start, it will causePocoservice.apk
to be reinstalled repeatedly. At this time, you can check the following aspects:
- Is the Android version too low? Poco supports
Android SDK API
version 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
poco\ poco\drivers\android\lib
- Some vivo and oppo phones need to set the
Yosemite
input method as the default input method and the current input method in thephone 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 startPocoservice
, displaying the current UI control hierarchy tree. After that, you can write and record Poco statements in AirtestIDE.
Code example
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
+
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[0])
poco2 = AndroidUiautomationPoco(G.DEVICE_LIST[1])