def access_and_check_pattern(access_pattern, msg, check_pattern=None, access_type=None): """Access and check(if it exists) the patterns received. :param access_pattern: pattern to find and access if access_type is not None. :param msg: Message to display on test result :param check_pattern: pattern to assert after accessing 'find_pattern'. :param access_type: action to be performed on the access_pattern image. TODO Add more actions when needed :return: None. """ try: exists = wait(access_pattern, 10) logger.debug('%s pattern is displayed properly.' % access_pattern) if access_type and access_type == 'click': click(access_pattern) except FindError: raise APIHelperError('Can\'t find the %s pattern, aborting.' % access_pattern.get_filename()) if check_pattern: try: exists = wait(check_pattern, 15) logger.debug('%s pattern has been found.' % check_pattern.get_filename()) except FindError: raise APIHelperError('Can\'t find the %s option, aborting.' % check_pattern.get_filename()) return Step(exists, '%s was accessed and displayed properly.' % msg)
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 remove_zoom_indicator_from_toolbar(): """Remove the zoom indicator from toolbar by clicking on the 'Remove from Toolbar' button. """ zoom_control_toolbar_decrease_pattern = NavBar.ZOOM_OUT remove_from_toolbar_pattern = Pattern('remove_from_toolbar.png') try: wait(zoom_control_toolbar_decrease_pattern, FirefoxSettings.FIREFOX_TIMEOUT) logger.debug('\'Decrease\' zoom control found.') right_click(zoom_control_toolbar_decrease_pattern) except FindError: raise APIHelperError( 'Can\'t find the \'Decrease\' zoom control button in the page, \ aborting.') try: wait(remove_from_toolbar_pattern, FirefoxSettings.FIREFOX_TIMEOUT) logger.debug('\'Remove from Toolbar\' option found.') click(remove_from_toolbar_pattern) except FindError: raise APIHelperError( 'Can\'t find the \'Remove from Toolbar\' option in the page, \ aborting.') try: wait_vanish(zoom_control_toolbar_decrease_pattern, FirefoxSettings.FIREFOX_TIMEOUT) except FindError: raise APIHelperError( 'Zoom indicator not removed from toolbar, aborting.')
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 click_cancel_button(): """Click cancel button.""" cancel_button_pattern = Pattern('cancel_button.png').similar(.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') try: wait(customize_done_button_pattern, 10) logger.debug('Done button found.') click(customize_done_button_pattern) 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 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 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_bookmarks_toolbar(): """ Open the Bookmarks Toolbar using the context menu from the navigation bar """ home_button = NavBar.HOME_BUTTON w, h = home_button.get_size() horizontal_offset = w * 1.7 navbar_context_menu = home_button.target_offset(horizontal_offset, 0) try: right_click(navbar_context_menu) click(NavBar.ContextMenu.BOOKMARKS_TOOLBAR) logger.debug('Click is performed successfully on Bookmarks Toolbar option from navigation bar context menu.') except FindError: raise APIHelperError('Could not open the Bookmarks Toolbar using context menu from the navigation bar.') restore_firefox_focus()
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) 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 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 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 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, 10) logger.debug('Hamburger menu found.') except FindError: raise APIHelperError( 'Can\'t find the "hamburger menu" in the page, aborting test.') else: click(hamburger_menu_pattern) time.sleep(Settings.DEFAULT_UI_DELAY) try: region = create_region_from_image(hamburger_menu_pattern) 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 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.')