예제 #1
0
def test_wait_template(region_and_template):
    _, template = region_and_template
    library = Images()
    library.take_screenshot = lambda: to_image(IMAGES / "source.png")

    library.matcher = TemplateMatcher(opencv=True)
    matches = library.wait_template_on_screen(IMAGES / template, timeout=0.52)
    assert len(matches) == 1
예제 #2
0
def test_find_template(region_and_template):
    region, template = region_and_template
    region = Region(*region)

    library = Images()
    library.matcher = TemplateMatcher(opencv=True)

    matches = library.find_template_in_image(image=IMAGES / "source.png",
                                             template=IMAGES / template,
                                             limit=1)

    assert len(matches) == 1
    match = matches[0]
    assert match.center == region.center
예제 #3
0
def test_find_template(region_and_template):
    region, template = region_and_template
    region = Region(*region)

    library = Images()
    matches = library.find_template_in_image(
        image=IMAGES / "source.png",
        template=IMAGES / template,
        tolerance=0.8,
    )

    assert len(matches) == 1
    match = matches[0]
    assert match.center == region.center
예제 #4
0
    def screenshot(
        self,
        filename: str,
        element: dict = None,
        ctrl: Any = None,
        desktop: bool = False,
    ) -> None:
        """Save screenshot into filename.

        :param filename: name of the file
        :param element: take element screenshot if True, defaults to None
        :param ctrl: take control screenshot if True, defaults to None
        :param desktop: take desktop screenshot if True, defaults to False
        """
        if desktop:
            region = None
        elif element:
            region = self._get_element_coordinates(element["rectangle"])
        elif ctrl:
            region = self.get_dialog_rectangle(ctrl)
        else:
            region = self.get_dialog_rectangle()

        try:
            output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}")
        except (ModuleNotFoundError, RobotNotRunningError):
            output_dir = Path.cwd()

        filename = Path(output_dir, "images", clean_filename(filename))
        os.makedirs(filename.parent)
        Images().take_screenshot(filename=filename, region=region)

        self.logger.info("Saved screenshot as '%s'", filename)
예제 #5
0
def click_image(
    imagefile: str, button: str = "left", clicks: int = 1, interval: float = 0.25
):
    """Click on the center of the given image file if it is found on the screen

    :param imagefile: click on the center of this image (filepath)
    :param button: which button to click, defaults to "left"
    :param clicks: how many times mouse is clicked, defaults to 1
    :param interval: time interval between clicks if more than 1 clicks, defaults to 0.25
    """
    os.environ["DISPLAY"] = ":99.0"
    match = Images().find_template_on_screen(imagefile, limit=1)
    if match:
        center = match[0].center
        pyautogui.click(
            x=center.x, y=center.y, button=button, clicks=clicks, interval=interval
        )
예제 #6
0
    def mouse_click_image(self,
                          template: str,
                          off_x: int = 0,
                          off_y: int = 0,
                          ctype: str = "click") -> None:
        """Click at template image on desktop

        :param image: [description]
        :param off_x: [description], defaults to 0
        :param off_y: [description], defaults to 0
        :param ctype: [description], defaults to "click"
        """
        matches = Images().find_template_on_screen(template, limit=1)

        center_x = matches[0].center.x + int(off_x)
        center_y = matches[0].center.y + int(off_y)

        self.click_type(center_x, center_y, ctype)
예제 #7
0
def test_screenshot_region_and_find_it():
    library = Images()
    region = Region(0, 0, 100, 100)
    first_capture = library.take_screenshot(region=region)
    find_result = library.find_template_on_screen(first_capture)
    assert region == find_result[0]