Exemplo n.º 1
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
Exemplo n.º 2
0
def open_library_menu(option):
    """Open a specific option from 'Library' menu with an option as an argument.

    :param option: Library menu option to be selected.
    :return: None
    """

    library_menu_pattern = NavBar.LIBRARY_MENU

    library_option_list = {
        'Bookmarks': 1,
        'View Pocket List': 2,
        'History': 3,
        'Downloads': 4,
        'Synced Tabs': 5
    }

    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(LibraryMenu.BOOKMARKS_OPTION, 10)
            option_number_in_library_list = library_option_list[option]
            for _ in range(option_number_in_library_list):
                time.sleep(0.5)
                type(Key.TAB)
            time.sleep(1)
            type(Key.ENTER)

        except FindError:
            raise APIHelperError(
                "Can't find the option in the page, aborting test.")
Exemplo n.º 3
0
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 Settings.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 Settings.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")
Exemplo n.º 4
0
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.")
Exemplo n.º 5
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)