# pylint: disable=too-few-public-methods import os import platform from selenium import webdriver from selenium.webdriver.chrome.options import Options as ChromeOptions from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.edge.options import Options as EdgeOptions from selenium.webdriver.edge.service import Service as EdgeService from selenium.webdriver.firefox.options import Options as FirefoxOptions from conftest import CWD class BrowserDriver: @staticmethod def get(browser=None, headless=True): valid_browsers = ['chrome', 'firefox', 'safari', 'edge'] if browser in valid_browsers: browser_method = globals()[browser] return browser_method(headless) raise ValueError(f"'{browser}' is not a supported browser") def chrome(headless=True): chrome_path = os.path.join(CWD, 'chrome-linux64', 'chrome') driver_path = os.path.join(CWD, 'chrome-linux64', 'chromedriver') options = ChromeOptions() options.binary_location = chrome_path if headless: options.add_argument("--headless") options.add_argument('--ignore-certificate-errors') chrome_service = ChromeService(executable_path=driver_path) return webdriver.Chrome(service=chrome_service, options=options) def firefox(headless=True): options = FirefoxOptions() if headless: options.add_argument("--headless") options.accept_insecure_certs = True return webdriver.Firefox(options=options) def edge(headless=True): options = EdgeOptions() driver_name = "msedgedriver" if platform.system() == 'Windows': driver_name += ".exe" edgedriver_loc = os.path.join(CWD, 'drivers', driver_name) if os.path.isfile(edgedriver_loc): print(f'\nThe file {edgedriver_loc} exists.\n') else: print(f'\nThe file {edgedriver_loc} does not exist!\n') options.use_chromium = True options.add_argument('disable-gpu') options.add_argument("no-sandbox") options.add_argument("disable-dev-shm-usage") if headless: options.add_argument('headless') edge_service = EdgeService(executable_path=edgedriver_loc) edge_driver = webdriver.Edge(service=edge_service, options=options) return edge_driver def safari(): # Because safari driver is bundled with safari return webdriver.Safari()