|
11 | 11 | from poco.utils.simplerpc.rpcclient import RpcClient
|
12 | 12 | from poco.utils.simplerpc.transport.tcp.main import TcpClient
|
13 | 13 | from poco.utils.simplerpc.utils import sync_wrapper
|
| 14 | +from poco.utils.device import default_device |
14 | 15 |
|
15 | 16 | from airtest.core.api import connect_device, device as current_device
|
16 | 17 | from airtest.core.helper import device_platform
|
@@ -70,38 +71,41 @@ class StdPoco(Poco):
|
70 | 71 | device = connect_device('Android:///')
|
71 | 72 | poco = StdPoco(10054, device)
|
72 | 73 |
|
| 74 | + # or use ip:port to initialize poco object |
| 75 | + poco = StdPoco(port=10054, ip='xx.xx.xx.xx') |
| 76 | +
|
73 | 77 | # now you can play with poco
|
74 | 78 | ui = poco('...')
|
75 | 79 | ui.click()
|
76 | 80 | ...
|
77 | 81 |
|
78 | 82 | """
|
79 | 83 |
|
80 |
| - def __init__(self, port=DEFAULT_PORT, device=None, use_airtest_input=True, **kwargs): |
81 |
| - self.device = device or current_device() |
82 |
| - if not self.device: |
83 |
| - self.device = connect_device("Android:///") |
84 |
| - |
85 |
| - platform_name = device_platform(self.device) |
86 |
| - if platform_name == 'Android': |
87 |
| - # always forward for android device to avoid network unreachable |
88 |
| - local_port, _ = self.device.adb.setup_forward('tcp:{}'.format(port)) |
89 |
| - ip = self.device.adb.host or 'localhost' |
90 |
| - port = local_port |
91 |
| - elif platform_name == 'IOS': |
92 |
| - # ip = device.get_ip_address() |
93 |
| - # use iproxy first |
94 |
| - ip = 'localhost' |
95 |
| - port, _ = self.device.instruct_helper.setup_proxy(port) |
96 |
| - else: |
97 |
| - try: |
98 |
| - ip = self.device.get_ip_address() |
99 |
| - except AttributeError: |
100 |
| - try: |
101 |
| - ip = socket.gethostbyname(socket.gethostname()) |
102 |
| - except socket.gaierror: |
103 |
| - # 某些特殊情况下会出现这个error,无法正确获取本机ip地址 |
| 84 | + def __init__(self, port=DEFAULT_PORT, device=None, use_airtest_input=True, ip=None, **kwargs): |
| 85 | + if ip is None or ip == "localhost": |
| 86 | + self.device = device or default_device() |
| 87 | + |
| 88 | + platform_name = device_platform(self.device) |
| 89 | + if platform_name == 'Android': |
| 90 | + # always forward for android device to avoid network unreachable |
| 91 | + local_port, _ = self.device.adb.setup_forward('tcp:{}'.format(port)) |
| 92 | + ip = self.device.adb.host or 'localhost' |
| 93 | + port = local_port |
| 94 | + elif platform_name == 'IOS': |
| 95 | + port, _ = self.device.setup_forward(port) |
| 96 | + if self.device.is_local_device: |
104 | 97 | ip = 'localhost'
|
| 98 | + else: |
| 99 | + ip = self.device.ip |
| 100 | + else: |
| 101 | + try: |
| 102 | + ip = self.device.get_ip_address() |
| 103 | + except AttributeError: |
| 104 | + try: |
| 105 | + ip = socket.gethostbyname(socket.gethostname()) |
| 106 | + except socket.gaierror: |
| 107 | + # 某些特殊情况下会出现这个error,无法正确获取本机ip地址 |
| 108 | + ip = 'localhost' |
105 | 109 |
|
106 | 110 | agent = StdPocoAgent((ip, port), use_airtest_input)
|
107 | 111 | kwargs['reevaluate_volatile_attributes'] = True
|
|
0 commit comments