Beispiel #1
0
def create_region_from_image(image):
    """Create region starting from a pattern.

    :param image: Pattern used to create a region.
    :return: None.
    """
    try:
        from mattapi.api.rectangle import Rectangle
        from mattapi.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.')
Beispiel #2
0
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
Beispiel #3
0
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)
Beispiel #4
0
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 find pattern {}'.format(ps.get_filename()))

    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)