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 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 _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)