def get_driver(request, cmdopt_browser, cmdopt_window): """Fixture to create, return and close driver""" server = Server( "C:\\Program Files (x86)\\browsermob-proxy\\bin\\browsermob-proxy", {"port": 9090}) server.start() proxy = server.create_proxy() url = urllib.parse.urlparse(proxy.proxy).path driver = None if cmdopt_browser == "ie": driver = webdriver.Ie() elif cmdopt_browser == "firefox": if cmdopt_window == "headless": options = Firefox_options() options.add_argument("--headless") options.add_argument('--proxy-server={}'.format(url)) driver = webdriver.Firefox(firefox_options=options) else: options = Firefox_options() options.add_argument('--proxy-server={}'.format(url)) driver = webdriver.Firefox() proxy.new_har() request.addfinalizer(driver.quit) elif cmdopt_browser == "chrome": if cmdopt_window == "headless": options = Chrome_options() options.headless = True options.add_argument('--proxy-server={}'.format(url)) driver = webdriver.Chrome(chrome_options=options) proxy.new_har() request.addfinalizer(driver.quit) else: chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--proxy-server={}'.format(url)) d = DesiredCapabilities.CHROME d['loggingPrefs'] = {'browser': 'ALL'} driver = webdriver.Chrome(desired_capabilities=d, chrome_options=chrome_options) proxy.new_har() ef_driver = EventFiringWebDriver(driver, MyListener()) def fin(): log_timestamp = str(datetime.datetime.now())[0:-4].replace( '-', '.').replace(' ', '_').replace(':', '.') browserlog_filename = log_timestamp + '_browser_log_file.log' browserlogfile = open(browserlog_filename, 'w') print('-------------------------') for i in ef_driver.get_log('browser'): print(i) browserlogfile.write(str(i) + '\n') print(proxy.har) server.stop() ef_driver.quit request.addfinalizer(fin) return ef_driver else: return "unsupported browser" return driver
def get_driver(request, cmdopt_browser, cmdopt_window): """Fixture to create, return and close driver""" driver = None if cmdopt_browser == "ie": driver = webdriver.Ie() elif cmdopt_browser == "firefox": if cmdopt_window == "headless": options = Firefox_options() options.add_argument("--headless") driver = webdriver.Firefox(firefox_options=options) else: driver = webdriver.Firefox() elif cmdopt_browser == "chrome": if cmdopt_window == "headless": options = Chrome_options() options.headless = True driver = webdriver.Chrome(chrome_options=options) else: driver = webdriver.Chrome() else: return "unsupported browser" def close_driver(): driver.close() request.addfinalizer(close_driver) return driver
def driver(browser): if browser == 'chrome': chrome_options = Chrome_options() chrome_options.add_argument("--disable-extensions") chrome_options.add_argument('--headless') capabilities = webdriver.DesiredCapabilities().CHROME driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) logger.info("browser version: %s" % driver.capabilities['version']) elif browser == 'firefox': fp = webdriver.FirefoxProfile() fp.set_preference("dom.disable_beforeunload", True) firefox_options = Firefox_options() firefox_options.add_argument("--headless") capabilities = webdriver.DesiredCapabilities().FIREFOX.copy() capabilities['elementScrollBehavior'] = 1 driver = webdriver.Firefox(options=firefox_options, firefox_profile=fp, capabilities=capabilities) elif browser == 'iphone': chrome_options = Chrome_options() chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--headless") chrome_options.add_argument("--hide-scrollbars") chrome_options.add_argument("--window-size=360,640") chrome_options.add_argument( "user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, " "like Gecko) Version/11.0 Mobile/15A372 Safari/604.1") capabilities = webdriver.DesiredCapabilities().CHROME driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) logger.info("browser version: %s" % driver.capabilities['version']) elif browser == 'ipad': chrome_options = Chrome_options() chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--headless") chrome_options.add_argument("--hide-scrollbars") chrome_options.add_argument("--window-size=800,1024") chrome_options.add_argument( "user-agent=Mozilla/5.0 (iPad; CPU iPad OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1" ) capabilities = webdriver.DesiredCapabilities().CHROME driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) logger.info("browser version: %s" % driver.capabilities['version']) yield driver driver.delete_all_cookies() driver.quit() logger.info('close driver now')