def get_object_property(self, window_name, object_name, property_name): """ [关键字概要] 获取一个对象的属性值 :@参数 window_name: 窗口名称 :@参数 object_name: 对象名称 :@参数 property_name: 属性名称 :@返回值: 属性值字符串 Examples: | *Test Cases* | *Returns* | *Action* | *Argument* | *Argument* | *Argument* | | Example_Test | ${property_value}= | Get Object Property | ${window_name} | ${object_name} | ${prop_name} | """ try: self._info("Get object property value (%s, %s, %s)" % (window_name, object_name, property_name)) return ldtp.getobjectproperty(window_name, object_name, property_name) except LdtpExecutionError: raise LdtpExecutionError("get object property failed")
def _gettextvalue(self, window_name, object_name, startPosition=None, endPosition=None): def findFirst(node, search_string): try: children = node.children except: return None for child in children: if self._matches(search_string, child.name): return child else: child = findFirst(child,search_string) if child: return child retval = "" if ldtp.getobjectproperty(window_name, object_name, "class") == "label": f = dogtail.tree.root.application('subscription-manager-gui') w = f.childNamed(window_name) o = findFirst(w, object_name) if o: retval = o.text else: raise Exception("Cannot find object: %s in tree."%(object_name)) else: retval = ldtp.gettextvalue(window_name, object_name, startPosition, endPosition) if not ((isinstance(retval, str) or isinstance(retval, unicode))): retval = "" return retval
def printProperties(objs=None, deep=0, deepstart=0): ''' Print all Properties of all available objects in a custom deep. If objs is None, it uses CurrentObjs. ''' for o in getTree(objs,deep,deepstart): print( "%-80s -> {" % o , end="" ) for p in ldtp.getobjectpropertieslist(subContext(o),o): print( "'"+p+"':'" + ldtp.getobjectproperty(subContext(o),o,p) +"' " , end="") print("}")
def getProperties(objs=None): ''' Return all Properties (in a dictionary) for each objects. If objs is None, it uses CurrentObjs. ''' objs, rstr = __checkObjs(objs) r = [] for o in objs: dic = {} for p in ldtp.getobjectpropertieslist(subContext(o),o): dic[p]=ldtp.getobjectproperty(subContext(o),o,p) r.append( dic ) return __checkR(r,rstr)
def _gettablerowindex(self, window, table, target): numrows = ldtp.getrowcount(window, table) numcols = len(ldtp.getobjectproperty(window, table, 'children').split()) for i in range(0,numrows): for j in range(0,numcols): try: value = ldtp.getcellvalue(window, table, i, j) if self._matches(target,value): ldtp.selectrowindex(window, table, i) return i except: continue raise Exception("Item not found in table!")
def to_script( self, val ,flag ,key=None): print flag if(define.NORMAL==flag): val = val.replace( "<parent>", "\"%s\""%self.parent ) val = val.replace( "<self>", "\"%s\""%self.self ) if(define.PTAB==flag): #ptab flag=1 val=val.replace("<parent>","\"%s\""%self.parent) val=val.replace("<tab_list_name>","\"ptl0\"") tmp=ldtp.getobjectproperty(self.parent,self.self,'child_index') val=val.replace("<self_index>","%d"%tmp) if(define.LST==flag): #lst flag=2 val=val.replace("<parent>","\"%s\""%self.parent) val=val.replace("<component_name>","\"%s\""%key) tmp=ldtp.getobjectproperty(self.parent,self.self,'child_index') val=val.replace("<self_index>","%d"%tmp) if(define.MNU_ITEM==flag): #mnu_item flag=3 val=val.replace("<parent>","\"%s\""%self.parent) val=val.replace("<mnu;mnuitem>","\"%s\""%key) if(define.TBL==flag): #tbl flag=4 val=val.replace("<parent>","\"%s\""%self.parent) val=val.replace("<self>","\"%s\""%self.self) tmp=ldtp.getrowcount(self.parent,self.self) val=val.replace("<child_index>","0~%d"%(tmp-1)) return val
def capture_translations(app="app",lang="unknown"): wins1=l.getwindowlist() time.sleep(2) # sometimes the window is not ready yet wins=l.getwindowlist() img_name = l.imagecapture() re_pattern = r"(.*)auto([0-9]+)auto(.*)" for w in wins: obj = l.getobjectlist(w) w_clean = re.sub(re_pattern,"\\1\\3",w) for o in obj: info = l.getobjectinfo(w,o) o_clean = re.sub(re_pattern,"\\1\\3",o) if 'label' in info: label = l.getobjectproperty(w,o,'label') size = None # check if we find the "automate language" m = re.search(re_pattern, label) if m: id_num = m.group(2) size = l.getobjectsize(w,o) if size[0] > 0: translation[id_num] = (w_clean, o_clean) w_o_mapping[(w_clean, o_clean)] = id_num print("Translation #{} is here: ('{}','{}')".format(id_num, w_clean, o_clean)) print("Located in picture: {}".format(size)) else: size = None # or check if we already know the translation from the "automate language"-run elif (w, o) in w_o_mapping.keys(): id_num = w_o_mapping[(w, o)] size = l.getobjectsize(w,o) if size[0] > 0: print("Found translation #{}".format(id_num)) else: size = None # in both cases we want a screenshot if size: img = cv2.imread(img_name) new_img = cv2.rectangle(img, (size[0], size[1]), (size[0] + size[2], size[1] + size[3]), (0,0,255), 3) cv2.imwrite('/tmp/translation_{}_{}_{}_{}.png'.format(app, lang, id_num, o), new_img)
def capture_translations(app="app",lang="unknown"): wins1=l.getwindowlist() time.sleep(2) # sometimes the window is not ready yet wins=l.getwindowlist() img_name = l.imagecapture() re_pattern = r"(.*)auto([0-9]+)auto(.*)" for w in wins: obj = l.getobjectlist(w) w_clean = re.sub(re_pattern,"\\1\\3",w) for o in obj: info = l.getobjectinfo(w,o) o_clean = re.sub(re_pattern,"\\1\\3",o) if 'label' in info: label = l.getobjectproperty(w,o,'label') size = None # check if we find the "automate language" m = re.search(re_pattern, label) if m: id_num = m.group(2) size = l.getobjectsize(w,o) if size[0] > 0: translation[id_num] = (w_clean, o_clean) w_o_mapping[(w_clean, o_clean)] = id_num print("Translation #{} is here: ('{}','{}')".format(id_num, w_clean, o_clean)) print("Located in picture: {}".format(size)) else: size = None # or check if we already know the translation from the "automate language"-run elif (w, o) in w_o_mapping.keys(): id_num = w_o_mapping[(w, o)] size = l.getobjectsize(w,o) if size[0] > 0: print("Found translation #{}".format(id_num)) else: size = None # in both cases we want a screenshot if size: img = cv2.imread(img_name) new_img = cv2.rectangle(img, (size[0], size[1]), (size[0] + size[2], size[1] + size[3]), (0,0,255), 3) cv2.imwrite('{}/translation_{}_{}_{}_{}.png'.format(OUTPUT_DIR, app, lang, id_num, o), new_img)
def step_impl(context, thing): time.sleep(1) win = None for w in l.getwindowlist(): objs = l.getobjectlist(w) if thing in objs: win = w break if not win: print(f"Failed to find {thing}") return print("Parent:") print(l.getobjectproperty(win, thing, 'parent')) (x, y, w, h) = l.getobjectsize(win, thing) click_x = x + (w / 2) click_y = y + (h / 2) print(f"Clicking {click_x}/{click_y} in {x}/{y}+{w}/{h}") if click_x < 0 or click_y < 0: print(f"I'd rather not click {click_x}/{click_y}") print(f"failed to click {thing} in {l.getobjectlist(win)}") return # assert click_x > 0, f"I'd rather not click {click_x}/{click_y}" # assert click_y > 0, f"I'd rather not click {click_x}/{click_y}" context._root["_click_animated"](context, click_x, click_y)
def step_impl(context, win): lang = context._root.get('my_lang', None) w_o_mapping = context._root.get('w_o_mapping', {}) feature = context._stack[1]["feature"].filename line = context._root["my_line"] po_map = context._root.get('po_map', {}) locator_map = context._root.get('locator_map', {}) (win, _) = _resolveNames(context, win) # _resolveNames() already saved the indexes - we can quit now if lang is None: return img_name = l.imagecapture() re_pattern = r"(.*)auto([0-9]+)auto(.*)" time.sleep(1) obj = l.getobjectlist(win) w_clean = re.sub(re_pattern, "\\1\\3", win) # avoiding multiple appearances with the same filename multi_appear = {} for o in obj: print(f"Get info for {o}") info = l.getobjectinfo(win, o) o_clean = re.sub(re_pattern, "\\1\\3", o) # "or o_clean" means that the pattern can be applied if 'label' in info or o_clean: if 'label' in info: print("Get real label") label = l.getobjectproperty(win, o, 'label') else: print("Use object as label") label = o print(f" label: {label}") size = None if label is None: print("label is none") continue try: size = l.getobjectsize(win, o) if size[0] < 0: size = None except Exception as e: print(e) size = None # check if we find the "automate language" m = re.search(re_pattern, label) if m: print(f"Found automate here: {label}") id_num = m.group(2) if size: # store "translation ID" (i.e. po-line number) for later w_o_mapping[(w_clean, o_clean, line)] = id_num print("Translation #{} is here: ('{}','{}')".format( id_num, w_clean, o_clean)) print("Located in picture: {}".format(size)) # or check if we already know the translation from the "automate language"-run elif (win, o, line) in w_o_mapping.keys(): print(f"Found mapping: {label}") id_num = w_o_mapping[(win, o, line)] if size: print("Found translation #{}".format(id_num)) # SMELL: move to feature function and execute only once if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) img = cv2.imread(img_name) x = size[0] y = size[1] w = size[2] h = size[3] new_img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3) timestamp = int(time.time()) filename = f"{feature}_{lang}_po{id_num}_featureline_{line}_{timestamp}" # for multiple ocurrances of one translation in the same window and same step multi_appear[filename] = multi_appear.get(filename, 0) + 1 filename = f"{filename}_{multi_appear[filename]}.png" cv2.imwrite(os.path.join(OUTPUT_DIR, filename), new_img) if timestamp not in locator_map: locator_map[timestamp] = [] locator_map[timestamp].append({ "x": x, "y": y, "w": w, "h": h, "timestamp": timestamp, "name": f"PO{id_num}", "filename": filename }) else: print("Neither Automate nor mapping") context._root["w_o_mapping"] = w_o_mapping context._root["locator_map"] = locator_map