1import time
2import subprocess
3from .base import Browser, ExecutorBrowser, require_arg
4from .base import NullBrowser  # noqa: F401
5from ..webdriver_server import EdgeDriverServer
6from ..executors import executor_kwargs as base_executor_kwargs
7from ..executors.executorselenium import (SeleniumTestharnessExecutor,  # noqa: F401
8                                          SeleniumRefTestExecutor)  # noqa: F401
9from ..executors.executoredge import EdgeDriverWdspecExecutor  # noqa: F401
10
11__wptrunner__ = {"product": "edge",
12                 "check_args": "check_args",
13                 "browser": {None: "EdgeBrowser",
14                             "wdspec": "NullBrowser"},
15                 "executor": {"testharness": "SeleniumTestharnessExecutor",
16                              "reftest": "SeleniumRefTestExecutor",
17                              "wdspec": "EdgeDriverWdspecExecutor"},
18                 "browser_kwargs": "browser_kwargs",
19                 "executor_kwargs": "executor_kwargs",
20                 "env_extras": "env_extras",
21                 "env_options": "env_options",
22                 "run_info_extras": "run_info_extras",
23                 "timeout_multiplier": "get_timeout_multiplier"}
24
25
26def get_timeout_multiplier(test_type, run_info_data, **kwargs):
27    if kwargs["timeout_multiplier"] is not None:
28        return kwargs["timeout_multiplier"]
29    if test_type == "wdspec":
30        return 10
31    return 1
32
33
34def check_args(**kwargs):
35    require_arg(kwargs, "webdriver_binary")
36
37
38def browser_kwargs(logger, test_type, run_info_data, config, **kwargs):
39    return {"webdriver_binary": kwargs["webdriver_binary"],
40            "webdriver_args": kwargs.get("webdriver_args"),
41            "timeout_multiplier": get_timeout_multiplier(test_type,
42                                                         run_info_data,
43                                                         **kwargs)}
44
45
46def executor_kwargs(logger, test_type, test_environment, run_info_data,
47                    **kwargs):
48    executor_kwargs = base_executor_kwargs(test_type, test_environment, run_info_data, **kwargs)
49    executor_kwargs["close_after_done"] = True
50    executor_kwargs["timeout_multiplier"] = get_timeout_multiplier(test_type,
51                                                                   run_info_data,
52                                                                   **kwargs)
53    executor_kwargs["capabilities"] = {}
54    if test_type == "testharness":
55        executor_kwargs["capabilities"]["pageLoadStrategy"] = "eager"
56    return executor_kwargs
57
58
59def env_extras(**kwargs):
60    return []
61
62
63def env_options():
64    return {"supports_debugger": False}
65
66
67class EdgeBrowser(Browser):
68    init_timeout = 60
69
70    def __init__(self, logger, webdriver_binary,
71            timeout_multiplier=None, webdriver_args=None, **kwargs):
72        Browser.__init__(self, logger)
73        self.server = EdgeDriverServer(self.logger,
74                                       binary=webdriver_binary,
75                                       args=webdriver_args)
76        self.webdriver_host = "localhost"
77        self.webdriver_port = self.server.port
78        if timeout_multiplier:
79            self.init_timeout = self.init_timeout * timeout_multiplier
80
81
82    def start(self, **kwargs):
83        print(self.server.url)
84        self.server.start()
85
86    def stop(self, force=False):
87        self.server.stop(force=force)
88        # Wait for Edge browser process to exit if driver process is found
89        edge_proc_name = 'MicrosoftEdge.exe'
90        for i in range(0,5):
91            procs = subprocess.check_output(['tasklist', '/fi', 'ImageName eq ' + edge_proc_name])
92            if b'MicrosoftWebDriver.exe' not in procs:
93                # Edge driver process already exited, don't wait for browser process to exit
94                break
95            elif edge_proc_name.encode() in procs:
96                time.sleep(0.5)
97            else:
98                break
99
100        if edge_proc_name.encode() in procs:
101            # close Edge process if it is still running
102            subprocess.call(['taskkill.exe', '/f', '/im', 'microsoftedge*'])
103
104    def pid(self):
105        return self.server.pid
106
107    def is_alive(self):
108        # TODO(ato): This only indicates the server is alive,
109        # and doesn't say anything about whether a browser session
110        # is active.
111        return self.server.is_alive()
112
113    def cleanup(self):
114        self.stop()
115
116    def executor_browser(self):
117        return ExecutorBrowser, {"webdriver_url": self.server.url}
118
119
120def run_info_extras(**kwargs):
121    osReleaseCommand = r"(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').ReleaseId"
122    osBuildCommand = r"(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').BuildLabEx"
123    try:
124        os_release = subprocess.check_output(["powershell.exe", osReleaseCommand]).strip()
125        os_build = subprocess.check_output(["powershell.exe", osBuildCommand]).strip()
126    except (subprocess.CalledProcessError, OSError):
127        return {}
128
129    rv = {"os_build": os_build,
130          "os_release": os_release}
131    return rv
132