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 try: wait(library_menu_pattern, 10) region = Region( image_find(library_menu_pattern).x - Screen().width / 4, image_find(library_menu_pattern).y, Screen().width / 4, Screen().height / 4 - 100) 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 create_region_from_image(image): """Create region starting from a pattern. :param image: Pattern used to create a region. :return: None. """ try: from src.core.api.rectangle import Rectangle from src.core.api.enums import Alignment m = image_find(image) if m: sync_pattern = Pattern('sync_hamburger_menu.png') sync_width, sync_height = sync_pattern.get_size() sync_image = image_find(sync_pattern) top_left = Rectangle(sync_image.x, sync_image.y, sync_width, sync_width). \ apply_alignment(Alignment.TOP_RIGHT) if OSHelper.is_mac(): exit_pattern = Pattern('help_hamburger_menu.png') else: exit_pattern = Pattern('exit_hamburger_menu.png') exit_width, exit_height = exit_pattern.get_size() exit_image = image_find(exit_pattern) bottom_left = Rectangle(exit_image.x, exit_image.y, exit_width, exit_height). \ apply_alignment(Alignment.BOTTOM_RIGHT) x0 = top_left.x + 2 y0 = top_left.y height = bottom_left.y - top_left.y width = Screen().width - top_left.x - 2 region = Region(x0, y0, width, height) return region else: raise APIHelperError('No matching found.') except FindError: raise APIHelperError('Image not present.')
def wait(ps, timeout=None, region=None) -> bool or FindError: """Verify that a Pattern or str appears. :param ps: String or Pattern. :param timeout: Number as maximum waiting time in seconds. :param region: Rectangle object in order to minimize the area. :return: True if found, otherwise raise FindError. """ if isinstance(ps, Pattern): if timeout is None: timeout = Settings.auto_wait_timeout image_found = image_find(ps, timeout, region) if image_found is not None: if get_core_args().highlight: highlight(region=region, ps=ps, location=[image_found]) return True else: raise FindError('Unable to find image %s' % ps.get_filename()) elif isinstance(ps, str): text_found = text_find(ps, region) if len(text_found) > 0: if get_core_args().highlight: highlight(region=region, ps=ps, text_location=text_found) return Location(text_found[0].x, text_found[0].y) else: raise FindError('Unable to find text %s' % ps) else: raise ValueError('Invalid input')
def _get_pattern_click_location(ps: Pattern, region: Rectangle = None, align: Alignment = None): """Returns the click location based on the pattern/string found location and alignment.""" if align is None: align = Alignment.CENTER width, height = ps.get_size() find_location = image_find(ps, region=region) if find_location is None: raise FindError('Unable to click on: %s' % ps.get_file_path()) if ps.get_target_offset(): target_offset = ps.get_target_offset() find_location.x += target_offset.x find_location.y += target_offset.y rect = Rectangle(find_location.x, find_location.y, width, height) return rect.apply_alignment(align)