Skip to content

Commit

Permalink
run_in_headless_android_emulator: add port forwarding for webdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jul 19, 2018
1 parent f5ff709 commit 33234af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions etc/run_in_headless_android_emulator.py
Expand Up @@ -57,6 +57,7 @@ def main(avd_name, apk_path, *args):

logcat_args = ["RustAndroidGlueStdouterr:D", "*:S", "-v", "raw"]
with terminate_on_exit(adb + ["logcat"] + logcat_args) as logcat:
forward_webdriver(adb, args)
logcat.wait()


Expand Down Expand Up @@ -119,6 +120,28 @@ def write_args(adb, args):
check_call(adb + ["shell", "echo %s >> %s" % (shell_quote(arg), params_file)])


def forward_webdriver(adb, args):
webdriver_port = extract_arg("--webdriver", args)
if webdriver_port is not None:
wait_for_tcp_server(adb, webdriver_port)
port = "tcp:%s" % webdriver_port
check_call(adb + ["forward", port, port])
sys.stderr.write("Forwarding WebDriver port %s to the emulator\n" % webdriver_port)


def extract_arg(name, args):
previous_arg_matches = False
for arg in args:
if previous_arg_matches:
return arg
previous_arg_matches = arg == name


def wait_for_tcp_server(adb, port):
while call(adb + ["shell", "nc -z 127.0.0.1 %s" % port]) != 0:
time.sleep(1)


# Copied from Python 3.3+'s shlex.quote()
def shell_quote(arg):
# use single quotes, and put single quotes into double quotes
Expand Down
Expand Up @@ -57,8 +57,8 @@ def __init__(self, executor, browser, capabilities, **kwargs):

def connect(self):
"""Connect to browser via WebDriver."""
# Largish timeout for the case where we're booting an Android emulator.
wait_for_service((self.host, self.port), timeout=120)
# Large timeout for the case where we're booting an Android emulator.
wait_for_service((self.host, self.port), timeout=300)

self.session = webdriver.Session(self.host, self.port, extension=ServoCommandExtensions)
self.session.start()
Expand Down

0 comments on commit 33234af

Please sign in to comment.