def find(self, locator: str) -> List[Union[Point, Region]]: """Internal method for resolving and searching locators.""" if isinstance(locator, (Region, Point)): return [locator] locator = parse_locator(locator) self.logger.info("Using locator: %s", locator) if isinstance(locator, Coordinates): position = Point(locator.x, locator.y) return [position] elif isinstance(locator, Offset): position = self.ctx.get_mouse_position() position.offset(locator.x, locator.y) return [position] elif isinstance(locator, ImageTemplate): if not HAS_RECOGNITION: raise ValueError( "Image templates not supported, please install " "rpaframework-recognition package") # TODO: Add built-in offset support return self._find_templates(locator) else: raise NotImplementedError(f"Unsupported locator: {locator}")
def _find(self, locator: str) -> List[Geometry]: """Internal method for resolving and searching locators.""" if isinstance(locator, (Region, Point)): return [locator] locator: Locator = parse_locator(locator) self.logger.info("Using locator: %s", locator) if isinstance(locator, PointLocator): position = Point(locator.x, locator.y) return [position] elif isinstance(locator, OffsetLocator): position = self.ctx.get_mouse_position() position = position.move(locator.x, locator.y) return [position] elif isinstance(locator, RegionLocator): region = Region(locator.left, locator.top, locator.right, locator.bottom) return [region] elif isinstance(locator, ImageLocator): ensure_recognition() return self._find_templates(locator) elif isinstance(locator, OcrLocator): ensure_recognition() return self._find_ocr(locator) else: raise NotImplementedError(f"Unsupported locator: {locator}")
def find(self, locator: str) -> List[Union[Point, Region]]: """Internal method for resolving and searching locators.""" if isinstance(locator, (Region, Point)): return [locator] locator = parse_locator(locator) self.logger.info("Using locator: %s", locator) if isinstance(locator, Coordinates): position = Point(locator.x, locator.y) return [position] elif isinstance(locator, Offset): position = self.ctx.get_mouse_position() position.offset(locator.x, locator.y) return [position] elif isinstance(locator, ImageTemplate): if not HAS_RECOGNITION: raise ValueError( "Image templates not supported, please install " "rpaframework-recognition module") # TODO: Add built-in offset support confidence = locator.confidence or self.confidence self.logger.info("Matching with confidence of %.1f", confidence) try: regions = templates.find( image=self.ctx.take_screenshot(), template=locator.path, confidence=confidence, ) # Virtual screen top-left might not be (0,0) left, top, _, _ = self.ctx.get_display_dimensions() for region in regions: region.move(left, top) except templates.ImageNotFoundError: return [] return regions else: raise NotImplementedError(f"Unsupported locator: {locator}")
def test_parse_offset(self): locator = parse_locator("offset:100,200") assert isinstance(locator, Offset) assert locator.x == 100 assert locator.y == 200
def test_parse_coordinates(self): locator = parse_locator("coordinates:100,200") assert isinstance(locator, Coordinates) assert locator.x == 100 assert locator.y == 200
def test_parse_image_args(self): locator = parse_locator("image:path/to/file.png,80.0") assert isinstance(locator, ImageTemplate) assert locator.path == "path/to/file.png" assert locator.confidence == 80.0
def test_parse_coordinates(self): # Kept for backwards compatibility locator = parse_locator("coordinates:100,200") assert isinstance(locator, PointLocator) assert locator.x == 100 assert locator.y == 200
def test_parse_point(self): locator = parse_locator("point:100,200") assert isinstance(locator, PointLocator) assert locator.x == 100 assert locator.y == 200
def test_parse_image(self): locator = parse_locator("image:path/to/file.png") assert isinstance(locator, ImageLocator) assert locator.path == "path/to/file.png" assert locator.confidence == None