1import time
2import subprocess
3from .base import require_arg
4from .base import WebDriverBrowser
5from ..executors import executor_kwargs as base_executor_kwargs
6from ..executors.base import WdspecExecutor  # noqa: F401
7from ..executors.executorselenium import (SeleniumTestharnessExecutor,  # noqa: F401
8                                          SeleniumRefTestExecutor)  # noqa: F401
9
10__wptrunner__ = {"product": "edge",
11                 "check_args": "check_args",
12                 "browser": "EdgeBrowser",
13                 "executor": {"testharness": "SeleniumTestharnessExecutor",
14                              "reftest": "SeleniumRefTestExecutor",
15                              "wdspec": "WdspecExecutor"},
16                 "browser_kwargs": "browser_kwargs",
17                 "executor_kwargs": "executor_kwargs",
18                 "env_extras": "env_extras",
19                 "env_options": "env_options",
20                 "run_info_extras": "run_info_extras",
21                 "timeout_multiplier": "get_timeout_multiplier"}
22
23
24def get_timeout_multiplier(test_type, run_info_data, **kwargs):
25    if kwargs["timeout_multiplier"] is not None:
26        return kwargs["timeout_multiplier"]
27    if test_type == "wdspec":
28        return 10
29    return 1
30
31
32def check_args(**kwargs):
33    require_arg(kwargs, "webdriver_binary")
34
35
36def browser_kwargs(logger, test_type, run_info_data, config, **kwargs):
37    return {"webdriver_binary": kwargs["webdriver_binary"],
38            "webdriver_args": kwargs.get("webdriver_args"),
39            "timeout_multiplier": get_timeout_multiplier(test_type,
40                                                         run_info_data,
41                                                         **kwargs)}
42
43
44def executor_kwargs(logger, test_type, test_environment, run_info_data,
45                    **kwargs):
46    executor_kwargs = base_executor_kwargs(test_type, test_environment, run_info_data, **kwargs)
47    executor_kwargs["close_after_done"] = True
48    executor_kwargs["timeout_multiplier"] = get_timeout_multiplier(test_type,
49                                                                   run_info_data,
50                                                                   **kwargs)
51    executor_kwargs["capabilities"] = {}
52    if test_type == "testharness":
53        executor_kwargs["capabilities"]["pageLoadStrategy"] = "eager"
54    return executor_kwargs
55
56
57def env_extras(**kwargs):
58    return []
59
60
61def env_options():
62    return {"supports_debugger": False}
63
64
65class EdgeBrowser(WebDriverBrowser):
66    init_timeout = 60
67
68    def __init__(self, logger, binary, webdriver_binary, webdriver_args=None,
69                 host="localhost", port=None, base_path="/", env=None, **kwargs):
70        super().__init__(logger, binary, webdriver_binary, webdriver_args=webdriver_args,
71                         host=host, port=port, base_path=base_path, env=env, **kwargs)
72        self.host = "localhost"
73
74    def stop(self, force=False):
75        super(self).stop(force)
76        # Wait for Edge browser process to exit if driver process is found
77        edge_proc_name = 'MicrosoftEdge.exe'
78        for i in range(0, 5):
79            procs = subprocess.check_output(['tasklist', '/fi', 'ImageName eq ' + edge_proc_name])
80            if b'MicrosoftWebDriver.exe' not in procs:
81                # Edge driver process already exited, don't wait for browser process to exit
82                break
83            elif edge_proc_name.encode() in procs:
84                time.sleep(0.5)
85            else:
86                break
87
88        if edge_proc_name.encode() in procs:
89            # close Edge process if it is still running
90            subprocess.call(['taskkill.exe', '/f', '/im', 'microsoftedge*'])
91
92    def make_command(self):
93        return [self.webdriver_binary, f"--port={self.port}"] + self.webdriver_args
94
95
96def run_info_extras(**kwargs):
97    osReleaseCommand = r"(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').ReleaseId"
98    osBuildCommand = r"(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').BuildLabEx"
99    try:
100        os_release = subprocess.check_output(["powershell.exe", osReleaseCommand]).strip()
101        os_build = subprocess.check_output(["powershell.exe", osBuildCommand]).strip()
102    except (subprocess.CalledProcessError, OSError):
103        return {}
104
105    rv = {"os_build": os_build,
106          "os_release": os_release}
107    return rv
108