def launch_browser(): if env.RUNNING_BROWSER.upper() == "FIREFOX": # the end of the browser process , the end of the browser driven process os.popen("TASKKILL /F /IM firefoxdriver.exe") fp = FirefoxProfile() fp.native_events_enabled = False binary_path = PublicImp.common.get_value_from_conf("FIREFOX_BINARY_PATH") if binary_path == "": env.driver = selenium.webdriver.Firefox(firefox_profile=fp) else: fb = FirefoxBinary(firefox_path=binary_path) env.driver = selenium.webdriver.Firefox(firefox_profile=fp, firefox_binary=fb) elif env.RUNNING_BROWSER.upper() == "CHROME": os.popen("TASKKILL /F /IM chromedriver.exe") binary_path = PublicImp.common.get_value_from_conf("CHROME_BINARY_PATH") chromedriver = PublicImp.common.get_value_from_conf("DRIVER_CHROME") if binary_path == "": os.environ["webdriver.chrome.driver"] = chromedriver env.driver = selenium.webdriver.Chrome(executable_path=chromedriver) else: opts = Options() opts.binary_location = binary_path os.environ["webdriver.chrome.driver"] = chromedriver env.driver = selenium.webdriver.Chrome(executable_path=chromedriver, chrome_options=opts) elif env.RUNNING_BROWSER.upper() == "IE": os.popen("TASKKILL /F /IM IEDriverServer.exe") dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['acceptSslCerts'] = True dc['nativeEvents'] = True iedriver = PublicImp.common.get_value_from_conf("DRIVER_IE") os.environ["webdriver.ie.driver"] = iedriver env.driver = selenium.webdriver.Ie(executable_path=iedriver, capabilities=dc) else: return False env.platformName = env.RUNNING_BROWSER env.TEST_URL = PublicImp.common.get_value_from_conf("TESTING_URL") env.driver.get(env.TEST_URL) env.driver.maximize_window() time.sleep(3) env.driver.refresh() # env.driver.set_window_size(480, 800) time.sleep(3) return True
def webBrowser(self): if self.driverName.upper() == 'FIREFOX': '''There are 2 different way to get the instance of webdriver: 1. Use configuration file to record it, but when use it, system will not recognize its methods, but useful. i.e. Conf.BROWSER = webdriver.Firefox() #-> Init and create instance di = DriverInvoker("firefox"); di.webBrowser() Conf.BROWSER.get(Conf.URL) Conf.BROWSER.maximize_window() Conf.BROWSER.quit() 2. To return the instance of webdriver. i.e. DRIVER = webdriver.Firefox() return DRIVER #-> Init and create instance di = DriverInvoker("firefox"); DRIVER = di.webBrowser() DRIVER.get(Conf.URL) DRIVER.maximize_window() DRIVER.quit() ''' fp = FirefoxProfile() fp.native_events_enabled = False Conf.DRIVER = webdriver.Firefox(firefox_profile=fp) elif self.driverName.upper() == 'CHROME': os.popen("TASKKILL /F /IM chromedriver.exe") Conf.DRIVER = webdriver.Chrome(executable_path=Conf.DRIVER_CHROME) elif self.driverName.upper() == 'IE': os.popen("TASKKILL /F /IM IEDriverServer.exe") dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['acceptSslCerts'] = True dc['nativeEvents'] = True dc['ignoreProtectedModeSettings'] = True os.environ["webdriver.ie.DRIVER"] = Conf.DRIVER_IE Conf.DRIVER = webdriver.Ie(executable_path=Conf.DRIVER_IE, capabilities=dc) else: return None
def make_driver(context): ''' Создать объект-драйвер для взаимодействия с браузером. Можно вызывать в before_feature, если мы хотим каждый тест проводить, в отдельном браузере. P.S. в таком случае, не забыть в after_feature закрывать браузер. ''' # использовать конкретные бинарники print ('Opening browser now!') path_to_binary = getcwd() + '/steps/data/firefox/firefox' binary = FirefoxBinary(path_to_binary) profile = FirefoxProfile() profile.native_events_enabled = False context.list_ud = list() # выбор профиля if "win" in platform: context.browser = webdriver.Firefox(firefox_profile=profile) else: context.browser = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary) context.browser.maximize_window()
def get_firefox_webdriver_args(self): options = selenium.webdriver.FirefoxOptions() if self['headless']: options.add_argument('-headless') profile = FirefoxProfile( os.environ.get( 'GOCEPT_WEBDRIVER_FF_PROFILE', os.environ.get('GOCEPT_SELENIUM_FF_PROFILE'))) profile.native_events_enabled = True profile.update_preferences() # Save downloads always to disk into a predefined dir. profile.set_preference("browser.download.folderList", 2) profile.set_preference( "browser.download.manager.showWhenStarting", False) profile.set_preference( "browser.download.dir", str(self['selenium_download_dir'])) profile.set_preference( "browser.helperApps.neverAsk.saveToDisk", "application/pdf") profile.set_preference("pdfjs.disabled", True) return {'options': options, 'firefox_profile': profile}
def webBrowser(self): if self.driverName.upper() == 'FIREFOX': '''There are 2 different way to get the instance of webdriver: 1. Use configuration file to record it, but when use it, system will not recognize its methods, but useful. i.e. Conf.BROWSER = webdriver.Firefox() #-> Init and create instance di = DriverInvoker("firefox"); di.webBrowser() Conf.BROWSER.get(Conf.URL) Conf.BROWSER.maximize_window() Conf.BROWSER.quit() 2. To return the instance of webdriver. i.e. DRIVER = webdriver.Firefox() return DRIVER #-> Init and create instance di = DriverInvoker("firefox"); DRIVER = di.webBrowser() DRIVER.get(Conf.URL) DRIVER.maximize_window() DRIVER.quit() ''' # Method 1 fp = FirefoxProfile() fp.native_events_enabled = False # fp.accept_untrusted_certs() Conf.DRIVER = webdriver.Firefox(firefox_profile=fp) # Method 2 # DRIVER = webdriver.Firefox() # return DRIVER elif self.driverName.upper() == 'CHROME': # os.popen("TASKKILL /F /IM chrome.exe") os.popen("TASKKILL /F /IM chromedriver.exe") # Method 1 Conf.DRIVER = webdriver.Chrome(executable_path=Conf.DRIVER_CHROME) # Method 2 # DRIVER = webdriver.Chrome(executable_path=Conf.DRIVER_CHROME) # return DRIVER elif self.driverName.upper() == 'IE': # os.popen("TASKKILL /F /IM iexplore.exe") os.popen("TASKKILL /F /IM IEDriverServer.exe") dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['acceptSslCerts'] = True dc['nativeEvents'] = True dc['ignoreProtectedModeSettings'] = True os.environ["webdriver.ie.DRIVER"] = Conf.DRIVER_IE Conf.DRIVER = webdriver.Ie(executable_path=Conf.DRIVER_IE, capabilities=dc) # Method 2 # DRIVER = webdriver.Ie(executable_path=Conf.DRIVER_IE, capabilities=dc) # return DRIVER elif self.driverName.upper() == 'HTMLUNIT': err_msg = "Selenium server is shut down, please to start it..." start_selenium_server = "java -jar selenium-server-standalone-2.41.0.jar" try: Conf.DRIVER = webdriver.Remote( "http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS) except Exception as e: print str(e) init_env_record( '-> %s %s \n %s \n Start by : %s' % (stamp_datetime(), str(e), err_msg, start_selenium_server)) else: return None
def launch_browser(): if env.RUNNING_BROWSER.upper() == "FIREFOX": # the end of the browser process , the end of the browser driven process os.popen("TASKKILL /F /IM firefoxdriver.exe") fp = FirefoxProfile() fp.native_events_enabled = False binary_path = PublicImp.common.get_value_from_conf( "FIREFOX_BINARY_PATH") if binary_path == "": env.driver = selenium.webdriver.Firefox(firefox_profile=fp) else: fb = FirefoxBinary(firefox_path=binary_path) env.driver = selenium.webdriver.Firefox(firefox_profile=fp, firefox_binary=fb) elif env.RUNNING_BROWSER.upper() == "CHROME": os.popen("TASKKILL /F /IM chromedriver.exe") binary_path = PublicImp.common.get_value_from_conf( "CHROME_BINARY_PATH") chromedriver = PublicImp.common.get_value_from_conf("DRIVER_CHROME") if binary_path == "": os.environ["webdriver.chrome.driver"] = chromedriver env.driver = selenium.webdriver.Chrome( executable_path=chromedriver) else: opts = Options() opts.binary_location = binary_path os.environ["webdriver.chrome.driver"] = chromedriver env.driver = selenium.webdriver.Chrome( executable_path=chromedriver, chrome_options=opts) elif env.RUNNING_BROWSER.upper() == "IE": os.popen("TASKKILL /F /IM IEDriverServer.exe") dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['acceptSslCerts'] = True dc['nativeEvents'] = True iedriver = PublicImp.common.get_value_from_conf("DRIVER_IE") os.environ["webdriver.ie.driver"] = iedriver env.driver = selenium.webdriver.Ie(executable_path=iedriver, capabilities=dc) else: return False env.platformName = env.RUNNING_BROWSER env.TEST_URL = PublicImp.common.get_value_from_conf("TESTING_URL") env.driver.get(env.TEST_URL) env.driver.maximize_window() time.sleep(3) env.driver.refresh() # env.driver.set_window_size(480, 800) time.sleep(3) return True
def launch_browser(url): ''' Launch a new browser, and set the parameters for the browser. ''' if env.threadlocal.TESTING_BROWSER.upper() == 'FIREFOX': fp = FirefoxProfile() fp.native_events_enabled = False if env.FIREFOX_BINARY == '': try: env.THREAD_LOCK.acquire() browser = webdriver.Firefox(firefox_profile=fp) except: if isinstance(env.RESERVED_FIREFOX_BINARY, str) and env.RESERVED_FIREFOX_BINARY != "": browser = webdriver.Firefox( firefox_profile=fp, firefox_binary=FirefoxBinary( firefox_path=env.RESERVED_FIREFOX_BINARY)) else: try: log.step_warning("try to start firefox again!") time.sleep(20) browser = webdriver.Firefox(firefox_profile=fp) except: log.handle_error() return False finally: env.THREAD_LOCK.release() else: browser = webdriver.Firefox( firefox_profile=fp, firefox_binary=FirefoxBinary(firefox_path=env.FIREFOX_BINARY)) elif env.threadlocal.TESTING_BROWSER.upper() == 'CHROME': if env.DRIVER_OF_CHROME == '': print('DRIVER_OF_CHROME is empty.') return False os.environ['webdriver.chrome.driver'] = env.DRIVER_OF_CHROME browser = webdriver.Chrome(executable_path=env.DRIVER_OF_CHROME) elif env.threadlocal.TESTING_BROWSER.upper() == 'IE': if env.DRIVER_OF_IE == '': print('DRIVER_OF_IE is empty.') return False # os.popen('TASKKILL /F /IM iexplore.exe') os.popen('TASKKILL /F /IM IEDriverServer.exe') dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['nativeEvents'] = False dc['acceptSslCerts'] = True os.environ['webdriver.ie.driver'] = env.DRIVER_OF_IE browser = webdriver.Ie(executable_path=env.DRIVER_OF_IE, capabilities=dc) elif env.threadlocal.TESTING_BROWSER.upper() == 'PHANTOMJS': browser = webdriver.PhantomJS( r'D:\\Emma\\qiqi\\pop\\drivers\\phantomjs\\phantomjs.exe') if not env.BROWSER_VERSION_INFO.has_key(env.threadlocal.TESTING_BROWSER): env.BROWSER_VERSION_INFO[ env.threadlocal.TESTING_BROWSER] = browser.capabilities['version'] browser.set_window_size(1920, 1080) browser.set_window_position(0, 0) browser.set_page_load_timeout(300) browser.implicitly_wait(0) browser.get(url) return browser
def launch_browser(url): ''' Launch a new browser, and set the parameters for the browser. ''' if env.threadlocal.TESTING_BROWSER.upper() == 'FIREFOX': fp = FirefoxProfile() fp.native_events_enabled = False if env.FIREFOX_BINARY == '': try: env.THREAD_LOCK.acquire() browser = webdriver.Firefox(firefox_profile=fp) except: if isinstance(env.RESERVED_FIREFOX_BINARY, str) and env.RESERVED_FIREFOX_BINARY != "": browser = webdriver.Firefox(firefox_profile=fp, firefox_binary=FirefoxBinary(firefox_path=env.RESERVED_FIREFOX_BINARY)) else: try: log.step_warning("try to start firefox again!") time.sleep(20) browser = webdriver.Firefox(firefox_profile=fp) except: log.handle_error() return False finally: env.THREAD_LOCK.release() else: browser = webdriver.Firefox(firefox_profile=fp, firefox_binary=FirefoxBinary(firefox_path=env.FIREFOX_BINARY)) elif env.threadlocal.TESTING_BROWSER.upper() == 'CHROME': if env.DRIVER_OF_CHROME == '': print ('DRIVER_OF_CHROME is empty.') return False os.environ['webdriver.chrome.driver'] = env.DRIVER_OF_CHROME browser = webdriver.Chrome(executable_path=env.DRIVER_OF_CHROME) elif env.threadlocal.TESTING_BROWSER.upper() == 'IE': if env.DRIVER_OF_IE == '': print ('DRIVER_OF_IE is empty.') return False # os.popen('TASKKILL /F /IM iexplore.exe') os.popen('TASKKILL /F /IM IEDriverServer.exe') dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['nativeEvents'] = False dc['acceptSslCerts'] = True os.environ['webdriver.ie.driver'] = env.DRIVER_OF_IE browser = webdriver.Ie(executable_path=env.DRIVER_OF_IE, capabilities=dc) elif env.threadlocal.TESTING_BROWSER.upper() == 'PHANTOMJS': browser = webdriver.PhantomJS(r'E:\\AutomationWork\\phantomjs-1.9.8-windows\\phantomjs.exe') if not env.threadlocal.TESTING_BROWSER in env.BROWSER_VERSION_INFO: env.BROWSER_VERSION_INFO[env.threadlocal.TESTING_BROWSER] = browser.capabilities['version'] browser.set_window_size(1366, 758) browser.set_window_position(0, 0) browser.set_page_load_timeout(300) browser.implicitly_wait(0) browser.get(url) return browser