def before_all(context: Context): # setup global variables setup = context.config.userdata context.os = setup["os"] context.env = get_env_data(os=setup["os"]) # setup page_objects if context.os.upper() == "ANDROID": context.driver = get_driver( os=context.os, browserstack_config=context.env["browserstack"]) context.android_login_page = AndroidLoginPage(context=context) context.android_question_page = AndroidQuestionPage(context=context) context.android_question_ask_page = AndroidAskQuestionPage( context=context) context.android_markets_list_page = AndroidMarketsListPage( context=context) context.android_user_profile_page = AndroidUserProfilePage( context=context) context.android_search_by_image_page = AndriodSearchByImagePage( context=context) context.android_search_by_text_page = AndriodSearchByTextPage( context=context) context.android_main_menu_section = AndriodMainMenuSection( context=context)
def set_up_browser(ctx: Context) -> None: """Setup the browser we will use with selenium for testing""" if os.getenv("BROWSER", "Chrome") == "Firefox": ctx.driver = webdriver.Firefox() else: _instantiate_chromedriver(ctx) ctx.default_window_size = ctx.driver.get_window_size()
def before_all(context: Context): # setup global variables context.env = get_env(context.config.userdata["env"]) context.driver = get_driver(context.config.userdata["browser"]) # setup page_objects context.login_page = LoginPage(context) context.main_page = MainPage(context) context.question_page = QuestionPage(context)
def before_scenario(context: Context, scenario: Scenario): logging.debug(f"Starting scenario: {scenario.name}") context.scenario_data = initialize_scenario_data() session_name = f"{scenario.feature.name} -> {scenario.name}" mobile = True if "mobile" in scenario.tags else False context.driver = start_driver_session(session_name, DRIVER_CAPABILITIES, mobile=mobile)
def start_driver_session(context: Context, session_name: str): remote_desired_capabilities = context.remote_desired_capabilities remote_desired_capabilities["name"] = session_name local_desired_capabilities = context.local_desired_capabilities if CONFIG["hub_url"]: context.driver = webdriver.Remote( desired_capabilities=remote_desired_capabilities, command_executor=CONFIG["hub_url"], ) else: browser_name = CONFIG["environments"][0]["browser"] drivers = { "chrome": webdriver.Chrome, "edge": webdriver.Edge, "firefox": webdriver.Firefox, "ie": webdriver.Ie, "phantomjs": webdriver.PhantomJS, "safari": webdriver.Safari, } print("Starting local instance of {}".format(browser_name)) if local_desired_capabilities: print("Will use following browser capabilities: {}".format( local_desired_capabilities)) if browser_name.lower() in ["firefox", "edge", "ie"]: context.driver = drivers[browser_name.lower()]( capabilities=local_desired_capabilities) elif browser_name.lower() in ["chrome", "phantomjs", "safari"]: context.driver = drivers[browser_name.lower()]( desired_capabilities=local_desired_capabilities) else: print("Will use default browser capabilities") context.driver = drivers[browser_name.lower()]() context.driver.set_page_load_timeout(time_to_wait=27) try: context.driver.maximize_window() logging.debug("Maximized the window.") except WebDriverException: logging.debug("Failed to maximize the window.") try: context.driver.set_window_size(1600, 1200) logging.warning("Set window size to 1600x1200") except WebDriverException: logging.warning("Failed to set window size, will continue as is") logging.debug("Browser Capabilities: %s", context.driver.capabilities)
def before_all(context: Context): # setup global variables setup = context.config.userdata context.driver = get_driver(browser=setup["browser"], resolution=setup["resolution"]) # setup page_objects context.main_page = MainPage(context=context) context.search_page = SearchPage(context=context) context.authentication_page = AuthenticationPage(context=context) context.registration_page = RegistrationPage(context=context) context.shopping_cart_page = ShoppingCartPage(context=context) # open application under test context.search_page.go_to_url( url="http://automationpractice.com/index.php")
def before_scenario(context: Context, scenario: Scenario): # setup page_objects if context.os.upper() == "IOS": context.driver = get_driver( os=context.os, browserstack_config=context.env["browserstack"]) context.ios_login_page = IosLoginPage(context=context) context.ios_question_page = IosQuestionPage(context=context) context.ios_question_ask_page = IosAskQuestionPage(context=context) context.ios_markets_list_page = IosMarketsListPage(context=context) context.ios_user_profile_page = IosUserProfilePage(context=context) context.ios_main_menu_section = IosMainMenuSection(context=context) context.ios_search_by_image_page = IosSearchByImagePage( context=context) context.ios_search_by_text_page = IosSearchByTextPage(context=context)
def before_scenario(context: runner.Context, scenario) -> None: context.driver = behave.use_fixture(configuration.driver, context)
def before_all(context: runner.Context): context.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def setup_webdriver(context: runner.Context) -> None: path_to_chromedriver = os.path.join(os.getcwd(), 'tutorial/chromedriver') context.driver = webdriver.Chrome(executable_path=path_to_chromedriver)
def _instantiate_chromedriver(ctx: Context) -> None: """Attempt to start the chromedriver, retrying if there are connection errors. Args: ctx: The behave context object. Raises: :py:class:`.ConnectionResetError`: If starting chromedriver fails too many times. """ # Set the selenium logs to warning only LOG.setLevel(logging.WARNING) chrome_options = webdriver.ChromeOptions() # Prevent images from loading (should decrease load times) prefs = {"profile.managed_default_content_settings.images": 1} chrome_options.add_experimental_option("prefs", prefs) chrome_options.add_argument("--no-sandbox") # Ignore CORS errors chrome_options.add_argument("--disable-web-security") # set logging capability chrome_options.set_capability("loggingPrefs", {"browser": "ALL"}) # if ctx.zap_proxy_url: # chrome_options.add_argument(f"--proxy-server={ctx.zap_proxy_url}") chrome_options.add_argument("--window-size=1920,1080") # Attempt the connection... Max attempts 3 attempts_remaining = 3 while attempts_remaining > 0: try: LOGGER.info("Instantiating chromedriver...") # use the chromedriver binary loader. this forces the location on path chromedriver_binary.add_chromedriver_to_path() ctx.driver = webdriver.Chrome(chrome_options=chrome_options) LOGGER.debug( f"Chromedriver running from {chromedriver_binary.chromedriver_filename}" ) LOGGER.info("Connected to chromedriver successfully!") break except (ConnectionResetError, ProtocolError): # one attempt used... attempts_remaining -= 1 LOGGER.warning( "Connection was refused, will try again {} more " "time{}".format( attempts_remaining, "" if attempts_remaining == 1 else "s" ) ) # sleep 3 seconds between attempts time.sleep(3) else: raise ConnectionResetError( "Failed connecting to chromedriver after exhausting all 3 attempts. Giving up!" ) # Set the latency for the browser if not defaulted to 0 latency = ctx.latency if latency != "None": LOGGER.debug(f"Non default latency was detected as: {latency}") _set_browser_latency(ctx, latency)