1# Copyright 2023 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7import logging 8from typing import TYPE_CHECKING, List 9 10import selenium.common.exceptions 11from selenium import webdriver 12from selenium.webdriver.chrome.options import Options as ChromeOptions 13from selenium.webdriver.chrome.service import Service as ChromeService 14 15from crossbench.browsers.attributes import BrowserAttributes 16from crossbench.browsers.chrome.helper import ChromePathMixin 17from crossbench.browsers.chromium.webdriver import ( 18 ChromiumWebDriver, ChromiumWebDriverAndroid, ChromiumWebDriverChromeOsSsh, 19 ChromiumWebDriverSsh, LocalChromiumWebDriverAndroid, 20 build_chromedriver_instructions) 21from crossbench.browsers.webdriver import DriverException 22 23if TYPE_CHECKING: 24 from selenium.webdriver.chromium.options import ChromiumOptions 25 from selenium.webdriver.chromium.service import ChromiumService 26 from selenium.webdriver.chromium.webdriver import ChromiumDriver 27 28 29class ChromeWebDriver(ChromePathMixin, ChromiumWebDriver): 30 31 WEB_DRIVER_OPTIONS = ChromeOptions 32 WEB_DRIVER_SERVICE = ChromeService 33 34 @property 35 def attributes(self) -> BrowserAttributes: 36 return (BrowserAttributes.CHROME | BrowserAttributes.CHROMIUM_BASED 37 | BrowserAttributes.WEBDRIVER) 38 39 def _create_driver(self, options: ChromiumOptions, 40 service: ChromiumService) -> ChromiumDriver: 41 assert isinstance(options, ChromeOptions) 42 assert isinstance(service, ChromeService) 43 try: 44 return webdriver.Chrome(options=options, service=service) 45 except selenium.common.exceptions.WebDriverException as e: 46 msg: List[str] = [f"Could not start WebDriver: {e.msg}"] 47 if self.platform.is_android: 48 msg += [ 49 f"Possibly missing chrome settings on {self.platform}.", 50 "Please make sure to allow chrome-flags on " 51 "non-rooted android devices:", 52 "chrome://flags#enable-command-line-on-non-rooted-devices", 53 ] 54 if self.is_locally_compiled(): 55 msg.append(build_chromedriver_instructions(self.app_path.parent)) 56 msg_str = "\n".join(msg) 57 logging.error(msg_str) 58 raise DriverException(msg_str) from e 59 60 61class ChromeWebDriverAndroid(ChromiumWebDriverAndroid, ChromeWebDriver): 62 pass 63 64 65class LocalChromeWebDriverAndroid(LocalChromiumWebDriverAndroid, 66 ChromeWebDriver): 67 pass 68 69 70class ChromeWebDriverSsh(ChromiumWebDriverSsh, ChromeWebDriver): 71 pass 72 73 74class ChromeWebDriverChromeOsSsh(ChromiumWebDriverChromeOsSsh, ChromeWebDriver): 75 pass 76