From 499a7756b7ef9a4f6e14bdbe9464ed1a6374f53b Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Fri, 29 Jan 2021 17:16:04 +0800 Subject: [PATCH] add support tidevice xctest -e USE_PORT:8100 --- README.md | 5 ++++- README_EN.md | 5 ++++- tidevice/__main__.py | 27 ++++++++++++++++++--------- tidevice/_device.py | 6 ++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4b45bbb..92c9d24 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ $ tidevice kill com.example.demo $ tidevice applist ``` -### 运行XCTest +### 运行WebDriverAgent > 请先确保手机上已经安装有[WebDriverAgent](https://github.com/appium/WebDriverAgent)应用 ```bash @@ -79,6 +79,9 @@ $ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner [I 210127 11:40:23 _device:1003] ProductVersion: 12.4 [I 210127 11:40:24 _device:952] Start execute test plan with IDE version: 29 [I 210127 11:40:24 _device:875] WebDriverAgent start successfully + +# 修改监听端口为8200 +$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner -e USB_PORT:8200 ``` 启动后你就可以使用Appium 或者 [facebook-wda](https://github.com/openatx/facebook-wda) 来运行iOS自动化了 diff --git a/README_EN.md b/README_EN.md index efe1423..f565703 100644 --- a/README_EN.md +++ b/README_EN.md @@ -62,7 +62,7 @@ $ tidevice kill com.example.demo $ tidevice applist ``` -### Run XCTest +### Run WebDriverAgent > Please make sure your iPhone already have [WebDriverAgent](https://github.com/appium/WebDriverAgent) installed ```bash @@ -74,6 +74,9 @@ $ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner [I 210127 11:40:23 _device:1003] ProductVersion: 12.4 [I 210127 11:40:24 _device:952] Start execute test plan with IDE version: 29 [I 210127 11:40:24 _device:875] WebDriverAgent start successfully + +# Change WDA listen port to 8200 +$ tidevice xctest -B com.facebook.wda.WebDriverAgent.Runner -e USB_PORT:8200 ``` Then you can connect with Appium or [facebook-wda](https://github.com/openatx/facebook-wda) diff --git a/tidevice/__main__.py b/tidevice/__main__.py index 7488861..b59d02e 100644 --- a/tidevice/__main__.py +++ b/tidevice/__main__.py @@ -200,7 +200,13 @@ def cmd_xctest(args: argparse.Namespace): Run XCTest required WDA installed. """ d = _udid2device(args.udid) - d.xctest(args.bundle_id, logger=setup_logger(level=logging.INFO)) + env = {} + for kv in args.env or {}: + key, val = kv.split(":", 1) + env[key] = val + if env: + logger.info("Launch env: %s", env) + d.xctest(args.bundle_id, logger=setup_logger(level=logging.INFO), env=env) def cmd_screenshot(args: argparse.Namespace): @@ -230,7 +236,7 @@ def cmd_applist(args: argparse.Namespace): try: display_name = info['CFBundleDisplayName'] # major.minor.patch - version = info.get('CFBundleShortVersionString','') + version = info.get('CFBundleShortVersionString', '') print(bundle_id, display_name, version) except BrokenPipeError: break @@ -243,8 +249,8 @@ def cmd_launch(args: argparse.Namespace): d = _udid2device(args.udid) try: pid = d.instruments.app_launch(args.bundle_id, - args=args.arguments, - kill_running=args.kill) + args=args.arguments, + kill_running=args.kill) print("PID:", pid) except ServiceError as e: sys.exit(e) @@ -278,14 +284,12 @@ def cmd_developer(args: argparse.Namespace): def cmd_test(args: argparse.Namespace): print("Just test") # files = os.listdir(path) - + # Here need device unlocked # signatures = d.imagemounter.lookup() # if signatures: # logger.info("DeveloperImage already mounted") # return - - _commands = [ @@ -339,7 +343,10 @@ def cmd_test(args: argparse.Namespace): help="test application bundle id"), dict(args=['-I', '--install-wda'], action='store_true', - help='install webdriveragent app') + help='install webdriveragent app'), + dict(args=['-e', '--env'], + action='append', + help="set env with format key:value, support multi -e"), ], help="run XCTest"), dict(action=cmd_screenshot, @@ -357,7 +364,9 @@ def cmd_test(args: argparse.Namespace): dict(args=['arguments'], nargs='*', help='app arguments'), ], help="launch app with bundle_id"), - dict(action=cmd_developer, command="developer", help="mount developer image to device"), + dict(action=cmd_developer, + command="developer", + help="mount developer image to device"), dict(action=cmd_kill, command="kill", flags=[dict(args=['name'], help='pid or bundle_id')], diff --git a/tidevice/_device.py b/tidevice/_device.py index 72d73f5..7f3d3ad 100644 --- a/tidevice/_device.py +++ b/tidevice/_device.py @@ -765,6 +765,7 @@ def instruments(self) -> ServiceInstruments: def _launch_wda(self, bundle_id: str, session_identifier: uuid.UUID, + env: dict = {}, logger: logging.Logger = logging, quit_event: threading.Event = None) -> int: # pid @@ -817,6 +818,7 @@ def _launch_wda(self, 'MJPEG_SERVER_PORT': '', 'USE_PORT': '', } # yapf: disable + app_env.update(env) if self.major_version() >= 11: app_env['DYLD_INSERT_LIBRARIES'] = '/Developer/usr/lib/libMainThreadChecker.dylib' @@ -898,7 +900,7 @@ def _fnmatch_find_bundle_id(self, bundle_id: str) -> str: key=lambda v: v != 'com.facebook.wda.irmarunner.xctrunner') return bundle_ids[0] - def xctest(self, bundle_id="com.facebook.*.xctrunner", logger=None): + def xctest(self, bundle_id="com.facebook.*.xctrunner", logger=None, env: dict={}): """ Launch xctrunner and wait until quit """ @@ -979,7 +981,7 @@ def _show_log_message(m: DTXMessage): # launch test app # index: 1540 - pid = self._launch_wda(bundle_id, session_identifier, logger) + pid = self._launch_wda(bundle_id, session_identifier, env=env, logger=logger) # xcode call the following commented method, twice # but it seems can be ignored