def confirm_firefox_launch(image=None): """Waits for firefox to exist by waiting for the iris logo to be present. :param image: Pattern to confirm Firefox launch :return: None. """ if image is None: image = Pattern("iris_logo.png") try: wait(image, 60) except Exception: raise APIHelperError("Can't launch Firefox - aborting test run.")
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 download_file( file_to_download, accept_download, max_number_of_attempts=20, expect_accept_download_available=True, ): """ :param file_to_download: Pattern of file to be downloaded. :param accept_download: Accept download pattern. :param max_number_of_attempts: Max number of attempts to locate file_to_download pattern. :param expect_accept_download_available: True if we expect accept_download button, False - if we don't; (in Windows 7 download UI don't have extra accept button in certain cases) :return: None. """ for _ in range(max_number_of_attempts): file_found = exists(file_to_download, FirefoxSettings.FIREFOX_TIMEOUT) if file_found: click(file_to_download) break type(Key.PAGE_DOWN) if exists(DownloadFiles.ABOUT, FirefoxSettings.FIREFOX_TIMEOUT): raise APIHelperError("File to be downloaded not found.") try: wait(DownloadFiles.SAVE_FILE, FirefoxSettings.HEAVY_SITE_LOAD_TIMEOUT) logger.debug("The 'Save file' option is present in the page.") time.sleep( FirefoxSettings.TINY_FIREFOX_TIMEOUT ) # prevent click on inactive button on windows click(DownloadFiles.SAVE_FILE) except FindError: raise APIHelperError( "The 'Save file' option is not present in the page, aborting." ) if expect_accept_download_available: accept_download_button = exists( accept_download, FirefoxSettings.FIREFOX_TIMEOUT ) if accept_download_button: logger.debug("The accept download button found in the page.") click(accept_download) else: raise APIHelperError( "The accept download button was not found in the page." )
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 open_library_menu(option): """Open a specific option from 'Library' menu with an option as an argument. :param option: Library menu option to be selected. :return: None """ library_menu_pattern = NavBar.LIBRARY_MENU library_option_list = { 'Bookmarks': 1, 'View Pocket List': 2, 'History': 3, 'Downloads': 4, 'Synced Tabs': 5 } 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(LibraryMenu.BOOKMARKS_OPTION, 10) option_number_in_library_list = library_option_list[option] for _ in range(option_number_in_library_list): time.sleep(0.5) type(Key.TAB) time.sleep(1) type(Key.ENTER) except FindError: raise APIHelperError( "Can't find the option in the page, aborting test.")
def wait(self, ps=None, timeout=None) -> bool or FindError: """Wait for a Pattern or image to appear. :param ps: Pattern or String. :param timeout: Number as maximum waiting time in seconds. :return: True or FineError Exception. """ return wait(ps, timeout, self._area)
def select_throttling(option): open_web_console() try: wait(Pattern('network.png'), 10) click(Pattern('network.png')) except FindError: raise APIHelperError('Can\'t find the network menu in the page, aborting test.') try: wait(Pattern('no_throttling.png'), 10) click(Pattern('no_throttling.png')) except FindError: raise APIHelperError('Can\'t find the throttling menu in the page, aborting test.') for i in range(option + 1): type(Key.DOWN) type(Key.ENTER)
def create_region_for_hamburger_menu(): """Create region for hamburger menu pop up.""" hamburger_menu_pattern = NavBar.HAMBURGER_MENU try: wait(hamburger_menu_pattern, 10) click(hamburger_menu_pattern) sign_in_to_firefox_pattern = Pattern("sign_in_to_firefox.png").similar(0.6) wait(sign_in_to_firefox_pattern, 10) if OSHelper.is_linux(): quit_menu_pattern = Pattern("quit.png").similar(0.6) wait(quit_menu_pattern, 5) return RegionUtils.create_region_from_patterns( None, sign_in_to_firefox_pattern, quit_menu_pattern, None, padding_right=20, ) elif OSHelper.is_mac(): help_menu_pattern = Pattern("help.png") wait(help_menu_pattern, 5) return RegionUtils.create_region_from_patterns( None, sign_in_to_firefox_pattern, help_menu_pattern, None, padding_right=20, ) else: exit_menu_pattern = Pattern("exit.png").similar(0.6) wait(exit_menu_pattern, 5) return RegionUtils.create_region_from_patterns( None, sign_in_to_firefox_pattern, exit_menu_pattern, None, padding_right=20, ) except (FindError, ValueError): raise APIHelperError( "Can't create a region for the hamburger menu, aborting test." )
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 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 get_telemetry_info(): """Returns telemetry information as a JSON object from 'about:telemetry' page. """ copy_raw_data_to_clipboard_pattern = Pattern( "copy_raw_data_to_clipboard.png") raw_json_pattern = Pattern("raw_json.png") raw_data_pattern = Pattern("raw_data.png") new_tab() paste("about:telemetry") type(Key.ENTER) try: wait(raw_json_pattern, 10) logger.debug("'RAW JSON' button is present on the page.") click(raw_json_pattern) except (FindError, ValueError): raise APIHelperError("'RAW JSON' button not present in the page.") try: wait(raw_data_pattern, 10) logger.debug("'Raw Data' button is present on the page.") click(raw_data_pattern) except (FindError, ValueError): close_tab() raise APIHelperError("'Raw Data' button not present in the page.") try: click(copy_raw_data_to_clipboard_pattern) time.sleep(Settings.DEFAULT_UI_DELAY) json_text = get_clipboard() return json.loads(json_text) except Exception: raise APIHelperError( "Failed to retrieve raw message information value.") finally: close_tab()
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.")