run_in_headless_android_emulator: add port forwarding for webdriver

This commit is contained in:
Simon Sapin 2018-07-19 18:50:46 +02:00
parent f5ff709c79
commit 33234affc9
2 changed files with 25 additions and 2 deletions

View file

@ -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()
@ -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

View file

@ -57,8 +57,8 @@ class ServoWebDriverProtocol(Protocol):
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()