behave-framework/browserdriver/__init__.py

64 lines
1.8 KiB
Python
Raw Normal View History

2024-07-23 14:00:35 +00:00
# pylint: disable=too-few-public-methods
2024-07-23 17:05:15 +00:00
import platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
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
from conftest import CWD
import os
class BrowserDriver:
2024-07-23 14:00:35 +00:00
@staticmethod
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")
def chrome(headless=True):
options = ChromeOptions()
if headless:
options.add_argument("--headless")
options.add_argument('--ignore-certificate-errors')
2024-07-23 14:00:35 +00:00
return webdriver.Chrome(options=options)
def firefox(headless=True):
options = FirefoxOptions()
if headless:
options.add_argument("--headless")
options.accept_insecure_certs = True
2024-07-23 14:00:35 +00:00
return webdriver.Firefox(options=options)
2020-10-10 22:27:46 +00:00
def edge(headless=True):
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:29:17 +00:00
print("***** DRIVER LOCATION *****")
print(edgedriver_loc)
print("***** DRIVER LOCATION *****")
2020-10-10 22:27:46 +00:00
options.use_chromium = True
options.add_argument('disable-gpu')
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)
return edge_driver
def safari():
# Because safari driver is bundled with safari
return webdriver.Safari()