added phantom support

This commit is contained in:
Mathew Guest 2020-02-23 03:21:21 -07:00
parent b64cd13525
commit d028dea189
2 changed files with 34 additions and 11 deletions

@ -17,13 +17,22 @@ EXECUTOR_PORT = 4444
REMOTE_EXECUTOR = 'http://127.0.0.1:%s/wd/hub' REMOTE_EXECUTOR = 'http://127.0.0.1:%s/wd/hub'
class BrowserProxy: class BrowserProxy:
def __init__(self, session_name='webdriver', pickle_filename=PICKLE_FILENAME): def __init__(
self,
session_name='webdriver',
pickle_filename=PICKLE_FILENAME,
phantom=False
):
self._pickle_filename = pickle_filename self._pickle_filename = pickle_filename
LOGGER.info('creating selenium browser: session name = %s', session_name) LOGGER.info('creating selenium browser: session name = %s', session_name)
try: try:
driver_type = "remote_chromium2" driver_type = "remote_chromium2"
self.driver_type = driver_type self.driver_type = driver_type
print('session_name:', session_name) print('session_name:', session_name)
if phantom:
pass
print('phantom detected')
driver_type = "phantomjs"
self.driver = self.get_driver(driver_type, session_name) self.driver = self.get_driver(driver_type, session_name)
if not self.driver: if not self.driver:
LOGGER.error("failed to get selenium webdriver") LOGGER.error("failed to get selenium webdriver")
@ -169,7 +178,7 @@ class BrowserProxy:
# nhsn actively rejects http requests that do not request with an # nhsn actively rejects http requests that do not request with an
# IE user agent. All of these browser instances have to change the # IE user agent. All of these browser instances have to change the
# user agent in various ways. # user agent in various ways.
LOGGER.debug("creating selenium driver: %s" % (browser)) LOGGER.debug('creating selenium driver: %s' % (browser))
# todo(mathew guest) turn USER_AGENT into local variable so that it # todo(mathew guest) turn USER_AGENT into local variable so that it
# could be parameterized someday # could be parameterized someday
# user_agent = USER_AGENT # user_agent = USER_AGENT
@ -178,24 +187,27 @@ class BrowserProxy:
driver = None driver = None
if browser == "chromium2": # Selenium 2 - Chrome if browser == 'chromium2': # Selenium 2 - Chrome
driver = self._create_driver_chromium2() driver = self._create_driver_chromium2()
elif browser == 'remote_chromium2': elif browser == 'remote_chromium2':
driver = self._create_driver_remote_chromium2(session_name) driver = self._create_driver_remote_chromium2(session_name)
elif browser == "chromium1": # Selenium 1 - Chrome without working user agent switch elif browser == 'chromium1': # Selenium 1 - Chrome without working user agent switch
driver = self._create_driver_chromium1() driver = self._create_driver_chromium1()
elif browser == "firefox2": # Selenium 2 - Firefox elif browser == 'firefox2': # Selenium 2 - Firefox
driver = self._create_driver_firefox2() driver = self._create_driver_firefox2()
elif browser == "firefox1": # Selenium 1 - Firefox elif browser == 'firefox1': # Selenium 1 - Firefox
driver = self._create_driver_firefox1() driver = self._create_driver_firefox1()
elif browser == 'phantomjs':
driver = self._create_driver_phantomjs()
else: else:
LOGGER.error("an attempt was made to request an "\ LOGGER.error('an attempt was made to request an '\
"unsupported (by this product) selenium "\ 'unsupported (by this product) selenium '\
"webdriver; refusing. requested = %s"\ 'webdriver; refusing. requested = %s'\
% (browser)) % (browser))
driver.implicitly_wait(10) driver.implicitly_wait(10)
@ -301,3 +313,16 @@ class BrowserProxy:
selenium.webdriver.DesiredCapabilities.FIREFOX.copy(), browser_profile = profile ) selenium.webdriver.DesiredCapabilities.FIREFOX.copy(), browser_profile = profile )
return driver return driver
def _create_driver_phantomjs(self):
# Note(MG): Selenium support for PhantomJS has been deprecated, please use headless
# driver = selenium.webdriver.PhantomJS()
# driver.set_window_size(1280, 960)
# return driver
opt = selenium.webdriver.chrome.options.Options()
opt.add_argument("--user-agent=" + USER_AGENT)
opt.add_argument("--kiosk-printing")
opt.add_argument("--headless")
driver = selenium.webdriver.Chrome(chrome_options = opt)
self.driver = driver
return driver

@ -1,5 +1,3 @@
# import nhsn_lo.pages
import enum import enum
import random import random
import selenium.webdriver import selenium.webdriver