Skip to content

Commit

Permalink
Using simplified usage of each broswers Service class
Browse files Browse the repository at this point in the history
Instead of a complicated library written Service Class, like for
Options, I went back to a extremely simply workaround to accept the
existing Open Browser Keyword arguments as is and just pass those which
were going to the driver creater method into the service creater and
pass the resulting opject along. Nothing fancy needed. These changes
reflect this.

A couple other changes the once seperate Firefox creation argument of the
profile has been moved to the options argument. This seems like a
straight forward fix.

The last is the dropping of desired capabilities. This has been changing
internal with selenium and anyone previously using desired capabilities
should have noticed this and corrected already. At this point selenium
has simply dropped the argument and we have take it in but not passed it
along. This we will need to document and maybe code a nicer warning than
silence.
  • Loading branch information
emanlove committed Aug 1, 2023
1 parent 8c0dcb8 commit a28d3a9
Showing 1 changed file with 64 additions and 57 deletions.
121 changes: 64 additions & 57 deletions src/SeleniumLibrary/keywords/webdrivertools/webdrivertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class WebDriverCreator:
def __init__(self, log_dir):
self.log_dir = log_dir
self.selenium_options = SeleniumOptions()
self.selenium_service = SeleniumService()
#self.selenium_service = SeleniumService()

def create_driver(
self,
Expand All @@ -80,7 +80,6 @@ def create_driver(
desired_capabilities = self._parse_capabilities(desired_capabilities, browser)
service_log_path = self._get_log_path(service_log_path)
options = self.selenium_options.create(self.browser_names.get(browser), options)
service = self.selenium_service.create(self.browser_names.get(browser), service_log_path, executable_path)
if service_log_path:
logger.info(f"Browser driver log file created to: {service_log_path}")
self._create_directory(service_log_path)
Expand All @@ -100,7 +99,6 @@ def create_driver(
desired_capabilities,
remote_url,
options=options,
# service=service,
service_log_path=service_log_path,
executable_path=executable_path,
)
Expand Down Expand Up @@ -154,7 +152,6 @@ def create_chrome(
return self._remote(desired_capabilities, remote_url, options=options)
if not executable_path:
executable_path = self._get_executable_path(webdriver.chrome.service.Service)
# service = self.selenium_service.create('chrome', executable_path=executable_path, service_log_path=service_log_path)
service = ChromeService(executable_path=executable_path, log_path=service_log_path)
return webdriver.Chrome(
options=options,
Expand Down Expand Up @@ -194,6 +191,15 @@ def create_firefox(
executable_path="geckodriver",
):
profile = self._get_ff_profile(ff_profile_dir)
if not options:
options = webdriver.FirefoxOptions()
options.profile = profile # <- moved to here :)

# Something I question here is/was whether or not we should create the option, not
# only on whether it exists, but if there is a profile provided. That is previously we just pass
# None along as options if there were none. But now no matter what we create an Options class so
# as to attach the profile to it. If there a scenario in which we don't want to do this???

if remote_url:
default_caps = webdriver.DesiredCapabilities.FIREFOX.copy()
desired_capabilities = self._remote_capabilities_resolver(
Expand All @@ -205,7 +211,7 @@ def create_firefox(
)
if not executable_path:
executable_path = self._get_executable_path(webdriver.firefox.service.Service)
service = self.selenium_service.create('firefox', executable_path=executable_path, service_log_path=service_log_path)
service = FirefoxService(executable_path=executable_path, log_path=service_log_path)
return webdriver.Firefox(
options=options,
#firefox_profile=profile, # need to move
Expand All @@ -231,7 +237,7 @@ def _get_ff_profile(self, ff_profile_dir):
else:
setattr(ff_profile, key, *option[key])
return ff_profile

@property
def _geckodriver_log(self):
log_file = self._get_log_path(
Expand Down Expand Up @@ -277,7 +283,7 @@ def create_ie(
return self._remote(desired_capabilities, remote_url, options=options)
if not executable_path:
executable_path = self._get_executable_path(webdriver.ie.service.Service)
service = self.selenium_service.create('ie', executable_path=executable_path, service_log_path=service_log_path)
service = IeService(executable_path=executable_path, log_path=service_log_path)
return webdriver.Ie(
options=options,
service=service,
Expand All @@ -304,7 +310,7 @@ def create_edge(
return self._remote(desired_capabilities, remote_url)
if not executable_path:
executable_path = self._get_executable_path(webdriver.edge.service.Service)
service = self.selenium_service.create('edge', executable_path=executable_path, service_log_path=service_log_path)
service = EdgeService(executable_path=executable_path, log_path=service_log_path)
return webdriver.Edge(
options=options,
service=service,
Expand All @@ -327,7 +333,7 @@ def create_safari(
return self._remote(desired_capabilities, remote_url)
if not executable_path:
executable_path = self._get_executable_path(webdriver.Safari)
service = self.selenium_service.create('safari', executable_path=executable_path, service_log_path=service_log_path)
service = SafariService(executable_path=executable_path, log_path=service_log_path)
return webdriver.Safari(options=options, service=service)

def create_phantomjs(
Expand Down Expand Up @@ -435,10 +441,10 @@ def _remote(self, desired_capabilities, remote_url, profile_dir=None, options=No
file_detector = self._get_sl_file_detector()
return webdriver.Remote(
command_executor=remote_url,
browser_profile=profile_dir,
# browser_profile=profile_dir,
options=options,
file_detector=file_detector,
**desired_capabilities,
# **desired_capabilities,
)

def _get_sl_file_detector(self):
Expand Down Expand Up @@ -547,52 +553,53 @@ def _get_index(self, alias_or_index):
except ValueError:
return None

class SeleniumService:
""" executable_path: str = DEFAULT_EXECUTABLE_PATH,
port: int = 0,
log_path: typing.Optional[str] = None,
service_args: typing.Optional[typing.List[str]] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
**kwargs,
executable_path = None, port, service_log_path, service_args, env
"""
def create(self, browser,
executable_path=None,
port=0,
service_log_path=None,
service_args=None,
env=None,
start_error_message=None, # chromium, chrome, edge
quiet=False, reuse_service=False, # safari
):
selenium_service = self._import_service(browser)
# chrome, chromium, firefox, edge
if any(chromium_based in browser.lower() for chromium_based in ('chromium', 'chrome', 'edge')):
service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
service_args=service_args,env=env,start_error_message=start_error_message
)
return service
elif 'safari' in browser.lower():
service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
service_args=service_args,env=env,quiet=quiet,reuse_service=reuse_service
)
return service
elif 'firefox' in browser.lower():
service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
service_args=service_args,env=env
)
return service
else:
service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
service_args=service_args,env=env
)
return service

def _import_service(self, browser):
browser = browser.replace("headless_", "", 1)
service = importlib.import_module(f"selenium.webdriver.{browser}.service")
return service.Service
# Temporarily removing as not going to use with initial 4.10.0 hotfixq
# class SeleniumService:
# """ executable_path: str = DEFAULT_EXECUTABLE_PATH,
# port: int = 0,
# log_path: typing.Optional[str] = None,
# service_args: typing.Optional[typing.List[str]] = None,
# env: typing.Optional[typing.Mapping[str, str]] = None,
# **kwargs,

# executable_path = None, port, service_log_path, service_args, env
# """
# def create(self, browser,
# executable_path=None,
# port=0,
# service_log_path=None,
# service_args=None,
# env=None,
# start_error_message=None, # chromium, chrome, edge
# quiet=False, reuse_service=False, # safari
# ):
# selenium_service = self._import_service(browser)
# # chrome, chromium, firefox, edge
# if any(chromium_based in browser.lower() for chromium_based in ('chromium', 'chrome', 'edge')):
# service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
# service_args=service_args,env=env,start_error_message=start_error_message
# )
# return service
# elif 'safari' in browser.lower():
# service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
# service_args=service_args,env=env,quiet=quiet,reuse_service=reuse_service
# )
# return service
# elif 'firefox' in browser.lower():
# service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
# service_args=service_args,env=env
# )
# return service
# else:
# service = selenium_service(executable_path=executable_path, port=port,log_path=service_log_path,
# service_args=service_args,env=env
# )
# return service

# def _import_service(self, browser):
# browser = browser.replace("headless_", "", 1)
# service = importlib.import_module(f"selenium.webdriver.{browser}.service")
# return service.Service

class SeleniumOptions:
def create(self, browser, options):
Expand Down

0 comments on commit a28d3a9

Please sign in to comment.