def click(self, entity, button=InputEvent.BUTTON1_MASK): # click self.screen.mouseMove(entity.getRegion().getClickLocation()) sleep(0.5) # Get the state of the button before we click state = self.screen.capture(entity.getRegion()) #sleep(0.2) self.screen.mouseDown(button) sleep(0.5) # Wait for button to change its appearnce because we're clicking on it if entity.getRegion().waitVanish(Pattern(state).similar(1.0), 0.7): sleep(0.2) else: if self.assertStateChanged: raise Exception("State did not change") else: self.logger.warn("State did not change") self.screen.mouseUp(button) sleep(0.5)
def anchor(target): if isinstance(target, Pattern): pattern = target elif isinstance(target, str): pattern = Pattern(target) comp = SikuliGuideAnchor(pattern) _g.addToFront(comp) return comp
def area(targets): patterns = [Pattern(target) for target in targets] comp = SikuliGuideArea() for pattern in patterns: anchor = SikuliGuideAnchor(pattern) _g.addToFront(anchor) comp.addLandmark(anchor) _g.addToFront(comp) return comp
def _addSideComponentToTarget(comp, target, **kwargs): r = _getRegionFromTarget(target) _setLocationRelativeToRegion(comp, r, **kwargs) if isinstance(target, str): _g.addTracker(Pattern(target), r, comp) elif isinstance(target, Pattern): _g.addTracker(target, r, comp) elif isinstance(target, SikuliGuideComponent): target.addFollower(comp) _g.addComponent(comp) return comp
def _addAraeComponentToTarget(comp_func, target, **kwargs): r = _getRegionFromTarget(target) r1 = _adjustRegion(r, **kwargs) comp = comp_func(r1) if isinstance(target, str): _g.addTracker(Pattern(target), r1, comp) elif isinstance(target, Pattern): _g.addTracker(target, r1, comp) elif isinstance(target, SikuliGuideComponent): target.addFollower(comp) _g.addComponent(comp) return comp
def parse_model(gui, level=0): for i in range(0, level): print "----", n = gui[0] ps, name = n node_n = GUINode(Pattern(ps).similar(0.75)) node_n.setName(name) print node_n children = gui[1:] for c in children: node_c = parse_model(c, level + 1) node_n.add(node_c) return node_n
def assertThat(self, text): self.validate() self.logger.info("Asserting [%s]" % text) baselinePath = self.config.imageBaseline + "/assert/" + str(Env.getOS()).lower() + "/" + text + ".png" baseline = Pattern(baselinePath) # TODO: Refactor to allow for using RegionFinder class so PNG attributes can be used try: ImageLocator().locate( baselinePath ) result = self.region.find(baseline) self.logger.info('%%s matched %%s', self.logger.getFormatter()(result), self.logger.getFormatter()(baseline)) except FindFailed, e: self.logger.error('%%s does not match %%s', self.logger.getFormatter()(self.region), self.logger.getFormatter()(baseline)) raise Exception("Assertion failure")
def _find_element(session_id, context, many=False): try: json_request_data = json.loads(request.body.read()) locator_strategy = json_request_data.get('using') value = json_request_data.get('value') if locator_strategy == "id": path = app.config.get("Element Mapping",value) elif locator_strategy == "name": path = os.path.join(app.image_path,value) elif locator_strategy == "xpath": path = value else: response.status = 501 return {'sessionId': session_id, 'status': 32, 'value': 'Unsupported location strategy, use id, name, or XPath only. See docs for details.'} elem = Pattern(path).similar(app.similarity) if not many: try: if context == "root": result = app.SS.find(elem) else: result = app.element_list[context].find(elem) except: return {'sessionId': session_id, 'status': 7, 'value': 'Element not found'} #found_elements = {'ELEMENT':encode_value_4_wire(path)} app.element_list.append(result) found_elements = {'ELEMENT':app.element_counter} app.element_counter = app.element_counter + 1 else: try: if context == "root": result = app.SS.findAll(elem) else: result = app.element_list[context].findAll(elem) temp_elements = [] while result.hasNext(): # loop as long there is a first and more matches app.element_list.append(result.next()) temp_elements.append({'ELEMENT':app.element_counter}) app.element_counter = app.element_counter + 1 found_elements = temp_elements except: found_elements = [] return {'sessionId': session_id, 'status': 0, 'value': found_elements} except: response.status = 400 return {'sessionId': session_id, 'status': 13, 'value': str(sys.exc_info()[0])}
def _addComponentHelper(comp, target, side='best', margin=0, offset=(0, 0), horizontalalignment='center', verticalalignment='center', font=None, fontsize=0, width=0, shadow='default', front=None, back=None, frame=None, text=None): # set the component's colors comp.setColors(front, back, frame, text) # set the component's font comp.setFont(font, fontsize) # set the components width if width > 0: comp.setMaxWidth(width) # Margin if margin: if isinstance(margin, tuple): (dt, dl, db, dr) = margin else: (dt, dl, db, dr) = (margin, margin, margin, margin) comp.setMargin(dt, dl, db, dr) # Offset if offset: (x, y) = offset comp.setOffset(x, y) # Side if (side == 'right'): sideConstant = Layout.RIGHT elif (side == 'top'): sideConstant = Layout.TOP elif (side == 'bottom'): sideConstant = Layout.BOTTOM elif (side == 'left'): sideConstant = Layout.LEFT elif (side == 'inside'): sideConstant = Layout.INSIDE elif (side == 'over'): sideConstant = Layout.OVER # Alignment # if (horizontalalignment == 'left'): # comp.setHorizontalAlignmentWithRegion(r,0.0) # elif (horizontalalignment == 'right'): # if (verticalalignment == 'top'): # comp.setVerticalAlignmentWithRegion(r,0.0) # elif (verticalalignment == 'bottom'): # comp.setVerticalAlignmentWithRegion(r,1.0) if isinstance(target, Region): # absolute location wrt a Region comp.setLocationRelativeToRegion(target, sideConstant) elif isinstance(target, tuple): # absolute location wrt a point (specified as (x,y)) comp.setLocationRelativeToRegion(Region(target[0], target[1], 1, 1), Layout.RIGHT) else: targetComponent = None if isinstance(target, str): # relative location to a string (image filename) targetComponent = anchor(Pattern(target)) targetComponent.setOpacity(0) elif isinstance(target, Pattern): # relative location to a pattern targetComponent = anchor(target) targetComponent.setOpacity(0) elif isinstance(target, SikuliGuideComponent): targetComponent = target if targetComponent: comp.setLocationRelativeToComponent(targetComponent, sideConstant) else: Debug.error("GuideComponentSetup: invalid target: ", target) return None # set shadow, different sizes for different types of components #TODO shadow handling if shadow == 'default': if (isinstance(comp, SikuliGuideCircle) or \ isinstance(comp, SikuliGuideRectangle) or \ isinstance(comp, SikuliGuideBracket)): comp.setShadow(5, 2) elif not (isinstance(comp, SikuliGuideSpotlight)): comp.setShadow(10, 2) # add the component to guide comp.updateComponent() _g.addToFront(comp) return comp
def addTarget(x): if (isinstance(x, Pattern)): pattern = x elif (isinstance(x, str)): pattern = Pattern(x) m.addTarget(pattern)
def _addComponentHelper(comp, target, side='best', margin=0, offset=(0, 0), horizontalalignment='center', verticalalignment='center', shadow='default'): # Margin if margin: if isinstance(margin, tuple): (dt, dl, db, dr) = margin else: (dt, dl, db, dr) = (margin, margin, margin, margin) comp.setMargin(dt, dl, db, dr) # Offset if offset: (x, y) = offset comp.setOffset(x, y) # Side if (side == 'right'): sideConstant = Layout.RIGHT elif (side == 'top'): sideConstant = Layout.TOP elif (side == 'bottom'): sideConstant = Layout.BOTTOM elif (side == 'left'): sideConstant = Layout.LEFT elif (side == 'inside'): sideConstant = Layout.INSIDE elif (side == 'over'): sideConstant = Layout.OVER # Alignment # if (horizontalalignment == 'left'): # comp.setHorizontalAlignmentWithRegion(r,0.0) # elif (horizontalalignment == 'right'): # # if (verticalalignment == 'top'): # comp.setVerticalAlignmentWithRegion(r,0.0) # elif (verticalalignment == 'bottom'): # comp.setVerticalAlignmentWithRegion(r,1.0) if isinstance(target, Region): # absolute location wrt a Region comp.setLocationRelativeToRegion(target, sideConstant) elif isinstance(target, tuple): # absolute location wrt a point (specified as (x,y)) comp.setLocationRelativeToRegion(Region(target[0], target[1], 1, 1), Layout.RIGHT) else: if isinstance(target, str): # relative location to a string (image filename) targetComponent = anchor(Pattern(target)) targetComponent.setOpacity(0) elif isinstance(target, Pattern): # relative location to a pattern targetComponent = anchor(target) targetComponent.setOpacity(0) elif isinstance(target, SikuliGuideComponent): targetComponent = target comp.setLocationRelativeToComponent(targetComponent, sideConstant) # set shadow, different sizes for different types of components if shadow == 'default': if (isinstance(comp, SikuliGuideCircle) or isinstance(comp, SikuliGuideRectangle) or isinstance(comp, SikuliGuideBracket)): comp.setShadow(5, 2) elif not (isinstance(comp, SikuliGuideSpotlight)): comp.setShadow(10, 2) # add the component to guide _g.addToFront(comp) return comp
def testCanDisplayPattern(self): # Should have and a md5 of the path the pattern refers to formatter = Formatter(Pattern("C:/image.png")) self.assertEqual(str(formatter), '["Pattern("C:/image.png").similar(0.7)"](md5.png)')
def performFind(self): """ Loads the baseline images from disk and finds them on the screen. Works with the Transforms class to load extra data stored in the PNG's to control how they're matched. """ # sequence and series for series in self.seriesRange: regions = [] lastRegion = self.region nextRegion = Region(self.region) # try to match all images in the sequence try: for (sequence, filename) in enumerate( self.getImageNames(series=series, state=self.state)): transform = self.transform(filename, entity=self.entity, regionsMatched=regions, context=self) # Apply prev search attribs nextRegion = transform.apply( nextRegion, self.transform.CONTEXT_PREVIOUS) # Apply search attribs pattern = transform.apply(Pattern(filename), self.transform.CONTEXT_CURRENT) self.logger.trace("Loading %%s", self.logger.getFormatter()(pattern)) # find the image on the screen lastRegion = nextRegion.wait( pattern ) # If we don't set to zero wait time (dialog handler threads wait indefinitely) lastRegion = transform.apply(lastRegion, self.transform.CONTEXT_MATCH) self.logger.trace( "validated %%s %%s in region %%s nameType=%s colType=%s ser=%s seq=%s" % (self.nameType, self.collectionType, series, sequence), self.logger.getFormatter()(pattern), self.logger.getFormatter()(lastRegion), self.logger.getFormatter()(nextRegion)) regions.append(lastRegion) # Transform next region with the spacial region # spacialRegion is only used if there are spacial modifiers nextRegion = transform.apply(Region(nextRegion), self.transform.CONTEXT_NEXT, override=lastRegion) except FindFailed, e: self.logger.trace( "failed to find on screen %%s in %%s nameType=%s colType=%s ser=%s seq=%s" % (self.nameType, self.collectionType, series, sequence), self.logger.getFormatter()(self).setLabel("Images"), self.logger.getFormatter()(nextRegion)) else: region = None for currentRegion in regions: if not region: region = Region(currentRegion) else: region.add(currentRegion) region = transform.apply(region, self.transform.CONTEXT_FINAL) # Apply entity transforms transform.apply(self.entity, self.transform.CONTEXT_ENTITY) self.lastRegionFound = region self.lastSeriesFound = series return region
if args.element_image_mapping_file is not None: app.element_locator_map_file = args.element_image_mapping_file else: app.element_locator_map_file = os.path.join(os.path.curdir,'element_image_map.cfg') if args.images_folder is not None: app.image_path = args.images_folder else: app.image_path = os.path.join(os.path.curdir,'images') if args.sikuli_ide_dir is not None: app.sikuli_ide_dir = args.sikuli_ide_dir else: app.sikuli_ide_dir = os.path.curdir app.similarity = args.similarity app.timeout = args.timeout app.config = ConfigParser.RawConfigParser() app.config.read(app.element_locator_map_file) app.SS = Screen() app.PT = Pattern() app.Buttons = Button() app.Keys = Key() app.KeyMods = KeyModifier() app.SS.setAutoWaitTimeout(app.timeout) app.element_counter = 0 app.element_list = [] app.SESSION_ID = "%s:%d" % (args.address, args.port) app.started = False run(app, host=args.address, port=args.port)