Skip to content

Commit

Permalink
Merge pull request alibaba#340 from mhaoli/support_passing_env_and_args
Browse files Browse the repository at this point in the history
Support passing target app env vars, target app args, and test runner args in `xctest` subcommand
  • Loading branch information
codeskyblue authored Aug 4, 2023
2 parents 913eb41 + 8c19fc8 commit 435dd99
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
28 changes: 27 additions & 1 deletion tidevice/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,29 @@ def cmd_xctest(args: argparse.Namespace):
env[key] = val
if env:
logger.info("Launch env: %s", env)

test_runner_args = []
if args.test_runner_args:
test_runner_args = args.test_runner_args.split(',')
logger.info("Launch test runner args: %s", test_runner_args)

target_app_env = dict(
token.split(":", 1) for token in args.target_app_env or [])
if target_app_env:
logger.info("Target app env: %s", target_app_env)

target_app_args = []
if args.target_app_args:
target_app_args = args.target_app_args.split(',')
logger.info("Target app args: %s", target_app_args)

d.xctest(args.bundle_id,
target_bundle_id=args.target_bundle_id,
logger=setup_logger(level=logging.INFO),
env=env)
env=env,
test_runner_args=test_runner_args,
target_app_env=target_app_env,
target_app_args=target_app_args)


def cmd_screenshot(args: argparse.Namespace):
Expand Down Expand Up @@ -827,6 +846,13 @@ def cmd_test(args: argparse.Namespace):
dict(args=['-e', '--env'],
action='append',
help="set env with format key:value, support multi -e"),
dict(args=['--target-app-env'],
action='append',
help="set env to target app with format key:value, support multi --target-app-env"),
dict(args=['--test-runner-args'],
help="set command line args to test runner with a comma-separated list of strings"),
dict(args=['--target-app-args'],
help="set command line args to target app with a comma-separated list of strings"),
],
help="run XCTest (XCUITest)"),
dict(
Expand Down
38 changes: 30 additions & 8 deletions tidevice/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ def _launch_app_runner(self,
env: dict = {},
target_app_bundle_id: str = None,
logger: logging.Logger = logging,
quit_event: threading.Event = None) -> typing.Tuple[ServiceInstruments, int]: # pid
quit_event: threading.Event = None,
extra_args: Optional[list] = None,
target_app_env: Optional[dict] = None,
target_app_args: Optional[list] = None) -> typing.Tuple[ServiceInstruments, int]: # pid

logger = logging.getLogger(LOG.xctest)

Expand All @@ -849,6 +852,8 @@ def _launch_app_runner(self,
"testBundleURL": bplist.NSURL(None, f"file://{app_info['Path']}/PlugIns/{target_name}.xctest"),
"sessionIdentifier": session_identifier,
"targetApplicationBundleID": target_app_bundle_id,
"targetApplicationArguments": target_app_args or [],
"targetApplicationEnvironment": target_app_env or {},
})) # yapf: disable

fsync = self.app_sync(bundle_id, command="VendContainer")
Expand Down Expand Up @@ -900,6 +905,7 @@ def _launch_app_runner(self,
'-NSTreatUnknownArgumentsAsOpen', 'NO',
'-ApplePersistenceIgnoreState', 'YES'
]
app_args.extend(extra_args or [])
app_options = {'StartSuspendedKey': False}
if self.major_version() >= 12:
app_options['ActivateSuspended'] = True
Expand Down Expand Up @@ -978,20 +984,34 @@ 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, fuzzy_bundle_id="com.*.xctrunner", target_bundle_id=None, logger=None, env: dict={}):
def xctest(self, fuzzy_bundle_id="com.*.xctrunner", target_bundle_id=None,
logger=None, env: dict={},
test_runner_args: Optional[list]=None,
target_app_env: Optional[dict]=None,
target_app_args: Optional[list]=None):
""" Alias of xcuitest """
bundle_id = self._fnmatch_find_bundle_id(fuzzy_bundle_id)
logger.info("BundleID: %s", bundle_id)
return self.xcuitest(bundle_id, target_bundle_id=target_bundle_id, logger=logger, env=env)

def xcuitest(self, bundle_id, target_bundle_id=None, logger=None, env: dict={}):
return self.xcuitest(bundle_id, target_bundle_id=target_bundle_id,
logger=logger, env=env,
test_runner_args=test_runner_args,
target_app_env=target_app_env,
target_app_args=target_app_args)

def xcuitest(self, bundle_id, target_bundle_id=None, logger=None,
env: dict={}, test_runner_args: Optional[list]=None,
target_app_env: Optional[dict]=None,
target_app_args: Optional[list]=None):
"""
Launch xctrunner and wait until quit
Args:
bundle_id (str): xctrunner bundle id
target_bundle_id (str): optional, launch WDA-UITests will not need it
env: launch env
env (dict[str, str]): optional, the environment variables to be passed to the test runner
test_runner_args (list[str]): optional, the command line arguments to be passed to the test runner
target_app_env (dict[str, str]): optional, the environmen variables to be passed to the target app
target_app_args (list[str]): optional, the command line arguments to be passed to the target app
"""
if not logger:
logger = setup_logger(level=logging.INFO)
Expand Down Expand Up @@ -1069,9 +1089,11 @@ def _show_log_message(m: DTXMessage):
# launch test app
# index: 1540
xclogger = setup_logger(name='xctest')
_, pid = self._launch_app_runner(bundle_id, session_identifier,
_, pid = self._launch_app_runner(
bundle_id, session_identifier,
target_app_bundle_id=target_bundle_id,
env=env, logger=xclogger)
env=env, logger=xclogger, extra_args=test_runner_args,
target_app_env=target_app_env, target_app_args=target_app_args)

# xcode call the following commented method, twice
# but it seems can be ignored
Expand Down

0 comments on commit 435dd99

Please sign in to comment.