def bookmarks_sidebar(option: str): """Toggle open/close the bookmarks sidebar.""" if OSHelper.is_mac(): type(text='b', modifier=KeyModifier.CMD) else: type(text='b', modifier=KeyModifier.CTRL) bookmark_sidebar_header_pattern = SidebarBookmarks.BOOKMARKS_HEADER if option == 'open': try: wait(bookmark_sidebar_header_pattern, 10) logger.debug('Sidebar is opened.') except FindError: raise APIHelperError( 'Sidebar is NOT present on the page, aborting.') elif option == 'close': try: wait_vanish(bookmark_sidebar_header_pattern, 10) logger.debug('Sidebar is closed.') except FindError: raise APIHelperError('Sidebar is NOT closed, aborting.') else: raise APIHelperError('Option is not supported, aborting')
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 download_file(file_to_download, accept_download, max_number_of_attempts=20): """ :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. :return: None. """ for _ in range(max_number_of_attempts): file_found = exists(file_to_download, Settings.FIREFOX_TIMEOUT) if file_found: click(file_to_download) break type(Key.PAGE_DOWN) if exists(DownloadFiles.ABOUT, Settings.DEFAULT_UI_DELAY_LONG): raise APIHelperError('File to be downloaded not found.') try: wait(DownloadFiles.SAVE_FILE, 90) logger.debug('The \'Save file\' option is present in the page.') click(DownloadFiles.SAVE_FILE) except FindError: raise APIHelperError( 'The \'Save file\' option is not present in the page, aborting.') try: ok_button = exists(accept_download, 5) if ok_button: logger.debug('The OK button found in the page.') click(accept_download) except FindError: raise APIHelperError('The OK button is not found in the page.')
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 full_screen_control(window_type): """Click on full screen window mode (Applicable only for MAC system). :param window_type: Type of window that need to be maximized in full screen mode. :reurn: None. """ if OSHelper.is_mac(): find_window_controls(window_type) if window_type == 'auxiliary': width, height = AuxiliaryWindow.AUXILIARY_WINDOW_CONTROLS.get_size() click(AuxiliaryWindow.AUXILIARY_WINDOW_CONTROLS.target_offset(width - 10, height / 2), align=Alignment.TOP_LEFT) else: width, height = MainWindow.MAIN_WINDOW_CONTROLS.get_size() click(MainWindow.MAIN_WINDOW_CONTROLS.target_offset(width - 10, height / 2), align=Alignment.TOP_LEFT) else: raise APIHelperError('Full screen mode applicable only for MAC')
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 click_window_control(button, window_type='auxiliary'): """Click window with options: close, minimize, maximize, restore, full_screen. :param button: Auxiliary or main window options. :param window_type: Type of window that need to be controlled. :return: None. """ if button == 'close': close_window_control(window_type) elif button == 'minimize': minimize_window_control(window_type) elif button == 'maximize': maximize_window_control(window_type) elif button == 'restore': restore_window_control(window_type) elif button == 'full_screen': full_screen_control(window_type) else: raise APIHelperError('Button option is not supported.')
def get_firefox_channel_from_about_config(): """Returns the Firefox channel from 'about:config' page.""" try: return get_pref_value('app.update.channel') except APIHelperError: raise APIHelperError('Could not retrieve firefox channel information from about:config page.')
def copy_file(original, copy): try: shutil.copy(original, copy) except OSError: raise APIHelperError(f'Cannot copy {original} file')
def select_file_in_folder(directory, filename_pattern, file_option, max_num_of_attempts=3): """ Opens directory, selects file in opened directory, and provides action with it (e.g. copy, cut, delete), and closes opened directory. :param directory: Folder on hard drive to open. :param filename_pattern: File Pattern to select. :param file_option: File processing function. Appropriate methods are: edit_copy, edit_cut, edit_delete. :param max_num_of_attempts: Attempts to find pattern of file name. Default: 3 """ finder_list_view = '2' type_delay = 0.5 if not isinstance(directory, str): raise ValueError(INVALID_GENERIC_INPUT) if not isinstance(filename_pattern, Pattern): raise ValueError(INVALID_GENERIC_INPUT) if not callable(file_option): raise ValueError(INVALID_GENERIC_INPUT) open_directory(directory) try: for attempt in range(1, max_num_of_attempts + 1): file_located = exists(filename_pattern) if file_located: logger.debug('File {} in directory {} is available.'.format( filename_pattern, directory)) break else: if attempt == max_num_of_attempts: logger.debug( 'File {} is not available after {} attempt(s).'.format( filename_pattern, attempt)) raise Exception time.sleep(Settings.DEFAULT_UI_DELAY_LONG) if OSHelper.is_mac(): type(text=finder_list_view, modifier=KeyModifier.CMD, interval=type_delay) click(filename_pattern) file_option() except Exception: raise APIHelperError('Could not find file {} in folder {}.'.format( filename_pattern, directory)) finally: if OSHelper.is_windows(): type(text='w', modifier=KeyModifier.CTRL) elif OSHelper.is_linux(): type(text='q', modifier=KeyModifier.CTRL) elif OSHelper.is_mac(): type(text='w', modifier=[KeyModifier.CMD, KeyModifier.ALT])
def _get_image_path(caller, image: str) -> str: """Enforce proper location for all Pattern creation. :param caller: Path of calling Python module. :param image: String filename of image. :return: Full path to image on disk. We will look at all possible paths relative to the calling file, with this priority: - current platform locale folder - common locale folder - current platform root - common root Each directory is scanned for four possible file names, depending on resolution. If the above fails, we will look up the file name in the list of project-wide images, and return whatever we find, with a warning message. If we find nothing, we will raise an exception. """ module = os.path.split(caller)[1] module_directory = os.path.split(caller)[0] file_name = image.split('.')[0] names = [image, '*****@*****.**' % file_name] if OSHelper.get_os_version() == 'win7': os_version = 'win7' else: os_version = OSHelper.get_os().value paths = [] current_locale = Settings.locale platform_directory = os.path.join(module_directory, 'images', os_version) if current_locale != '': platform_locale_directory = os.path.join(platform_directory, current_locale) for name in names: paths.append(os.path.join(platform_locale_directory, name)) common_directory = os.path.join(module_directory, 'images', 'common') if current_locale != '': common_locale_directory = os.path.join(common_directory, current_locale) for name in names: paths.append(os.path.join(common_locale_directory, name)) for name in names: paths.append(os.path.join(platform_directory, name)) for name in names: paths.append(os.path.join(common_directory, name)) found = False image_path = None for path in paths: if os.path.exists(path): found = True image_path = path break if found: logger.debug('Module %s requests image %s' % (module, image)) logger.debug('Found %s' % image_path) return image_path else: raise APIHelperError('Image not found')