def execute_adb_shell(self, cmd, timeout): """ local adb shell :param cmd: 命令字符串 :param timeout: 设置超时时间,默认没有,单位为秒 :return: (未超时bool,stdoutput, erroutput) """ Logger.INFO(cmd) cmd_timer = None stdoutput = None erroutput = None p = subprocess.Popen("adb -s %s shell" % self.udid, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) if timeout is not None: try: timeout = int(timeout) except: timeout = 20 cmd_timer = Timer(timeout, self._kill_proc_by_timer, [p]) cmd_timer.start() try: cmd = (cmd + "\nexit\n").encode(encoding='utf-8') (stdoutput, erroutput) = p.communicate(cmd, timeout=timeout) except BaseException as err: print('except' + err) finally: if cmd_timer is not None: cmd_timer.cancel() print(stdoutput) print(erroutput) return (not self.has_timer_kill, stdoutput, erroutput) pass
def get_device_brand(self): cmd = "getprop ro.product.brand" # (execute_result, stdout, stderr) = self.adb_helper.adbstrategy.execute_adb_shell(cmd,timeout=30) (execute_result, stdout, stderr) = self.execute_adb_shell(cmd, timeout=30) Logger.INFO("brand type is: {}".format(stdout)) # brand = stdout.split("\n")[0] brand = stdout.decode("utf-8") #brand = stdout.split("\n") brand = brand.split("\n") print(len(brand)) if len(brand) <= 2: brandname = brand[0] else: brandname = stdout.split("\r\n")[3] #.split("\r")[0] return brandname
def get_available_device_list(): target_devices = [] request = urllib.request.Request(GET_DEVICE) request.add_header('Authorization', "Bearer %s" % AUTHOR) response = urllib.request.urlopen(request) try: devices = json.loads(response.read()) if devices['success']: for device in devices["devices"]: # print device['ready'], device['using'], device['present'], device['serial'] if device['ready'] and not device['using'] and device[ 'present']: target_devices.append(device['serial']) pass else: return target_devices except: import traceback traceback.print_exc() return None Logger.INFO(target_devices) return target_devices
def setU2Driver(self): driver = None try: adbshellcmd = "%s -s %s shell" % (whichcraft.which("adb"), self._serial) readObj = os.popen(adbshellcmd + " input keyevent 3") readObj.close() killATXAgent = "%s ps -ef | grep atx |awk '{print $2}'|xargs -I {} %s kill -9 {}" % ( adbshellcmd, adbshellcmd) readObj = os.popen(killATXAgent) readObj.close() killATXAgent = "%s ps | grep atx |awk '{print $2}'|xargs -I {} %s kill -9 {}" % ( adbshellcmd, adbshellcmd) readObj = os.popen(killATXAgent) readObj.close() readObj = os.popen("%s am force-stop %s" % (adbshellcmd, 'com.github.uiautomator')) readObj.close() time.sleep(3) cmd = "python -m uiautomator2 init --serial %s" % self._serial readObj = os.popen(cmd) readObj.close() time.sleep(3) times = 3 while (times): readObj = os.popen( "%s am start -n %s/%s" % (adbshellcmd, 'com.github.uiautomator', '.MainActivity')) readObj.close() time.sleep(2) # cur_app = self.current_app() # if cur_app.has_key('package'): # cur_pkg_name = cur_app['package'] # times -= 1 driver = u2.connect(self._serial) Logger.INFO(info="driver initialed", tag=self._serial) except: Logger.INFO(info="failed to connect to uiautomator2 first times", tag=self._serial) try: adbshellcmd = "%s -s %s shell" % (whichcraft.which("adb"), self._serial) killATXAgent = "%s ps -ef | grep atx |awk '{print $2}'|xargs -I {} %s kill -9 {}" % ( adbshellcmd, adbshellcmd) readObj = os.popen(killATXAgent) readObj.close() killATXAgent = "%s ps | grep atx |awk '{print $2}'|xargs -I {} %s kill -9 {}" % ( adbshellcmd, adbshellcmd) readObj = os.popen(killATXAgent) readObj.close() readObj = os.popen("%s am force-stop %s" % (adbshellcmd, 'com.github.uiautomator')) readObj.close() time.sleep(3) cmd = "python -m uiautomator2 init --mirror --serial %s" % self._serial readObj = os.popen(cmd) readObj.close() time.sleep(3) times = 3 while (times): readObj = os.popen("%s am start -n %s/%s" % (adbshellcmd, 'com.github.uiautomator', '.MainActivity')) readObj.close() time.sleep(2) # cur_app = self.current_app() # if cur_app.has_key('package'): # cur_pkg_name = cur_app['package'] # times -= 1 driver = u2.connect(self._serial) Logger.INFO("driver initialed twice time") except: adbshellcmd = "%s -s %s shell" % (whichcraft.which("adb"), self._serial) # killATXAgent = "%s ps | grep atx |awk '{print $2}'|xargs -I {} %s kill -9 {}"%( # adbshellcmd, adbshellcmd) # # subprocess.Popen(killATXAgent, stdout=subprocess.PIPE, # # stderr=subprocess.STDOUT, shell=True) # os.popen(killATXAgent) cmd = "python -m uiautomator2 init --mirror --serial %s" % self._serial # subprocess.Popen(cmd, stdout=subprocess.PIPE, # stderr=subprocess.STDOUT, shell=True) readObj = os.popen(cmd) readObj.close() time.sleep(3) try: driver = u2.connect(self._serial) Logger.INFO("driver initialed") except: Logger.INFO( "failed to connect to uiautomator2 third times") finally: if not driver: Logger.INFO("XXXXX failed to connect to uiautomator2 XXXX") return driver