VIII. Automated Testing for WeChat Mini Programs and Mini Games¶
In addition to supporting native applications, games, and web, the Airtest Project automated testing solution also supports the latest WeChat mini programs and mini games. In this article, we will take a look at how to use the Airtest Project for automated testing of mini programs and mini games respectively.
1.Mini Programs¶
For WeChat mini programs, we can use Airtest's image recognition method for automated testing or directly use the Poco of the native platform for control recognition. You can refer to the official documentation for information on the kernel used by WeChat mini programs. We have tested the latest version (V8.0.10) of WeChat which allows the use of Android or iOS poco mode to recognize controls of the mini programs. If you are unable to use it, it may be due to Webview kernel configuration on some Android devices. Please refer to this documentation for modification. Let's take a look at the effects on Android and iOS respectively:
1) Android mini programs¶
Connect your Android phone and open a WeChat mini program. Taking the Starbucks mini program as an example, in the Airtest IDE, select the Android mode to recognize controls on the page:
The following code can click on the control with a text attribute of "Taste of Autumn" on the Starbucks mini program:
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
poco(text="Taste of Autumn").click()
2) iOS mini programs¶
Connect your iOS phone and open a WeChat mini program. Taking the Starbucks mini program as an example, in the Airtest IDE, select the iOS mode to recognize controls on the page:
The following code can implement the operation of clicking on the corresponding card surface on iOS:
from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.ios import iosPoco
poco = iosPoco()
while not poco("Thank you for being here.").exists():
poco.scroll(direction='vertical', percent=0.3, duration=1.0)
snapshot()
poco("Thank you for being here.").click()
2.Mini Games¶
Airtest can use image recognition to perform automated testing on mini games. In addition, Poco UI control retrieval also supports mini games developed using the Egret engine. Poco integration requires integrating the Poco-SDK in the source code. The specific integration guide is as follows:
1) Environment configuration¶
- First, download the Poco-SDK package.
- Modify the modules property in egretProperties.json, and add the corresponding field in the red box, where the name property must be Poco.
- The path can be a relative path or an absolute path. For details, please refer to the instructions on the modules field in the Egret Engine documentation.
- Then use the shortcut key ctrl+`call out the terminal to execute the command egret build -e in the terminal.
- Create a new PocoManager object in the rungame function of the entry file main.ts, and pass in this.stage as a parameter.
- Run the code to start the game.
- Finally, enter "python -m poco.utils.net.stdbroker ws://:5003 tcp://:15004" in the terminal to open the proxy server broker.
- The websocket port defaults to 5003. If you need to change it, you can pass in the port parameter when creating a new pocomanager.
2) Connecting to AirtestIDE¶
AirtestIDE supports connecting to Android and iOS phones. You can open an Egret page (or WeChat mini program/game) on the phone, and then connect it through AirtestIDE. Both the Windows and MacOS versions of the IDE can be used. The basic principle of the connection is:
The specific steps are as follows:
- Connect your Android phone via USB, confirm that adb devices is connected, and execute the following command on your computer to map the phone's 5003 port to the computer's 5003 port.
adb reverse tcp:5003 tcp:5003
- Start broker on your computer:
python -m poco.utils.net.stdbroker "ws://*:5003" "tcp://*:15004"
- After starting the broker, access the Egret page on the phone (in full-screen mode to ensure accurate coordinates). When accessing, the broker will print the logs of the phone connection, as follows:
~ python -m poco.utils.net.stdbroker "ws://*:5003" "tcp://*:15004"
server listens on ("0.0.0.0", 5003) transport websocket
server listens on ("0.0.0.0", 15004) transport socket
StdBroker on.
server on accept. <poco.utils.net.transport.ws.MyWsApp object at 0x1100620d0>
- Open AirtestIDE and connect to the phone, select the poco Std-broker mode, and you will be able to see the UI hierarchy. At the same time, the broker will print the connection logs:
server on accept. <poco.utils.net.transport.ws.MyWsApp object at 0x10d03d2d0>
accept from: ('127.0.0.1', 56872)
received_message from ('127.0.0.1', 56323) {"id":"2a0ce828-132e-4d15-a645-55493d7eaf4b","jsonrpc":"2.0","result":{"children"
The AirtestIDE interface looks like this:
Then you can write and run scripts in AirtestIDE, as well as view the report.