Skip to content

V. Poco Initialization for Different Platforms

1.Preface

As we all know, Poco is a cross-platform automated testing framework. For different platforms, such as Android, iOS, and unity engines, we need to initialize different Poco to operate on the controls on the corresponding platforms. This article details the Poco initialization script for different platforms.

2.Poco Initialization for Android

1) Single Android device

Before proceeding with Poco initialization for the Android, we need to make sure that one or more Android devices have been started up and connected properly:

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *
auto_setup(__file__,devices=["android://127.0.0.1:5037/127.0.0.1:7555"])

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

poco(text="Netease Cloud Music").click()

2) Multiple Android devices

If you are initializing multiple Android devices, you need to do the initialization separately:

# -*- encoding=utf8 -*-

__author__ = "AirtestProject"

from airtest.core.api import *
auto_setup(__file__,devices=["android://127.0.0.1:5037/127.0.0.1:7555", "android://127.0.0.1:5037/0123456789"])

from airtest.core.android import Android
dev1 = Android('127.0.0.1:7555')
dev2 = Android('0123456789')

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco1 = AndroidUiautomationPoco(dev1)
poco2 = AndroidUiautomationPoco(dev2)

poco1(text="Netease Cloud Music").click()
poco2(text="Netease Cloud Music").click()

3.Poco Initialization for iOS

Similar to the Poco initialization for Android devices, we need to make sure we have the iOS device connected before initializing the iOS Poco:

# -*- encoding=utf8 -*-

__author__ = "AirtestProject"

from airtest.core.api import *
auto_setup(__file__)

connect_device("iOS:///http://127.0.0.1:8100")

from poco.drivers.ios import iosPoco
poco = iosPoco()

1) Connect the device and then initialize Poco

If you initialize iOS Poco before connecting an iOS device, you will receive an error message of please call connect_device to connect an ios device first:

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *
auto_setup(__file__)

# Incorrect example!!!
from poco.drivers.ios import iosPoco
poco = iosPoco()

connect_device("iOS:///http://127.0.0.1:8100")

image-20211125175945938

4.Poco Initialization for Unity Project

1) Unity games on Android/ iOS devices

Before performing Poco initialization for unity game applications on Android/ iOS devices, make sure that it has Poco-SDK integrated beforehand. if the Poco-SDK has been integrated in the game, the Poco service will be started when the game starts. So, make sure that the game is fully launched before you initialize the unity Poco:

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *
auto_setup(__file__)

# Fully start the game
start_app("com.NetEase")
sleep(3.0) # Sleep for a certain amount of time to make sure the game is fully started

from poco.drivers.unity3d import UnityPoco
poco = UnityPoco()

When writing the Poco initialization script for game applications, many users tend to mess up the order of scripts for connecting to the device, initializing the Poco and starting the application, resulting in a bunch of errors.

The most common errors include: "The remote host forcibly closed an existing connection", "socket connection broken", etc.

The correct order should be: connect the device first (usually using the auto_setup API) --> start the application (usually using the start_app API) --> after the application is fully started, initialize the Poco.

2) Unity games on Windows

For a unity game running on Windows (with prior integration with the Poco-SDK), we can initialize its Poco as follows:

from poco.drivers.unity3d import UnityPoco
from airtest.core.api import connect_device

# Use regular expression to match a Windows window
dev = connect_device('Windows:///?title_re=^your game title.*$')

# make sure your poco-sdk in the game runtime listens on the following port.
# default value will be 5001
# IP is not used for now
addr = ('', 5001)

# Specify the device object to initialize unity Poco
poco = UnityPoco(addr, device=dev)

ui = poco('...')
ui.click()

3) UnityEditor on Windows

Poco also supports direct connection to UnityEditor for debugging. Initialize the Poco is as follows (please make sure the game project has Poco-SDK integrated and the game has been started properly):

from poco.drivers.unity3d import UnityPoco
from poco.drivers.unity3d.device import UnityEditorWindow

# specify to work on UnityEditor in this way
dev = UnityEditorWindow()

# make sure your poco-sdk component listens on the following port.
# Default value will be 5001. Change to any other if you like.
# IP is not used for now
addr = ('', 5001)

# Specify the device object to initialize unity Poco
poco = UnityPoco(addr, device=dev)

ui = poco('...')
ui.click()

4) Poco initialization for multi-device unity project

If you have multiple devices or multiple device objects connected, you need to initialize a Poco for the unity project on each device (make sure each of them has the Poco-SDK integrated and the unity game started):

from poco.drivers.unity3d import UnityPoco
from poco.drivers.unity3d.device import UnityEditorWindow

# Initialize different device object one by one
dev1 = UnityEditorWindow()
dev2 = connect_device('Android:///')
dev3 = connect_device('Windows:///?title_re=^title xxx.*$')

# Use this default address. Separate them if the devices do not listen on the same port.
addr = ('', 5001)

# Initialize Poco instance one by one by specifying different device objects
poco1 = UnityPoco(addr, device=dev1)
poco2 = UnityPoco(addr, device=dev2)
poco3 = UnityPoco(addr, device=dev3)

ui1 = poco1('...')
ui1.click()
ui2 = poco2('...')
ui2.swipe('up')

5.Poco Initialization for Cocos2dx-lua Project

After integrating the Poco-SDK into the cocos2dx-lua project, we usually build an Android package for testing. After starting the game on an Android device, initialize the Poco as follows:

from poco.drivers.std import StdPoco
poco = StdPoco()

# Or pass in the specified device object

from poco.drivers.std import StdPoco
from airtest.core.api import connect_device
device = connect_device('Android:///')
poco = StdPoco(10054, device)
ui = poco('...')
ui.click()

6.Poco Initialization for Cocos2dx-js Project

As with the cocos2dx-lua project, after launching a cocos2dx-js game with Poco-SDK integrated on an Android device, we can initialize Poco in the following way:

from poco.drivers.cocosjs import CocosJsPoco
poco = CocosJsPoco()

7.Poco Initialization of Egret Project

1) Connect to a mobile browser

After configuring the environment, you can open an Egret page (or a WeChat mini program/game) on a mobile browser and initialize Poco through the following method:

from poco.drivers.std import StdPoco
from poco.utils.device import VirtualDevice
poco = StdPoco(15004, VirtualDevice('localhost'))

2) Connect to Windows desktop browser

As above, after configuring the environment and connecting to the desktop browser, you can initialize Poco as follows:

from poco.drivers.std import StdPoco
poco = StdPoco()

8.Poco Initialization for UE4 Project

After the device is connected and the UE4 project is started (with the Poco-SDK integrated beforehand), you can initialize Poco as follows:

from poco.drivers.ue4 import UE4Poco
poco = UE4Poco()

# Example
poco("StartButton").click()