2024-07-23 14:00:35 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
2024-07-23 17:42:36 +00:00
|
|
|
import os
|
2024-07-23 17:05:15 +00:00
|
|
|
import platform
|
|
|
|
|
2020-10-09 17:34:54 +00:00
|
|
|
from selenium import webdriver
|
2024-07-23 15:21:49 +00:00
|
|
|
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
2024-07-23 21:52:24 +00:00
|
|
|
from selenium.webdriver.chrome.service import Service as ChromeService
|
2024-07-23 15:21:49 +00:00
|
|
|
from selenium.webdriver.edge.options import Options as EdgeOptions
|
2024-07-23 17:05:15 +00:00
|
|
|
from selenium.webdriver.edge.service import Service as EdgeService
|
2024-07-23 17:42:36 +00:00
|
|
|
from selenium.webdriver.firefox.options import Options as FirefoxOptions
|
|
|
|
|
2024-07-23 17:05:15 +00:00
|
|
|
from conftest import CWD
|
2020-10-09 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BrowserDriver:
|
2024-07-23 14:00:35 +00:00
|
|
|
|
2020-10-09 17:34:54 +00:00
|
|
|
@staticmethod
|
2020-10-10 21:40:06 +00:00
|
|
|
def get(browser=None, headless=True):
|
2024-07-23 14:00:35 +00:00
|
|
|
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")
|
2020-10-09 17:34:54 +00:00
|
|
|
|
|
|
|
|
2020-10-10 21:40:06 +00:00
|
|
|
def chrome(headless=True):
|
2024-07-23 21:52:24 +00:00
|
|
|
chrome_path = os.path.join(CWD, 'chrome-linux64', 'chrome')
|
2024-07-24 09:40:41 +00:00
|
|
|
driver_path = os.path.join(CWD, 'chrome-linux64', 'chromedriver')
|
2024-07-23 15:21:49 +00:00
|
|
|
options = ChromeOptions()
|
2024-07-23 21:52:24 +00:00
|
|
|
options.binary_location = chrome_path
|
2024-07-23 15:21:49 +00:00
|
|
|
if headless:
|
|
|
|
options.add_argument("--headless")
|
2020-10-09 17:34:54 +00:00
|
|
|
options.add_argument('--ignore-certificate-errors')
|
2024-07-23 21:52:24 +00:00
|
|
|
chrome_service = ChromeService(executable_path=driver_path)
|
|
|
|
return webdriver.Chrome(service=chrome_service, options=options)
|
2020-10-09 17:34:54 +00:00
|
|
|
|
|
|
|
|
2020-10-10 21:40:06 +00:00
|
|
|
def firefox(headless=True):
|
2024-07-23 15:21:49 +00:00
|
|
|
options = FirefoxOptions()
|
|
|
|
if headless:
|
|
|
|
options.add_argument("--headless")
|
2020-10-09 17:34:54 +00:00
|
|
|
options.accept_insecure_certs = True
|
2024-07-23 14:00:35 +00:00
|
|
|
return webdriver.Firefox(options=options)
|
2020-10-09 17:34:54 +00:00
|
|
|
|
|
|
|
|
2020-10-10 22:27:46 +00:00
|
|
|
def edge(headless=True):
|
2024-07-23 15:21:49 +00:00
|
|
|
options = EdgeOptions()
|
2024-07-23 17:05:15 +00:00
|
|
|
driver_name = "msedgedriver"
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
driver_name += ".exe"
|
|
|
|
edgedriver_loc = os.path.join(CWD, 'drivers', driver_name)
|
2024-07-23 17:42:36 +00:00
|
|
|
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')
|
2024-07-23 17:29:17 +00:00
|
|
|
|
2020-10-10 22:27:46 +00:00
|
|
|
options.use_chromium = True
|
2024-07-23 15:21:49 +00:00
|
|
|
options.add_argument('disable-gpu')
|
2024-07-23 18:19:16 +00:00
|
|
|
options.add_argument("no-sandbox")
|
|
|
|
options.add_argument("disable-dev-shm-usage")
|
2024-07-23 15:21:49 +00:00
|
|
|
if headless:
|
|
|
|
options.add_argument('headless')
|
2024-07-23 17:05:15 +00:00
|
|
|
edge_service = EdgeService(executable_path=edgedriver_loc)
|
|
|
|
edge_driver = webdriver.Edge(service=edge_service, options=options)
|
2024-07-23 16:21:29 +00:00
|
|
|
return edge_driver
|
2020-10-09 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def safari():
|
|
|
|
# Because safari driver is bundled with safari
|
|
|
|
return webdriver.Safari()
|