def find_in_region_from_pattern( outer_pattern: Pattern, inner_pattern: Pattern, outer_pattern_timeout=Settings.auto_wait_timeout, inner_pattern_timeout=Settings.auto_wait_timeout, ): """ Finds pattern in region created from another pattern :param outer_pattern: Pattern for region creation :param inner_pattern: Pattern to find in region :param outer_pattern_timeout: Time to finding outer_pattern :param inner_pattern_timeout: Time to finding inner_pattern, :return: Boolean. True if inner_pattern found in outer_pattern region :raises: ValueError and APIHelperError """ if not isinstance(outer_pattern, Pattern) or not isinstance( inner_pattern, Pattern): raise ValueError(INVALID_GENERIC_INPUT) try: wait(outer_pattern, outer_pattern_timeout) logger.debug("Outer pattern found.") except FindError: raise APIHelperError("Can't find the outer pattern.") width, height = outer_pattern.get_size() region = Region( image_find(outer_pattern).x, image_find(outer_pattern).y, width, height) pattern_found = exists(inner_pattern, inner_pattern_timeout, region=region) return pattern_found
def click_cancel_button(): """Click cancel button.""" cancel_button_pattern = Pattern("cancel_button.png").similar(0.7) try: wait(cancel_button_pattern, 10) logger.debug("Cancel button found.") click(cancel_button_pattern) except FindError: raise APIHelperError("Can't find the cancel button, aborting.")
def close_customize_page(): """Close the 'Customize...' page by pressing the 'Done' button.""" customize_done_button_pattern = Pattern("customize_done_button.png").similar(0.7) try: wait(customize_done_button_pattern, 10) logger.debug("Done button found.") click(customize_done_button_pattern) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) except FindError: raise APIHelperError("Can't find the Done button in the page, aborting.")
def copy_to_clipboard(): """Return the value copied to clipboard.""" time.sleep(Settings.DEFAULT_UI_DELAY) edit_select_all() time.sleep(Settings.DEFAULT_UI_DELAY) edit_copy() time.sleep(Settings.DEFAULT_UI_DELAY) value = get_clipboard() time.sleep(Settings.DEFAULT_UI_DELAY) logger.debug("Copied to clipboard: %s" % value) return value
def get_firefox_locale_from_about_config(): """Returns the Firefox locale from 'about:config' page.""" try: value_str = get_pref_value( "browser.newtabpage.activity-stream.feeds.section.topstories.options" ) logger.debug(value_str) temp = json.loads(value_str) return str(temp["stories_endpoint"]).split("&locale_lang=")[1].split("&")[0] except (APIHelperError, KeyError): raise APIHelperError("Pref format to determine locale has changed.")
def confirm_close_multiple_tabs(): """Click confirm 'Close all tabs' for warning popup when multiple tabs are opened. """ close_all_tabs_button_pattern = Pattern("close_all_tabs_button.png") try: wait(close_all_tabs_button_pattern, 5) logger.debug('"Close all tabs" warning popup found.') type(Key.ENTER) except FindError: logger.debug('Couldn\'t find the "Close all tabs" warning popup.') pass
def close_content_blocking_pop_up(): """Closes the content blocking pop up""" pop_up_region = Screen().new_region(0, 50, Screen.SCREEN_WIDTH / 2, Screen.SCREEN_HEIGHT / 2) try: pop_up_region.wait(ContentBlocking.POP_UP_ENABLED, 5) logger.debug( "Content blocking is present on the page and can be closed.") pop_up_region.click(ContentBlocking.CLOSE_CB_POP_UP) except FindError: logger.debug("Couldn't find the Content blocking pop up.") pass
def change_preference(pref_name, value): """Change the value for a specific preference. :param pref_name: Preference to be changed. :param value: Preference's value after the change. :return: None. """ if not isinstance(value, str): value = str(value).lower() try: new_tab() navigate("about:config") time.sleep(Settings.DEFAULT_UI_DELAY_LONG) type(Key.SPACE) time.sleep(Settings.DEFAULT_UI_DELAY) paste(pref_name) time.sleep(Settings.DEFAULT_UI_DELAY) type(Key.TAB) time.sleep(Settings.DEFAULT_UI_DELAY) try: retrieved_value = copy_to_clipboard().split("\t")[1] except Exception: raise APIHelperError("Failed to retrieve preference value.") if retrieved_value == value: logger.debug("Flag is already set to value:" + value) return None else: type(Key.ENTER) if not (value == "true" or value == "false"): try: paste(value) type(Key.ENTER) except FindError: pass close_tab() except Exception: raise APIHelperError( "Could not set value: %s to preference: %s" % (value, pref_name) )
def change_preference(pref_name, value): """Change the value for a specific preference. :param pref_name: Preference to be changed. :param value: Preference's value after the change. :return: None. """ try: new_tab() navigate("about:config") time.sleep(Settings.DEFAULT_UI_DELAY_LONG) type(Key.SPACE) time.sleep(Settings.DEFAULT_UI_DELAY) paste(pref_name) time.sleep(Settings.DEFAULT_UI_DELAY) type(Key.TAB) time.sleep(Settings.DEFAULT_UI_DELAY) try: retrieved_value = copy_to_clipboard() except Exception: raise APIHelperError("Failed to retrieve preference value.") if retrieved_value == value: logger.debug("Flag is already set to value:" + value) return None else: type(Key.ENTER) dialog_box_pattern = Pattern("preference_dialog_icon.png") try: wait(dialog_box_pattern, 3) paste(value) type(Key.ENTER) except FindError: pass close_tab() except Exception: raise APIHelperError( "Could not set value: %s to preference: %s" % (value, pref_name) )
def open_hamburger_menu(option=None): """Open a specific option from the hamburger menu. If no option is given, just open the menu. In that case, the calling test must close the menu on its own. :param option: Hamburger menu option to be selected. :return: None. """ hamburger_menu_pattern = NavBar.HAMBURGER_MENU region = Screen.UPPER_RIGHT_CORNER sign_in_to_firefox_pattern = Pattern("sign_in_to_firefox.png") option_list = { 'Restore Previous Session': 5, 'Customize': 14, 'Print': 17, 'Web Developer': 20, 'Help': 22, 'Exit': 23, 'Quit': 23 } try: region.wait(hamburger_menu_pattern, 5) logger.debug("Hamburger menu found.") except FindError: raise APIHelperError( 'Can\'t find the "hamburger menu" in the page, aborting test.') else: try: region.click(hamburger_menu_pattern) region.wait(sign_in_to_firefox_pattern, 10) if option is not None: reps = option_list[option] count = 0 while count < reps: time.sleep(0.5) type(Key.TAB) count = count + 1 time.sleep(1) type(Key.ENTER) except FindError: raise APIHelperError("Can't click the menu button. Aborting test.")
def click_hamburger_menu_option(option): """Click on a specific option from the hamburger menu. :param option: Hamburger menu option to be clicked. :return: The region created starting from the hamburger menu pattern. """ hamburger_menu_pattern = NavBar.HAMBURGER_MENU try: wait(hamburger_menu_pattern, 5) logger.debug("Hamburger menu found.") except FindError: raise APIHelperError( 'Can\'t find the "hamburger menu" in the page, aborting test.' ) else: try: region = create_region_for_hamburger_menu() region.click(option) return region except FindError: raise APIHelperError("Can't find the option in the page, aborting test.")
def open_library_menu(option): """Open the Library menu with an option as argument. :param option: Library menu option. :return: Custom region created for a more efficient and accurate image pattern search. """ library_menu_pattern = NavBar.LIBRARY_MENU if OSHelper.is_windows(): value = 5 else: value = 4 try: wait(library_menu_pattern, 10) region = Region( image_find(library_menu_pattern).x - Screen().width / value, image_find(library_menu_pattern).y, Screen().width / value, Screen().height / value, ) logger.debug("Library menu found.") except FindError: raise APIHelperError( "Can't find the library menu in the page, aborting test.") else: time.sleep(Settings.DEFAULT_UI_DELAY_LONG) click(library_menu_pattern) time.sleep(Settings.DEFAULT_UI_DELAY_SHORT) try: time.sleep(Settings.DEFAULT_UI_DELAY_SHORT) region.wait(option, 10) logger.debug("Option found.") region.click(option) return region except FindError: raise APIHelperError( "Can't find the option in the page, aborting test.")
def access_bookmarking_tools(option): """Access option from 'Bookmarking Tools'. :param option: Option from 'Bookmarking Tools'. :return: None. """ bookmarking_tools_pattern = LibraryMenu.BookmarksOption.BOOKMARKING_TOOLS open_library_menu(LibraryMenu.BOOKMARKS_OPTION) try: wait(bookmarking_tools_pattern, 10) logger.debug("Bookmarking Tools option has been found.") click(bookmarking_tools_pattern) except FindError: raise APIHelperError("Can't find the Bookmarking Tools option, aborting.") try: wait(option, 15) logger.debug("%s option has been found." % option) click(option) except FindError: raise APIHelperError("Can't find the %s option, aborting." % option)
def find_window_controls(window_type): """Find window controls for main and auxiliary windows. :param window_type: Controls for a specific window type. :return: None. """ if window_type == "auxiliary": Mouse().move(Location(1, 300)) if OSHelper.is_mac(): try: wait(AuxiliaryWindow.RED_BUTTON_PATTERN.similar(0.9), 5) logger.debug("Auxiliary window control found.") except FindError: raise APIHelperError( "Can't find the auxiliary window controls, aborting.") else: if OSHelper.is_linux(): Mouse().move(Location(80, 0)) try: wait(AuxiliaryWindow.CLOSE_BUTTON, 5) logger.debug("Auxiliary window control found.") except FindError: raise APIHelperError( "Can't find the auxiliary window controls, aborting.") elif window_type == "main": if OSHelper.is_mac(): try: wait(MainWindow.MAIN_WINDOW_CONTROLS.similar(0.9), 5) logger.debug("Main window controls found.") except FindError: raise APIHelperError( "Can't find the Main window controls, aborting.") else: try: if OSHelper.is_linux(): reset_mouse() wait(MainWindow.CLOSE_BUTTON, 5) logger.debug("Main window control found.") except FindError: raise APIHelperError( "Can't find the Main window controls, aborting.") else: raise APIHelperError("Window Type not supported.")