def __setitem__(self, k, v): if k in self.__fields: super(Selector, self).__setitem__(U(k), U(v)) super(Selector, self).__setitem__(self.__mask, self[self.__mask] | self.__fields[k][0]) else: raise ReferenceError("%s is not allowed." % k)
def dump_hierarchy(self, compressed=False, pretty=False) -> str: """ Args: shell (bool): use "adb shell uiautomator dump" to get hierarchy pretty (bool): format xml Same as content = self.jsonrpc.dumpWindowHierarchy(compressed, None) But through GET /dump/hierarchy will be more robust when dumpHierarchy fails, the atx-agent will restart uiautomator again, then retry """ res = self.server._reqsess.get(self.server.path2url("/dump/hierarchy")) try: if res.status_code == 500: if not self.server.uiautomator.running(): self.server.uiautomator.start() # 恢复uiautomator的运行 raise UiautomatorQuitError("dump hierarchy", res.text) res.raise_for_status() except requests.HTTPError: logging.warning("request error: %s", res.text) raise content = res.json().get("result") if pretty and "\n " not in content: xml_text = xml.dom.minidom.parseString(content.encode("utf-8")) content = U(xml_text.toprettyxml(indent=' ')) return content
def all(self): """ Returns: list of XMLElement """ xml_content = self._source or self._d.dump_hierarchy() root = etree.fromstring(xml_content.encode('utf-8')) for node in root.xpath("//node"): node.tag = safe_xmlstr(node.attrib.pop("class")) match_nodes = root.xpath( U(self._xpath), namespaces={"re": "http://exslt.org/regular-expressions"}) return [XMLElement(node) for node in match_nodes]
def dump_hierarchy(self, compressed=False, pretty=False) -> str: """ Args: shell (bool): use "adb shell uiautomator dump" to get hierarchy pretty (bool): format xml Same as content = self.jsonrpc.dumpWindowHierarchy(compressed, None) But through GET /dump/hierarchy will be more robust when dumpHierarchy fails, the atx-agent will restart uiautomator again, then retry v-1.3.4 change back to jsonrpc.dumpWindowHierarchy """ content = self.jsonrpc.dumpWindowHierarchy(compressed, None) if pretty and "\n " not in content: xml_text = xml.dom.minidom.parseString(content.encode("utf-8")) content = U(xml_text.toprettyxml(indent=' ')) return content
def send_keys(self, text): """ Raises: EnvironmentError """ try: self.wait_fastinput_ime() btext = U(text).encode('utf-8') base64text = base64.b64encode(btext).decode() self.server.shell([ 'am', 'broadcast', '-a', 'ADB_INPUT_TEXT', '--es', 'text', base64text ]) return True except EnvironmentError: warnings.warn( "set FastInputIME failed. use \"d(focused=True).set_text instead\"", Warning) return self(focused=True).set_text(text)
def send_keys(self, text: str, clear: bool = False): """ Args: text (str): text to set clear (bool): clear before set text Raises: EnvironmentError """ try: self.wait_fastinput_ime() btext = U(text).encode('utf-8') base64text = base64.b64encode(btext).decode() cmd = "ADB_SET_TEXT" if clear else "ADB_INPUT_TEXT" self.server.shell( ['am', 'broadcast', '-a', cmd, '--es', 'text', base64text]) return True except EnvironmentError: warnings.warn( "set FastInputIME failed. use \"d(focused=True).set_text instead\"", Warning) return self(focused=True).set_text(text)
def dump_hierarchy(self, compressed=False, pretty=False): content = self.jsonrpc.dumpWindowHierarchy(compressed, None) if pretty and "\n " not in content: xml_text = xml.dom.minidom.parseString(content.encode("utf-8")) content = U(xml_text.toprettyxml(indent=' ')) return content