def __init__(self, addr=DEFAULT_ADDR): # init airtest env try: # new version from airtest.core.api import connect_device, device as current_device if not current_device(): connect_device("Android:///") except ImportError: # old version from airtest.cli.runner import device as current_device from airtest.core.main import set_serialno if not current_device(): set_serialno() # cocos games poco sdk listens on Android localhost:5003 localport = addr.split(":")[-1] current_device().adb.forward("tcp:%s" % localport, "tcp:5003", False) self.conn = WebSocketClient(addr) self.c = RpcClient(self.conn) self.c.DEBUG = False self.c.run(backend=True) self.c.wait_connected() hierarchy = FreezedUIHierarchy(Dumper(self.c)) screen = AirtestScreen() input = AirtestInput() super(CocosJsPocoAgent, self).__init__(hierarchy, input, screen, None)
def __init__(self, addr=DEFAULT_ADDR, unity_editor=False): if not unity_editor: # init airtest env try: # new version from airtest.core.api import connect_device, device as current_device if not current_device(): connect_device("Android:///") except ImportError: # old version from airtest.cli.runner import device as current_device from airtest.core.main import set_serialno if not current_device(): set_serialno() # unity games poco sdk listens on Android localhost:5001 current_device().adb.forward("tcp:%s" % addr[1], "tcp:5001", False) self.conn = TcpClient(addr) self.c = RpcClient(self.conn) self.c.DEBUG = False self.c.run(backend=True) self.c.wait_connected() hierarchy = FreezedUIHierarchy(Dumper(self.c), UnityAttributor(self.c)) if unity_editor: screen = UnityScreen(self.c) else: screen = AirtestScreen() input = AirtestInput() super(UnityPocoAgent, self).__init__(hierarchy, input, screen, None)
def preload_airtest_device_context(run_on_win=False, window_title='^.*errors and.*$'): from airtest.cli.runner import device as current_device if not current_device(): from airtest.core.main import set_serialno, set_windows from airtest.core.settings import Settings from airtest.core.android.adb import ADB adb_client = ADB() available_devices = adb_client.devices('device') if run_on_win or not available_devices: Settings.FIND_INSIDE = [8, 30] # 窗口边框偏移 set_windows(window_title=window_title) else: set_serialno() exec("from airtest.core.main import *") in globals()
def __init__(self, addr=DEFAULT_ADDR, unity_editor=False, connect_default_device=True): # init airtest env try: # new version from airtest.core.api import connect_device, device as current_device from airtest.core.helper import device_platform if unity_editor and not current_device(): connect_device( "Windows:///?class_name=UnityWndClass&title_re=Unity.*") game_window = current_device().app.top_window().child_window( title="UnityEditor.GameView") current_device()._top_window = game_window.wrapper_object() current_device().focus_rect = (0, 40, 0, 0) if connect_default_device and not current_device(): # currently only connect to Android as default # can apply auto detection in the future connect_device("Android:///") if device_platform() == "Android": # always forward port for Android # unity games poco sdk listens on Android localhost:5001 current_device().adb.forward("tcp:%s" % addr[1], "tcp:5001", False) except ImportError: # old version, 逐渐废弃 from airtest.cli.runner import device as current_device from airtest.core.main import set_serialno if not current_device(): set_serialno() # unity games poco sdk listens on Android localhost:5001 current_device().adb.forward("tcp:%s" % addr[1], "tcp:5001", False) self.conn = TcpClient(addr) self.c = RpcClient(self.conn) self.c.DEBUG = False self.c.wait_connected() hierarchy = FrozenUIHierarchy(Dumper(self.c), UnityAttributor(self.c)) screen = UnityScreen(self.c) input = AirtestInput() super(UnityPocoAgent, self).__init__(hierarchy, input, screen, None)
def __init__(self, device=None, using_proxy=True, **options): if not device: try: # new version from airtest.core.api import connect_device, device as current_device if not current_device(): connect_device("Android:///") except ImportError: # old version from airtest.cli.runner import device as current_device from airtest.core.main import set_serialno if not current_device(): set_serialno() self.android = current_device() else: self.android = device self.adb_client = self.android.adb if using_proxy: self.device_ip = self.adb_client.host or "127.0.0.1" else: if new_airtest_api: self.device_ip = self.adb_client.get_ip_address() else: self.device_ip = get_ip_address(self.adb_client) # save current top activity (@nullable) current_top_activity_package = self.android.get_top_activity_name() if current_top_activity_package is not None: current_top_activity_package = current_top_activity_package.split( '/')[0] # install ime self.ime = YosemiteIme(self.adb_client) self.ime.start() # install self._instrument_proc = None self._install_service() # forward if using_proxy: p0, _ = self.adb_client.setup_forward("tcp:10080") p1, _ = self.adb_client.setup_forward("tcp:10081") else: p0 = 10080 p1 = 10081 # start if self._is_running('com.github.uiautomator'): warnings.warn( '{} should not run together with "uiautomator". "uiautomator" will be killed.' .format(self.__class__.__name__)) self.adb_client.shell( ['am', 'force-stop', 'com.github.uiautomator']) ready = self._start_instrument(p0) if not ready: # 启动失败则需要卸载再重启,instrument的奇怪之处 uninstall(self.adb_client, PocoServicePackage) self._install_service() ready = self._start_instrument(p0) if current_top_activity_package is not None: current_top_activity2 = self.android.get_top_activity_name() if current_top_activity2 is None or current_top_activity_package not in current_top_activity2: self.android.start_app(current_top_activity_package, activity=True) if not ready: raise RuntimeError("unable to launch AndroidUiautomationPoco") endpoint = "http://{}:{}".format(self.device_ip, p1) agent = AndroidPocoAgent(endpoint, self.ime) super(AndroidUiautomationPoco, self).__init__(agent, **options)
from poco.vendor.android.uiautomation import AndroidUiautomationPoco class AndroidNativeUITestCase(PocoTestCase): @classmethod def setUpClass(cls): super(AndroidNativeUITestCase, cls).setUpClass() cls.poco = AndroidUiautomationPoco() # 启用动作捕捉(action tracker)和游戏运行时日志捕捉插件(runtime logger) action_tracker = ActionTracker(cls.poco) cls.register_addin(action_tracker) class T1(AndroidNativeUITestCase): def runTest(self): self.poco(text='设置').click() self.poco(text='WLAN').click() if __name__ == '__main__': from airtest.cli.runner import device as current_device from airtest.core.main import set_serialno if current_device() is None: set_serialno() import pocounit pocounit.main()