|
| 1 | +import subprocess |
| 2 | + |
| 3 | +from .base import Browser, ExecutorBrowser, require_arg |
| 4 | +from .base import get_timeout_multiplier # noqa: F401 |
| 5 | +from .chrome import executor_kwargs as chrome_executor_kwargs |
| 6 | +from ..webdriver_server import ChromeDriverServer |
| 7 | +from ..executors.executorwebdriver import (WebDriverTestharnessExecutor, # noqa: F401 |
| 8 | + WebDriverRefTestExecutor) # noqa: F401 |
| 9 | +from ..executors.executorchrome import ChromeDriverWdspecExecutor # noqa: F401 |
| 10 | + |
| 11 | + |
| 12 | +__wptrunner__ = {"product": "android_webview", |
| 13 | + "check_args": "check_args", |
| 14 | + "browser": "SystemWebViewShell", |
| 15 | + "executor": {"testharness": "WebDriverTestharnessExecutor", |
| 16 | + "reftest": "WebDriverRefTestExecutor", |
| 17 | + "wdspec": "ChromeDriverWdspecExecutor"}, |
| 18 | + "browser_kwargs": "browser_kwargs", |
| 19 | + "executor_kwargs": "executor_kwargs", |
| 20 | + "env_extras": "env_extras", |
| 21 | + "env_options": "env_options", |
| 22 | + "timeout_multiplier": "get_timeout_multiplier"} |
| 23 | + |
| 24 | +_wptserve_ports = set() |
| 25 | + |
| 26 | + |
| 27 | +def check_args(**kwargs): |
| 28 | + require_arg(kwargs, "webdriver_binary") |
| 29 | + |
| 30 | + |
| 31 | +def browser_kwargs(test_type, run_info_data, config, **kwargs): |
| 32 | + return {"binary": kwargs["binary"], |
| 33 | + "webdriver_binary": kwargs["webdriver_binary"], |
| 34 | + "webdriver_args": kwargs.get("webdriver_args")} |
| 35 | + |
| 36 | + |
| 37 | +def executor_kwargs(test_type, server_config, cache_manager, run_info_data, |
| 38 | + **kwargs): |
| 39 | + # Use update() to modify the global list in place. |
| 40 | + _wptserve_ports.update(set( |
| 41 | + server_config['ports']['http'] + server_config['ports']['https'] + |
| 42 | + server_config['ports']['ws'] + server_config['ports']['wss'] |
| 43 | + )) |
| 44 | + |
| 45 | + executor_kwargs = chrome_executor_kwargs(test_type, server_config, |
| 46 | + cache_manager, run_info_data, |
| 47 | + **kwargs) |
| 48 | + del executor_kwargs["capabilities"]["goog:chromeOptions"]["prefs"] |
| 49 | + del executor_kwargs["capabilities"]["goog:chromeOptions"]["useAutomationExtension"] |
| 50 | + capabilities = executor_kwargs["capabilities"] |
| 51 | + # Note that for WebView, we launch a test shell and have the test shell use WebView. |
| 52 | + # https://chromium.googlesource.com/chromium/src/+/HEAD/android_webview/docs/webview-shell.md |
| 53 | + capabilities["goog:chromeOptions"]["androidPackage"] = \ |
| 54 | + "org.chromium.webview_shell" |
| 55 | + capabilities["goog:chromeOptions"]["androidActivity"] = ".WebPlatformTestsActivity" |
| 56 | + |
| 57 | + # Workaround: driver.quit() cannot quit SystemWebViewShell. |
| 58 | + executor_kwargs["pause_after_test"] = False |
| 59 | + # Workaround: driver.close() is not supported. |
| 60 | + executor_kwargs["restart_after_test"] = True |
| 61 | + executor_kwargs["close_after_done"] = False |
| 62 | + return executor_kwargs |
| 63 | + |
| 64 | + |
| 65 | +def env_extras(**kwargs): |
| 66 | + return [] |
| 67 | + |
| 68 | + |
| 69 | +def env_options(): |
| 70 | + return {} |
| 71 | + |
| 72 | + |
| 73 | +#TODO: refactor common elements of SystemWebViewShell and ChromeAndroidBrowser |
| 74 | +class SystemWebViewShell(Browser): |
| 75 | + """Chrome is backed by chromedriver, which is supplied through |
| 76 | + ``wptrunner.webdriver.ChromeDriverServer``. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, logger, binary, webdriver_binary="chromedriver", |
| 80 | + webdriver_args=None): |
| 81 | + """Creates a new representation of Chrome. The `binary` argument gives |
| 82 | + the browser binary to use for testing.""" |
| 83 | + Browser.__init__(self, logger) |
| 84 | + self.binary = binary |
| 85 | + self.server = ChromeDriverServer(self.logger, |
| 86 | + binary=webdriver_binary, |
| 87 | + args=webdriver_args) |
| 88 | + self.setup_adb_reverse() |
| 89 | + |
| 90 | + def _adb_run(self, args): |
| 91 | + self.logger.info('adb ' + ' '.join(args)) |
| 92 | + subprocess.check_call(['adb'] + args) |
| 93 | + |
| 94 | + def setup_adb_reverse(self): |
| 95 | + self._adb_run(['wait-for-device']) |
| 96 | + self._adb_run(['forward', '--remove-all']) |
| 97 | + self._adb_run(['reverse', '--remove-all']) |
| 98 | + # "adb reverse" basically forwards network connection from device to |
| 99 | + # host. |
| 100 | + for port in _wptserve_ports: |
| 101 | + self._adb_run(['reverse', 'tcp:%d' % port, 'tcp:%d' % port]) |
| 102 | + |
| 103 | + def start(self, **kwargs): |
| 104 | + self.server.start(block=False) |
| 105 | + |
| 106 | + def stop(self, force=False): |
| 107 | + self.server.stop(force=force) |
| 108 | + |
| 109 | + def pid(self): |
| 110 | + return self.server.pid |
| 111 | + |
| 112 | + def is_alive(self): |
| 113 | + # TODO(ato): This only indicates the driver is alive, |
| 114 | + # and doesn't say anything about whether a browser session |
| 115 | + # is active. |
| 116 | + return self.server.is_alive() |
| 117 | + |
| 118 | + def cleanup(self): |
| 119 | + self.stop() |
| 120 | + self._adb_run(['forward', '--remove-all']) |
| 121 | + self._adb_run(['reverse', '--remove-all']) |
| 122 | + |
| 123 | + def executor_browser(self): |
| 124 | + return ExecutorBrowser, {"webdriver_url": self.server.url} |
0 commit comments