def new_tab(): """Open a new browser tab.""" if OSHelper.is_mac(): type(text='t', modifier=KeyModifier.CMD) else: type(text='t', modifier=KeyModifier.CTRL) time.sleep(Settings.DEFAULT_UI_DELAY)
def select_zoom_menu_option(option_number): """Open the 'Zoom' menu from the 'View' menu and select option.""" open_zoom_menu() repeat_key_down(option_number) type(text=Key.ENTER)
def restore_window_from_taskbar(option=None): """Restore firefox from task bar.""" if OSHelper.is_mac(): try: click(Pattern('main_menu_window.png')) if option == "browser_console": click(Pattern('window_browser_console.png')) else: click(Pattern('window_firefox.png')) except FindError: raise APIHelperError('Restore window from taskbar unsuccessful.') elif OSHelper.get_os_version() == 'win7': try: click(Pattern('firefox_start_bar.png')) if option == "library_menu": click(Pattern('firefox_start_bar_library.png')) if option == "browser_console": click(Pattern('firefox_start_bar_browser_console.png')) except FindError: raise APIHelperError('Restore window from taskbar unsuccessful.') else: type(text=Key.TAB, modifier=KeyModifier.ALT) if OSHelper.is_linux(): Mouse().move(Location(0, 50)) time.sleep(Settings.DEFAULT_UI_DELAY)
def maximize_window(): """Maximize the browser window to fill the screen. This is NOT Full Screen mode. """ if OSHelper.is_mac(): # There is no keyboard shortcut for this on Mac. We'll do it the old fashioned way. # This image is of the three window control buttons at top left of the window. # We have to resize the window to ensure maximize works properly in all cases. window_controls_pattern = Pattern('window_controls.png') controls_location = find(window_controls_pattern) xcoord = controls_location.x ycoord = controls_location.y width, height = window_controls_pattern.get_size() drag_start = Location(xcoord + 70, ycoord + 5) drag_end = Location(xcoord + 75, ycoord + 5) Mouse().drag_and_drop(drag_start, drag_end, duration=0.1) # Alt key changes maximize button from full screen to maximize window. maximize_button = window_controls_pattern.target_offset( width / 2 - 3, 0) key_down(Key.ALT) click(maximize_button) key_up(Key.ALT) elif OSHelper.is_windows(): type(text=Key.UP, modifier=KeyModifier.WIN) else: type(text=Key.UP, modifier=[KeyModifier.CTRL, KeyModifier.META]) time.sleep(Settings.DEFAULT_UI_DELAY)
def select_search_bar(): """If the search bar is present, select the search bar, otherwise this selects the location bar.""" if OSHelper.is_mac(): type(text='k', modifier=KeyModifier.CMD) else: type(text='k', modifier=KeyModifier.CTRL) time.sleep(Settings.DEFAULT_UI_DELAY_LONG)
def right_click_and_type(target, delay=None, keyboard_action=None): right_click(target) if delay: time.sleep(delay) else: time.sleep(Settings.DEFAULT_UI_DELAY_LONG) type(text=keyboard_action)
def open_browser_console(): """ Opens the Browser Console. """ if OSHelper.is_mac(): type(text="j", modifier=[KeyModifier.CMD, KeyModifier.SHIFT]) else: type(text="j", modifier=[KeyModifier.CTRL, KeyModifier.SHIFT])
def select_location_bar(): """Set focus to the location bar.""" if OSHelper.is_mac(): type(text='l', modifier=KeyModifier.CMD) else: type(text='l', modifier=KeyModifier.CTRL) # Wait to allow the location bar to become responsive. time.sleep(Settings.DEFAULT_UI_DELAY_LONG)
def select_folder_location_bar(): """Set focus to the location bar/open folder popup in previously opened file manager (e.g. "Open file", "Import bookmark") to navigate to path""" if OSHelper.is_mac(): type(text='g', modifier=[KeyModifier.SHIFT, KeyModifier.CMD]) else: type(text='l', modifier=KeyModifier.CTRL)
def bookmark_all_tabs(): """Open the Bookmark All Tabs dialog.""" if OSHelper.is_mac(): type(text='d', modifier=[KeyModifier.CMD, KeyModifier.SHIFT]) else: type(text='d', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT]) # Wait for the Bookmark All Tabs dialog to be opened. time.sleep(Settings.DEFAULT_UI_DELAY_LONG)
def restart_via_console(): """ restarts Firefox if web console is opened """ if OSHelper.is_mac(): type(text='r', modifier=[KeyModifier.CMD, KeyModifier.ALT]) else: type(text='r', modifier=[KeyModifier.CTRL, KeyModifier.ALT])
def change_search_previous(): """If the search bar has focus, change the search engine to the previous in the list. (side effect: this also opens the search engine manager, if it wasn't already open). """ if OSHelper.is_mac(): type(text=Key.UP, modifier=KeyModifier.CMD) else: type(text=Key.UP, modifier=KeyModifier.CTRL)
def find_next(): """Find next occurrence of term, if find is already active on a search term. Find next (again) can also find the next occurrence of a term without opening the find toolbar. """ if OSHelper.is_mac(): type(text='g', modifier=KeyModifier.CMD) else: type(text='g', modifier=KeyModifier.CTRL)
def repeat_key_down(num): """Repeat DOWN keystroke a given number of times. :param num: Number of times to repeat DOWN key stroke. :return: None. """ for i in range(num): type(Key.DOWN) time.sleep(Settings.DEFAULT_UI_DELAY_SHORT)
def repeat_key_up(num): """Repeat UP keystroke a given number of times. :param num: Number of times to repeat UP key stroke. :return: None. """ for i in range(num): type(Key.UP) time.sleep(1)
def find_previous(): """Find the previous occurrence of term, if find is already active on a search term. Find previous can also find the previous occurrence of a term without opening the find toolbar. """ if OSHelper.is_mac(): type(text='g', modifier=[KeyModifier.CMD, KeyModifier.SHIFT]) else: type(text='g', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT])
def open_firefox_menu(): """ Opens Firefox top menu """ if OSHelper.is_linux(): key_down(Key.ALT) time.sleep(0.5) key_up(Key.ALT) elif OSHelper.is_windows(): type(Key.ALT)
def bookmark_page(): """Bookmark the current page.""" if OSHelper.is_mac(): type(text='d', modifier=KeyModifier.CMD) else: type(text='d', modifier=KeyModifier.CTRL) try: wait(LocationBar.STAR_BUTTON_STARRED, 10) logger.debug('Page was successfully bookmarked') except FindError: raise APIHelperError('Page can not be bookmarked')
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 navigate(url): """Navigates, via the location bar, to a given URL. :param url: The string to type into the location bar. :return: None. """ try: select_location_bar() paste(url) time.sleep(Settings.DEFAULT_UI_DELAY_SHORT) type(Key.ENTER) except Exception: raise APIHelperError( 'No active window found, cannot navigate to page.')
def delete_selected_file(): """Delete selected file/files inside a folder.""" if OSHelper.is_mac(): type(text=Key.BACKSPACE, modifier=KeyModifier.CMD) elif OSHelper.get_os_version() == 'win7': type(text=Key.DELETE) type(text='y') else: type(text=Key.DELETE)
def key_to_one_off_search(highlighted_pattern, direction='left'): """Iterate through the one of search engines list until the given one is highlighted. param: highlighted_pattern: The pattern image to search for. param: direction: direction to key to: right or left (default) return: None. """ max_attempts = 7 while max_attempts > 0: if exists(highlighted_pattern, 1): max_attempts = 0 else: if direction == 'right': type(Key.RIGHT) else: type(Key.LEFT) max_attempts -= 1
def get_support_info(): """Returns support information as a JSON object from 'about:support' page.""" copy_raw_data_to_clipboard = Pattern('about_support_copy_raw_data_button.png') new_tab() select_location_bar() paste('about:support') type(Key.ENTER) time.sleep(Settings.DEFAULT_UI_DELAY) try: click(copy_raw_data_to_clipboard) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) json_text = get_clipboard() return json.loads(json_text) except Exception as e: raise APIHelperError('Failed to retrieve support information value.\n{}'.format(e)) finally: close_tab()
def get_pref_value(pref_name): """Returns the value of a provided preference from 'about:config' page. :param pref_name: Preference's name. :return: Preference's value. """ new_tab() select_location_bar() paste('about:config') type(Key.ENTER) time.sleep(Settings.DEFAULT_UI_DELAY) type(Key.SPACE) time.sleep(Settings.DEFAULT_UI_DELAY) paste(pref_name) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) type(Key.TAB) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) try: value = copy_to_clipboard().split(';'[0])[1] except Exception as e: raise APIHelperError( 'Failed to retrieve preference value.\n{}'.format(e)) close_tab() return value
def quit_firefox(): """Quit the browser.""" # Press the ESC key to exit any modal dialogs that might prevent key capture. type(text=Key.ESC) # Wait before quitting Firefox to avoid concurrency. time.sleep(1) if OSHelper.is_mac(): type(text='q', modifier=KeyModifier.CMD) elif OSHelper.is_windows(): type(text='q', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT]) else: type(text='q', modifier=KeyModifier.CTRL)
def select_location_bar_option(option_number): """Select option from the location bar menu. :param option_number: Option number. :return: None. """ if OSHelper.get_os() == OSPlatform.WINDOWS: for i in range(option_number + 1): type(Key.DOWN) type(Key.ENTER) else: for i in range(option_number - 1): type(Key.DOWN) type(Key.ENTER)
def check_preference(pref_name, value): """Check the value for a specific preference. :param pref_name: Preference to be searched. :param value: Preference's value to be checked. :return: None. """ new_tab() select_location_bar() paste('about:config') time.sleep(Settings.DEFAULT_UI_DELAY) type(Key.ENTER) time.sleep(Settings.DEFAULT_UI_DELAY) type(Key.SPACE) time.sleep(Settings.DEFAULT_UI_DELAY) paste(pref_name) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) type(Key.TAB) time.sleep(Settings.DEFAULT_UI_DELAY_LONG) try: retrieved_value = copy_to_clipboard().split(';'[0])[1] except Exception as e: raise APIHelperError('Failed to retrieve preference value. %s' % e.message) if retrieved_value == value: return True else: return False
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(): """Open the Library window.""" if OSHelper.is_mac(): type(text='b', modifier=[KeyModifier.CMD, KeyModifier.SHIFT]) elif OSHelper.is_windows(): type(text='b', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT]) else: type(text='o', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT])
def open_downloads(): """Open the Downloads dialog.""" if OSHelper.is_mac(): type(text='j', modifier=KeyModifier.CMD) elif OSHelper.is_windows(): type(text='j', modifier=KeyModifier.CTRL) else: type(text='y', modifier=[KeyModifier.CTRL, KeyModifier.SHIFT])