working remote firefox and chromium

This commit is contained in:
Mathew Guest 2022-03-02 08:56:08 -07:00
parent 3ef55c0590
commit fd2aa7c524
2 changed files with 76 additions and 20 deletions

@ -53,7 +53,8 @@ class BrowserProxy:
'firefox2',
'chromium2',
'remote_chromium2',
'phantomjs'
'phantomjs',
'remote_firefox'
), 'webdriver_type must be firefox1, firefox2, chromium2, remote_chromium2, or phantomjs'
@ -212,20 +213,24 @@ class BrowserProxy:
driver = None
browser_factory = CreateBrowser()
if browser == 'chromium2': # Selenium 2 - Chrome
driver = self._create_driver_chromium2()
driver = self._create_driver_chromium()
elif browser == 'remote_chromium2':
driver = browser_factory._create_driver_remote_chromium2(session_name)
# driver = self._create_driver_remote_chromium2(session_name)
elif browser == 'remote_chromium':
driver = browser_factory._create_driver_remote_chromium(session_name)
elif browser == 'remote_firefox': # Selenium 1 - Firefox
driver = browser_factory._create_driver_remote_firefox(session_name)
# TODO(MG) need to redo/rename below. selenium2/ not remote, uses native browser
# webdrviers instead of javascript
elif browser == 'chromium1': # Selenium 1 - Chrome without working user agent switch
driver = self._create_driver_chromium1()
elif browser == 'firefox2': # Selenium 2 - Firefox
driver = self._create_driver_firefox2()
elif browser == 'firefox1': # Selenium 1 - Firefox
driver = self._create_driver_firefox1()
elif browser == 'phantomjs':
driver = self._create_driver_phantomjs()

@ -45,9 +45,10 @@ class CreateBrowser:
def pickle_filename(self, value):
self._pickle_filename = value
def _create_driver_remote_chromium2(self, session_name):
# i think should be chromium1, ie selenium1 or seleniumRC
def _create_driver_remote_chromium(self, session_name):
p = self.pickle_filename
final_name = '{}-{}'.format('rchromium2', session_name)
final_name = '{}-{}'.format('remote-chromium', session_name)
driver = None
# Definitely no browser instance already, we must instantiate
@ -81,7 +82,7 @@ class CreateBrowser:
# At this point, need to instantiate a new browser instance
sel_host = REMOTE_EXECUTOR % (EXECUTOR_PORT)
LOGGER.info('instantianting new browser instance (chromium2)')
LOGGER.info('instantianting new browser instance (remote_chromium)')
LOGGER.info('remote selenium: %s', sel_host)
opt = selenium.webdriver.chrome.options.Options()
@ -102,6 +103,65 @@ class CreateBrowser:
return driver
# Pickle impl. is duped here
def _create_driver_remote_firefox(self, session_name):
p = self.pickle_filename
final_name = '{}-{}'.format('remote-firefox', session_name)
driver = None
# Definitely no browser instance already, we must instantiate
if not os.path.exists(p):
LOGGER.debug('no pickled file for saved browser instances (nothing saved yet)')
# There MAY be an open browser or an invalidated reference to a once-open browser
if os.path.exists(p):
LOGGER.debug('found pickled file for saved browser instances: %s', p)
# First, see if existing session_name browser instance exists
fp = None
drivers = {}
try:
fp = open(p, 'rb')
drivers = pickle.load(fp)
if not drivers:
raise Exception
LOGGER.debug('found saved browser instances: %s', list(drivers.keys()))
driver = drivers.get(final_name)
if not driver:
raise Exception
LOGGER.debug('connected to pickled webdriver instance: %s', final_name)
url = driver.current_url # throw error if driver isn't reliable anymore
LOGGER.info('webdriver instance is ready')
# self.driver = driver
return driver
except (FileNotFoundError, IOError) as ex:
LOGGER.error('unable to connect to existing webdriver: no pickled drivers found')
except Exception as ex:
self.driver = None
# At this point, need to instantiate a new browser instance
sel_host = REMOTE_EXECUTOR % (EXECUTOR_PORT)
LOGGER.info('instantianting new browser instance (remote_firefox)')
LOGGER.info('remote selenium: %s', sel_host)
profile = selenium.webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", USER_AGENT)
driver = selenium.webdriver.Remote(
# SELENIUM1_SERVER_PATH,
sel_host,
selenium.webdriver.DesiredCapabilities.FIREFOX.copy(),
browser_profile = profile
)
# Save to pickle
fp = open(p, 'wb')
drivers[final_name] = driver
LOGGER.info('saving browser instance to pickle: %s', final_name)
pickle.dump(drivers, fp)
return driver
def _create_driver_chromium2(self):
opt = selenium.webdriver.chrome.options.Options()
@ -150,16 +210,7 @@ class CreateBrowser:
driver = selenium.webdriver.Firefox(profile)
return driver
def _create_driver_firefox1(self):
profile = selenium.webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", USER_AGENT)
driver = selenium.webdriver.Remote(
# SELENIUM1_SERVER_PATH,
REMOTE_EXECUTOR % (EXECUTOR_PORT),
selenium.webdriver.DesiredCapabilities.FIREFOX.copy(),
browser_profile = profile
)
return driver
def _create_driver_phantomjs(self):
# Note(MG): Selenium support for PhantomJS has been deprecated, please use headless