def get_local_chromedriver( downloads_path, headless, servername, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, incognito, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio): try: chrome_options = _set_chrome_options( downloads_path, headless, servername, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, incognito, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) elif not is_chromedriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: chromedriver not found. Installing now:") sb_install.main(override="chromedriver") sys.argv = sys_args # Put back the original sys args return webdriver.Chrome(options=chrome_options) except Exception as e: if headless: raise Exception(e) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) return webdriver.Chrome()
def main(): # Install chromedriver if not installed if not is_chromedriver_on_path(): from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: chromedriver not found. Installing now:") sb_install.main(override="chromedriver") sys.argv = sys_args # Put back the original sys args dir_path = os.path.dirname(os.path.realpath(__file__)) num_args = len(sys.argv) if (sys.argv[0].split("/")[-1] == "seleniumbase" or (sys.argv[0].split("\\")[-1] == "seleniumbase") or (sys.argv[0].split("/")[-1] == "sbase") or (sys.argv[0].split("\\")[-1] == "sbase")): if num_args < 3: invalid_run_command() else: invalid_run_command() grid_hub_command = sys.argv[2] if grid_hub_command not in ["start", "stop", "restart"]: invalid_run_command() server_ip = "127.0.0.1" verbose = "False" if num_args >= 4: options = sys.argv[3:] for option in options: if option.startswith("--hub=") and (len(option.split("--hub=")[1]) > 0): server_ip = option.split("--hub=")[1] elif option == "-v" or option == "--verbose": verbose = "True" else: invalid_run_command() data = [] data.append(server_ip) file_path = "%s/%s" % (dir_path, "ip_of_grid_hub.dat") file = codecs.open(file_path, "w+", "utf-8") file.writelines("\r\n".join(data)) file.close() data = [] data.append(verbose) file_path = "%s/%s" % (dir_path, "verbose_node_server.dat") file = codecs.open(file_path, "w+", "utf-8") file.writelines("\r\n".join(data)) file.close() from seleniumbase.utilities.selenium_grid import download_selenium_server download_selenium_server.main(force_download=False) # Only runs if needed if "linux" in sys.platform or "darwin" in sys.platform: if grid_hub_command == "start": subprocess.check_call(dir_path + "/grid-node start", shell=True) elif grid_hub_command == "restart": subprocess.check_call(dir_path + "/grid-node restart", shell=True) elif grid_hub_command == "stop": subprocess.check_call(dir_path + "/grid-node stop", shell=True) else: invalid_run_command() else: if grid_hub_command == "start" or grid_hub_command == "restart": shell_command = ( """java -jar %s/selenium-server-standalone.jar -role node """ """-hub http://%s:4444/grid/register -browser browser""" """Name=chrome,maxInstances=5,version=ANY,seleniumProtocol=""" """WebDriver -browser browserName=firefox,maxInstances=5,""" """version=ANY,seleniumProtocol=WebDriver -browser browser""" """Name=MicrosoftEdge,maxInstances=1,version=ANY,""" """platform=WIN10,seleniumProtocol=WebDriver""" % (dir_path, server_ip)) print("\nStarting Selenium-WebDriver Grid node...\n") print(shell_command) print("") print("""Browser Sessions: http://127.0.0.1:5555""" """/wd/hub/static/resource/hub.html""") print("") subprocess.check_call(shell_command, shell=True) elif grid_hub_command == "stop": print("") print("To stop the Grid node, use CTRL-C inside the server shell!") print("") else: invalid_run_command()
def main(): command = None command_args = None num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] command = command.lower() if command == "install": if len(command_args) >= 1: from seleniumbase.console_scripts import sb_install sb_install.main() else: show_basic_usage() show_install_usage() elif command == "mkdir": if len(command_args) >= 1: from seleniumbase.console_scripts import sb_mkdir sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "mkfile": if len(command_args) >= 1: from seleniumbase.console_scripts import sb_mkfile sb_mkfile.main() else: show_basic_usage() show_mkfile_usage() elif command == "mkpres": if len(command_args) >= 1: from seleniumbase.console_scripts import sb_mkpres sb_mkpres.main() else: show_basic_usage() show_mkpres_usage() elif command == "mkchart": if len(command_args) >= 1: from seleniumbase.console_scripts import sb_mkchart sb_mkchart.main() else: show_basic_usage() show_mkchart_usage() elif command == "convert": if len(command_args) == 1: from seleniumbase.utilities.selenium_ide import convert_ide convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "print": if len(command_args) >= 1: if sys.version_info[0] == 2: c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX cr = colorama.Style.RESET_ALL msg = '"sbase print" does NOT support Python 2! ' msg += 'Try using the Unix "cat" command instead!' message = "\n" + c5 + msg + cr + "\n" print("") raise Exception(message) from seleniumbase.console_scripts import sb_print sb_print.main() else: show_basic_usage() show_print_usage() elif command == "translate": if len(command_args) >= 1: if sys.version_info[0] == 2: c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX cr = colorama.Style.RESET_ALL msg = "The SeleniumBase Translator does NOT support Python 2!" message = "\n" + c5 + msg + cr + "\n" print("") raise Exception(message) from seleniumbase.translate import translator translator.main() else: show_basic_usage() show_translate_usage() elif command == "extract-objects" or command == "extract_objects": if len(command_args) >= 1: from seleniumbase.console_scripts import objectify objectify.extract_objects() else: show_basic_usage() show_extract_objects_usage() elif command == "inject-objects" or command == "inject_objects": if len(command_args) >= 1: from seleniumbase.console_scripts import objectify objectify.inject_objects() else: show_basic_usage() show_inject_objects_usage() elif command == "objectify": if len(command_args) >= 1: from seleniumbase.console_scripts import objectify objectify.objectify() else: show_basic_usage() show_objectify_usage() elif command == "revert-objects" or command == "revert_objects": if len(command_args) >= 1: from seleniumbase.console_scripts import objectify objectify.revert_objects() else: show_basic_usage() show_revert_objects_usage() elif command == "encrypt" or command == "obfuscate": if len(command_args) >= 0: from seleniumbase.common import obfuscate obfuscate.main() else: show_basic_usage() show_encrypt_usage() elif command == "decrypt" or command == "unobfuscate": if len(command_args) >= 0: from seleniumbase.common import unobfuscate unobfuscate.main() else: show_basic_usage() show_decrypt_usage() elif command == "download": if len(command_args) >= 1 and command_args[0].lower() == "server": from seleniumbase.utilities.selenium_grid import ( download_selenium_server) download_selenium_server.main(force_download=True) else: show_basic_usage() show_download_usage() elif command == "grid-hub" or command == "grid_hub": if len(command_args) >= 1: from seleniumbase.utilities.selenium_grid import grid_hub grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node" or command == "grid_node": if len(command_args) >= 1: from seleniumbase.utilities.selenium_grid import grid_node grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "version" or command == "--version": if len(command_args) == 0: from seleniumbase.console_scripts import logo_helper seleniumbase_logo = logo_helper.get_seleniumbase_logo() print(seleniumbase_logo) print("") show_package_location() show_version_info() print("") else: show_basic_usage() elif command == "methods" or command == "--methods": show_methods() elif command == "options" or command == "--options": show_options() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "mkfile": print("") show_mkfile_usage() return elif command_args[0] == "mkpres": print("") show_mkpres_usage() return elif command_args[0] == "mkchart": print("") show_mkchart_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "print": print("") show_print_usage() return elif command_args[0] == "translate": print("") show_translate_usage() return elif command_args[0] == "extract-objects": print("") show_extract_objects_usage() return elif command_args[0] == "inject-objects": print("") show_inject_objects_usage() return elif command_args[0] == "objectify": print("") show_objectify_usage() return elif command_args[0] == "revert-objects": print("") show_revert_objects_usage() return elif command_args[0] == "encrypt": print("") show_encrypt_usage() return elif command_args[0] == "obfuscate": print("") show_encrypt_usage() return elif command_args[0] == "decrypt": print("") show_decrypt_usage() return elif command_args[0] == "unobfuscate": print("") show_decrypt_usage() return elif command_args[0] == "download": print("") show_download_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()
def get_local_driver(browser_name, headless, servername, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, no_sandbox, disable_gpu, incognito, guest_mode, devtools, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio): ''' Spins up a new web browser and returns the driver. Can also be used to spin up additional browsers for the same test. ''' downloads_path = download_helper.get_downloads_folder() download_helper.reset_downloads_folder() if browser_name == constants.Browser.FIREFOX: try: try: # Use Geckodriver for Firefox if it's on the PATH profile = _create_firefox_profile(downloads_path, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_capabilities['marionette'] = True options = webdriver.FirefoxOptions() if headless: options.add_argument('-headless') firefox_capabilities['moz:firefoxOptions'] = ({ 'args': ['-headless'] }) if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER): try: make_driver_executable_if_not(LOCAL_GECKODRIVER) except Exception as e: logging.debug("\nWarning: Could not make geckodriver" " executable: %s" % e) elif not is_geckodriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: geckodriver not found!" " Installing now:") try: sb_install.main(override="geckodriver") except Exception as e: print("\nWarning: Could not install geckodriver: " "%s" % e) sys.argv = sys_args # Put back the original sys args if "linux" in PLATFORM or not headless: firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities) else: firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities, options=options) except Exception: profile = _create_firefox_profile(downloads_path, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities) return firefox_driver except Exception as e: if headless: raise Exception(e) return webdriver.Firefox() elif browser_name == constants.Browser.INTERNET_EXPLORER: if not IS_WINDOWS: raise Exception( "IE Browser is for Windows-based operating systems only!") from selenium.webdriver.ie.options import Options ie_options = Options() ie_options.ignore_protected_mode_settings = False ie_options.ignore_zoom_level = True ie_options.require_window_focus = False ie_options.native_events = True ie_options.full_page_screenshot = True ie_options.persistent_hover = True ie_capabilities = ie_options.to_capabilities() if LOCAL_IEDRIVER and os.path.exists(LOCAL_IEDRIVER): try: make_driver_executable_if_not(LOCAL_IEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make iedriver" " executable: %s" % e) return webdriver.Ie(capabilities=ie_capabilities) elif browser_name == constants.Browser.EDGE: try: chrome_options = _set_chrome_options( downloads_path, headless, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, no_sandbox, disable_gpu, incognito, guest_mode, devtools, user_data_dir, extension_zip, extension_dir, servername, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER): try: make_driver_executable_if_not(LOCAL_EDGEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make edgedriver" " executable: %s" % e) elif not is_edgedriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: msedgedriver not found. Installing now:") sb_install.main(override="edgedriver") sys.argv = sys_args # Put back the original sys args return webdriver.Chrome(executable_path=LOCAL_EDGEDRIVER, options=chrome_options) except Exception as e: if headless: raise Exception(e) if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER): try: make_driver_executable_if_not(LOCAL_EDGEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make edgedriver" " executable: %s" % e) return webdriver.Chrome(executable_path=LOCAL_EDGEDRIVER) elif browser_name == constants.Browser.SAFARI: arg_join = " ".join(sys.argv) if ("-n" in sys.argv) or ("-n=" in arg_join) or (arg_join == "-c"): # Skip if multithreaded raise Exception("Can't run Safari tests in multi-threaded mode!") return webdriver.Safari() elif browser_name == constants.Browser.OPERA: if LOCAL_OPERADRIVER and os.path.exists(LOCAL_OPERADRIVER): try: make_driver_executable_if_not(LOCAL_OPERADRIVER) except Exception as e: logging.debug("\nWarning: Could not make operadriver" " executable: %s" % e) return webdriver.Opera() elif browser_name == constants.Browser.PHANTOM_JS: with warnings.catch_warnings(): # Ignore "PhantomJS has been deprecated" UserWarning warnings.simplefilter("ignore", category=UserWarning) return webdriver.PhantomJS() elif browser_name == constants.Browser.GOOGLE_CHROME: try: chrome_options = _set_chrome_options( downloads_path, headless, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, no_sandbox, disable_gpu, incognito, guest_mode, devtools, user_data_dir, extension_zip, extension_dir, servername, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) elif not is_chromedriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: chromedriver not found. Installing now:") sb_install.main(override="chromedriver") sys.argv = sys_args # Put back the original sys args return webdriver.Chrome(options=chrome_options) except Exception as e: if headless: raise Exception(e) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) return webdriver.Chrome() else: raise Exception("%s is not a valid browser option for this system!" % browser_name)
def get_local_driver( browser_name, headless, locale_code, servername, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_ws, enable_sync, use_auto_ext, no_sandbox, disable_gpu, incognito, guest_mode, devtools, swiftshader, block_images, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio): ''' Spins up a new web browser and returns the driver. Can also be used to spin up additional browsers for the same test. ''' downloads_path = download_helper.get_downloads_folder() download_helper.reset_downloads_folder() if browser_name == constants.Browser.FIREFOX: try: try: # Use Geckodriver for Firefox if it's on the PATH profile = _create_firefox_profile( downloads_path, locale_code, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_capabilities['marionette'] = True options = webdriver.FirefoxOptions() if headless: options.add_argument('-headless') firefox_capabilities['moz:firefoxOptions'] = ( {'args': ['-headless']}) if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER): try: make_driver_executable_if_not(LOCAL_GECKODRIVER) except Exception as e: logging.debug("\nWarning: Could not make geckodriver" " executable: %s" % e) elif not is_geckodriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: geckodriver not found!" " Installing now:") try: sb_install.main(override="geckodriver") except Exception as e: print("\nWarning: Could not install geckodriver: " "%s" % e) sys.argv = sys_args # Put back the original sys args if "linux" in PLATFORM or not headless: firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities) else: firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities, options=options) except Exception: profile = _create_firefox_profile( downloads_path, locale_code, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities) return firefox_driver except Exception as e: if headless: raise Exception(e) return webdriver.Firefox() elif browser_name == constants.Browser.INTERNET_EXPLORER: if not IS_WINDOWS: raise Exception( "IE Browser is for Windows-based operating systems only!") from selenium.webdriver.ie.options import Options ie_options = Options() ie_options.ignore_protected_mode_settings = False ie_options.ignore_zoom_level = True ie_options.require_window_focus = False ie_options.native_events = True ie_options.full_page_screenshot = True ie_options.persistent_hover = True ie_capabilities = ie_options.to_capabilities() if LOCAL_IEDRIVER and os.path.exists(LOCAL_IEDRIVER): try: make_driver_executable_if_not(LOCAL_IEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make iedriver" " executable: %s" % e) return webdriver.Ie(capabilities=ie_capabilities) elif browser_name == constants.Browser.EDGE: try: chrome_options = _set_chrome_options( browser_name, downloads_path, headless, locale_code, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_ws, enable_sync, use_auto_ext, no_sandbox, disable_gpu, incognito, guest_mode, devtools, swiftshader, block_images, user_data_dir, extension_zip, extension_dir, servername, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER): try: make_driver_executable_if_not(LOCAL_EDGEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make edgedriver" " executable: %s" % e) elif not is_edgedriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: msedgedriver not found. Installing now:") sb_install.main(override="edgedriver") sys.argv = sys_args # Put back the original sys args # For Microsoft Edge (Chromium) version 79 or lower return webdriver.Chrome(executable_path=LOCAL_EDGEDRIVER, options=chrome_options) except Exception: # For Microsoft Edge (Chromium) version 80 or higher from msedge.selenium_tools import Edge, EdgeOptions if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER): try: make_driver_executable_if_not(LOCAL_EDGEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make edgedriver" " executable: %s" % e) edge_options = EdgeOptions() edge_options.use_chromium = True prefs = { "download.default_directory": downloads_path, "local_discovery.notifications_enabled": False, "credentials_enable_service": False, "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": False, "safebrowsing.disable_download_protection": True, "profile": { "password_manager_enabled": False, "default_content_setting_values.automatic_downloads": 1, "managed_default_content_settings.automatic_downloads": 1, "default_content_settings.popups": 0, "managed_default_content_settings.popups": 0 } } if locale_code: prefs["intl.accept_languages"] = locale_code if block_images: prefs["profile.managed_default_content_settings.images"] = 2 edge_options.add_experimental_option("prefs", prefs) edge_options.add_experimental_option("w3c", True) edge_options.add_experimental_option( "useAutomationExtension", False) edge_options.add_experimental_option( "excludeSwitches", ["enable-automation", "enable-logging"]) if guest_mode: edge_options.add_argument("--guest") if headless: edge_options.add_argument("--headless") if mobile_emulator: emulator_settings = {} device_metrics = {} if type(device_width) is int and ( type(device_height) is int and ( type(device_pixel_ratio) is int)): device_metrics["width"] = device_width device_metrics["height"] = device_height device_metrics["pixelRatio"] = device_pixel_ratio else: device_metrics["width"] = 411 device_metrics["height"] = 731 device_metrics["pixelRatio"] = 3 emulator_settings["deviceMetrics"] = device_metrics if user_agent: emulator_settings["userAgent"] = user_agent edge_options.add_experimental_option( "mobileEmulation", emulator_settings) edge_options.add_argument("--enable-sync") edge_options.add_argument("--disable-infobars") edge_options.add_argument("--disable-save-password-bubble") edge_options.add_argument("--disable-single-click-autofill") edge_options.add_argument( "--disable-autofill-keyboard-accessory-view[8]") edge_options.add_argument("--disable-translate") if not enable_ws: edge_options.add_argument("--disable-web-security") edge_options.add_argument("--homepage=about:blank") edge_options.add_argument("--dns-prefetch-disable") edge_options.add_argument("--dom-automation") edge_options.add_argument("--disable-hang-monitor") edge_options.add_argument("--disable-prompt-on-repost") if proxy_string: edge_options.add_argument('--proxy-server=%s' % proxy_string) edge_options.add_argument("--test-type") edge_options.add_argument("--log-level=3") edge_options.add_argument("--no-first-run") edge_options.add_argument("--ignore-certificate-errors") if devtools and not headless: edge_options.add_argument("--auto-open-devtools-for-tabs") edge_options.add_argument("--allow-file-access-from-files") edge_options.add_argument("--allow-insecure-localhost") edge_options.add_argument("--allow-running-insecure-content") if user_agent: edge_options.add_argument("--user-agent=%s" % user_agent) edge_options.add_argument("--no-sandbox") if swiftshader: edge_options.add_argument("--use-gl=swiftshader") else: edge_options.add_argument("--disable-gpu") if "linux" in PLATFORM: edge_options.add_argument("--disable-dev-shm-usage") capabilities = edge_options.to_capabilities() capabilities["platform"] = '' return Edge( executable_path=LOCAL_EDGEDRIVER, capabilities=capabilities) elif browser_name == constants.Browser.SAFARI: arg_join = " ".join(sys.argv) if ("-n" in sys.argv) or ("-n=" in arg_join) or (arg_join == "-c"): # Skip if multithreaded raise Exception("Can't run Safari tests in multi-threaded mode!") safari_capabilities = _set_safari_capabilities() return webdriver.Safari(desired_capabilities=safari_capabilities) elif browser_name == constants.Browser.OPERA: try: if LOCAL_OPERADRIVER and os.path.exists(LOCAL_OPERADRIVER): try: make_driver_executable_if_not(LOCAL_OPERADRIVER) except Exception as e: logging.debug("\nWarning: Could not make operadriver" " executable: %s" % e) opera_options = _set_chrome_options( browser_name, downloads_path, headless, locale_code, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_ws, enable_sync, use_auto_ext, no_sandbox, disable_gpu, incognito, guest_mode, devtools, swiftshader, block_images, user_data_dir, extension_zip, extension_dir, servername, mobile_emulator, device_width, device_height, device_pixel_ratio) opera_options.headless = False # No support for headless Opera return webdriver.Opera(options=opera_options) except Exception: return webdriver.Opera() elif browser_name == constants.Browser.PHANTOM_JS: with warnings.catch_warnings(): # Ignore "PhantomJS has been deprecated" UserWarning warnings.simplefilter("ignore", category=UserWarning) return webdriver.PhantomJS() elif browser_name == constants.Browser.GOOGLE_CHROME: try: chrome_options = _set_chrome_options( browser_name, downloads_path, headless, locale_code, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_ws, enable_sync, use_auto_ext, no_sandbox, disable_gpu, incognito, guest_mode, devtools, swiftshader, block_images, user_data_dir, extension_zip, extension_dir, servername, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) elif not is_chromedriver_on_path(): args = " ".join(sys.argv) if not ("-n" in sys.argv or "-n=" in args or args == "-c"): # (Not multithreaded) from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: chromedriver not found. Installing now:") sb_install.main(override="chromedriver") sys.argv = sys_args # Put back the original sys args if not headless or "linux" not in PLATFORM: return webdriver.Chrome(options=chrome_options) else: # Running headless on Linux try: return webdriver.Chrome(options=chrome_options) except Exception: # Use the virtual display on Linux during headless errors logging.debug("\nWarning: Chrome failed to launch in" " headless mode. Attempting to use the" " SeleniumBase virtual display on Linux...") chrome_options.headless = False return webdriver.Chrome(options=chrome_options) except Exception as e: if headless: raise Exception(e) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): try: make_driver_executable_if_not(LOCAL_CHROMEDRIVER) except Exception as e: logging.debug("\nWarning: Could not make chromedriver" " executable: %s" % e) return webdriver.Chrome() else: raise Exception( "%s is not a valid browser option for this system!" % browser_name)
def main(): command = None command_args = None num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] command = command.lower() if command == "install": if len(command_args) >= 1: sb_install.main() else: show_basic_usage() show_install_usage() elif command == "convert": if len(command_args) == 1: convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "mkdir": if len(command_args) >= 1: sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "download": if len(command_args) >= 1: download_selenium_server.main(force_download=True) else: show_basic_usage() show_download_usage() elif command == "grid-hub": if len(command_args) >= 1: grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node": if len(command_args) >= 1: grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "download": print("") show_download_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()
def main(): command = None command_args = None num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] command = command.lower() if command == "install": if len(command_args) >= 1: sb_install.main() else: show_basic_usage() show_install_usage() elif command == "mkdir": if len(command_args) >= 1: sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "convert": if len(command_args) == 1: convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "extract-objects" or command == "extract_objects": if len(command_args) >= 1: objectify.extract_objects() else: show_basic_usage() show_extract_objects_usage() elif command == "inject-objects" or command == "inject_objects": if len(command_args) >= 1: objectify.inject_objects() else: show_basic_usage() show_inject_objects_usage() elif command == "objectify": if len(command_args) >= 1: objectify.objectify() else: show_basic_usage() show_objectify_usage() elif command == "revert-objects" or command == "revert_objects": if len(command_args) >= 1: objectify.revert_objects() else: show_basic_usage() show_revert_objects_usage() elif command == "encrypt" or command == "obfuscate": if len(command_args) >= 0: obfuscate.main() else: show_basic_usage() show_encrypt_usage() elif command == "decrypt" or command == "unobfuscate": if len(command_args) >= 0: unobfuscate.main() else: show_basic_usage() show_decrypt_usage() elif command == "download": if len(command_args) >= 1 and command_args[0].lower() == "server": download_selenium_server.main(force_download=True) else: show_basic_usage() show_download_usage() elif command == "grid-hub" or command == "grid_hub": if len(command_args) >= 1: grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node" or command == "grid_node": if len(command_args) >= 1: grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "version" or command == "--version": if len(command_args) == 0: show_version_info() else: show_basic_usage() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "extract-objects": print("") show_extract_objects_usage() return elif command_args[0] == "inject-objects": print("") show_inject_objects_usage() return elif command_args[0] == "objectify": print("") show_objectify_usage() return elif command_args[0] == "revert-objects": print("") show_revert_objects_usage() return elif command_args[0] == "encrypt": print("") show_encrypt_usage() return elif command_args[0] == "obfuscate": print("") show_encrypt_usage() return elif command_args[0] == "decrypt": print("") show_decrypt_usage() return elif command_args[0] == "unobfuscate": print("") show_decrypt_usage() return elif command_args[0] == "download": print("") show_download_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()
def get_local_driver(browser_name, headless, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio): ''' Spins up a new web browser and returns the driver. Can also be used to spin up additional browsers for the same test. ''' downloads_path = download_helper.get_downloads_folder() download_helper.reset_downloads_folder() if browser_name == constants.Browser.FIREFOX: try: try: # Use Geckodriver for Firefox if it's on the PATH profile = _create_firefox_profile(downloads_path, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_capabilities['marionette'] = True options = webdriver.FirefoxOptions() if headless: options.add_argument('-headless') if LOCAL_GECKODRIVER and os.path.exists(LOCAL_GECKODRIVER): make_driver_executable_if_not(LOCAL_GECKODRIVER) elif not is_geckodriver_on_path(): if not "".join(sys.argv) == "-c": # Skip if multithreaded from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: geckodriver not found." " Installing now:") sb_install.main(override="geckodriver") sys.argv = sys_args # Put back the original sys args firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities, options=options) except WebDriverException: # Don't use Geckodriver: Only works for old versions of Firefox profile = _create_firefox_profile(downloads_path, proxy_string, user_agent, disable_csp) firefox_capabilities = DesiredCapabilities.FIREFOX.copy() firefox_capabilities['marionette'] = False firefox_driver = webdriver.Firefox( firefox_profile=profile, capabilities=firefox_capabilities) return firefox_driver except Exception as e: if headless: raise Exception(e) return webdriver.Firefox() elif browser_name == constants.Browser.INTERNET_EXPLORER: if not IS_WINDOWS: raise Exception( "IE Browser is for Windows-based operating systems only!") from selenium.webdriver.ie.options import Options ie_options = Options() ie_options.ignore_protected_mode_settings = False ie_options.ignore_zoom_level = True ie_options.require_window_focus = False ie_options.native_events = True ie_options.full_page_screenshot = True ie_options.persistent_hover = True ie_capabilities = ie_options.to_capabilities() if LOCAL_IEDRIVER and os.path.exists(LOCAL_IEDRIVER): make_driver_executable_if_not(LOCAL_IEDRIVER) return webdriver.Ie(capabilities=ie_capabilities) elif browser_name == constants.Browser.EDGE: if LOCAL_EDGEDRIVER and os.path.exists(LOCAL_EDGEDRIVER): make_driver_executable_if_not(LOCAL_EDGEDRIVER) # The new Microsoft Edge browser is based on Chromium chrome_options = _set_chrome_options( downloads_path, headless, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio) return webdriver.Chrome(executable_path=LOCAL_EDGEDRIVER, options=chrome_options) else: return webdriver.Edge() elif browser_name == constants.Browser.SAFARI: if "".join(sys.argv) == "-c": # Skip if multithreaded raise Exception("Can't run Safari tests in multi-threaded mode!") return webdriver.Safari() elif browser_name == constants.Browser.OPERA: if LOCAL_OPERADRIVER and os.path.exists(LOCAL_OPERADRIVER): make_driver_executable_if_not(LOCAL_OPERADRIVER) return webdriver.Opera() elif browser_name == constants.Browser.PHANTOM_JS: with warnings.catch_warnings(): # Ignore "PhantomJS has been deprecated" UserWarning warnings.simplefilter("ignore", category=UserWarning) return webdriver.PhantomJS() elif browser_name == constants.Browser.GOOGLE_CHROME: try: chrome_options = _set_chrome_options( downloads_path, headless, proxy_string, proxy_auth, proxy_user, proxy_pass, user_agent, disable_csp, enable_sync, user_data_dir, extension_zip, extension_dir, mobile_emulator, device_width, device_height, device_pixel_ratio) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): make_driver_executable_if_not(LOCAL_CHROMEDRIVER) elif not is_chromedriver_on_path(): if not "".join(sys.argv) == "-c": # Skip if multithreaded from seleniumbase.console_scripts import sb_install sys_args = sys.argv # Save a copy of current sys args print("\nWarning: chromedriver not found. Installing now:") sb_install.main(override="chromedriver") sys.argv = sys_args # Put back the original sys args return webdriver.Chrome(options=chrome_options) except Exception as e: if headless: raise Exception(e) if LOCAL_CHROMEDRIVER and os.path.exists(LOCAL_CHROMEDRIVER): make_driver_executable_if_not(LOCAL_CHROMEDRIVER) return webdriver.Chrome() else: raise Exception("%s is not a valid browser option for this system!" % browser_name)
def main(): num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] if command == "install": if len(command_args) >= 1: sb_install.main() else: show_basic_usage() show_install_usage() elif command == "convert": if len(command_args) == 1: convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "mkdir": if len(command_args) >= 1: sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "grid-hub": if len(command_args) >= 1: grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node": if len(command_args) >= 1: grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()
def main(): command = None command_args = None num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] command = command.lower() if command == "install": if len(command_args) >= 1: sb_install.main() else: show_basic_usage() show_install_usage() elif command == "convert": if len(command_args) == 1: convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "extract-objects" or command == "extract_objects": if len(command_args) >= 1: objectify.extract_objects() else: show_basic_usage() show_extract_objects_usage() elif command == "inject-objects" or command == "inject_objects": if len(command_args) >= 1: objectify.inject_objects() else: show_basic_usage() show_inject_objects_usage() elif command == "objectify": if len(command_args) >= 1: objectify.objectify() else: show_basic_usage() show_objectify_usage() elif command == "revert-objects" or command == "revert_objects": if len(command_args) >= 1: objectify.revert_objects() else: show_basic_usage() show_revert_objects_usage() elif command == "mkdir": if len(command_args) >= 1: sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "download": if len(command_args) >= 1 and command_args[0].lower() == "server": download_selenium_server.main(force_download=True) else: show_basic_usage() show_download_usage() elif command == "grid-hub" or command == "grid_hub": if len(command_args) >= 1: grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node" or command == "grid_node": if len(command_args) >= 1: grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "extract-objects": print("") show_extract_objects_usage() return elif command_args[0] == "inject-objects": print("") show_inject_objects_usage() return elif command_args[0] == "objectify": print("") show_objectify_usage() return elif command_args[0] == "revert-objects": print("") show_revert_objects_usage() return elif command_args[0] == "download": print("") show_download_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()
def main(): command = None command_args = None num_args = len(sys.argv) if num_args == 1: show_usage() return elif num_args == 2: command = sys.argv[1] command_args = [] elif num_args > 2: command = sys.argv[1] command_args = sys.argv[2:] command = command.lower() if command == "install": if len(command_args) >= 1: sb_install.main() else: show_basic_usage() show_install_usage() elif command == "mkdir": if len(command_args) >= 1: sb_mkdir.main() else: show_basic_usage() show_mkdir_usage() elif command == "convert": if len(command_args) == 1: convert_ide.main() else: show_basic_usage() show_convert_usage() elif command == "translate": if len(command_args) >= 1: if sys.version_info[0] == 2: colorama.init(autoreset=True) c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX cr = colorama.Style.RESET_ALL msg = "The SeleniumBase Translator does NOT support Python 2!" message = "\n" + c5 + msg + cr + "\n" print("") raise Exception(message) from seleniumbase.translate import translator translator.main() else: show_basic_usage() show_translate_usage() elif command == "extract-objects" or command == "extract_objects": if len(command_args) >= 1: objectify.extract_objects() else: show_basic_usage() show_extract_objects_usage() elif command == "inject-objects" or command == "inject_objects": if len(command_args) >= 1: objectify.inject_objects() else: show_basic_usage() show_inject_objects_usage() elif command == "objectify": if len(command_args) >= 1: objectify.objectify() else: show_basic_usage() show_objectify_usage() elif command == "revert-objects" or command == "revert_objects": if len(command_args) >= 1: objectify.revert_objects() else: show_basic_usage() show_revert_objects_usage() elif command == "encrypt" or command == "obfuscate": if len(command_args) >= 0: obfuscate.main() else: show_basic_usage() show_encrypt_usage() elif command == "decrypt" or command == "unobfuscate": if len(command_args) >= 0: unobfuscate.main() else: show_basic_usage() show_decrypt_usage() elif command == "download": if len(command_args) >= 1 and command_args[0].lower() == "server": download_selenium_server.main(force_download=True) else: show_basic_usage() show_download_usage() elif command == "grid-hub" or command == "grid_hub": if len(command_args) >= 1: grid_hub.main() else: show_basic_usage() show_grid_hub_usage() elif command == "grid-node" or command == "grid_node": if len(command_args) >= 1: grid_node.main() else: show_basic_usage() show_grid_node_usage() elif command == "version" or command == "--version": if len(command_args) == 0: show_version_info() else: show_basic_usage() elif command == "help" or command == "--help": if len(command_args) >= 1: if command_args[0] == "install": print("") show_install_usage() return elif command_args[0] == "mkdir": print("") show_mkdir_usage() return elif command_args[0] == "convert": print("") show_convert_usage() return elif command_args[0] == "translate": print("") show_translate_usage() return elif command_args[0] == "extract-objects": print("") show_extract_objects_usage() return elif command_args[0] == "inject-objects": print("") show_inject_objects_usage() return elif command_args[0] == "objectify": print("") show_objectify_usage() return elif command_args[0] == "revert-objects": print("") show_revert_objects_usage() return elif command_args[0] == "encrypt": print("") show_encrypt_usage() return elif command_args[0] == "obfuscate": print("") show_encrypt_usage() return elif command_args[0] == "decrypt": print("") show_decrypt_usage() return elif command_args[0] == "unobfuscate": print("") show_decrypt_usage() return elif command_args[0] == "download": print("") show_download_usage() return elif command_args[0] == "grid-hub": print("") show_grid_hub_usage() return elif command_args[0] == "grid-node": print("") show_grid_node_usage() return show_detailed_help() else: show_usage()