class Port: def port_is_used(self, port): self.dos = DosCmd() result = self.dos.excute_cmd('netstat -an | grep %s' % port) if len(result) > 0: flag = True else: flag = False return flag def create_port(self, start_port=4700, device_list=None): port_list = [] if len(device_list) > 0: while len(device_list) != len(port_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port += 1 return port_list else: return '不存在可用的设备信息,无法生成appium端口'
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.write_file = WriteUserCommand() def get_devices(self): ''' 获取设备信息 ''' devices_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'List' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': devices_list.append(devices_info[0]) return devices_list else: return None def create_port_list(self, start_port): ''' 创建可用端口 ''' port = Port() port_list = [] port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self, i): ''' 生成命令 ''' #appium -p 4700 -bp 4701 -U 127.0.0.1:21503 command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U " + device_list[ i] + " --no-reset --session-override --log E:/Teacher/Imooc/AppiumPython/log/test02.log" #appium -p 4723 -bp 4726 -U 127.0.0.1:62001 --no-reset --session-override --log E:/Teacher/Imooc/AppiumPython/log/test01.log command_list.append(command) self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]), str(appium_port_list[i])) return command_list def start_server(self, i): ''' 启动服务 ''' self.start_list = self.create_command_list(i) print self.start_list self.dos.excute_cmd(self.start_list[0]) def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') def main(self): thread_list = [] self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) thread_list.append(appium_start) for j in thread_list: j.start() time.sleep(25)
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.write_file = WriteUserCommand() def get_devices(self): ''' 获取设备信息 ''' devices_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'list' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': # list index out of range devices_list.append(devices_info[0]) return devices_list else: return None def create_port_list(self, start_post): ''' 创建可用端口 ''' port = Port() port_list = [] port_list = port.create_post_list(start_post, self.device_list) return port_list def create_command_list(self, i): ''' 生成命令 ''' #appium -p 4700 -bp 4701 -U 127.0.0.1:21503 command_list = [] appium_post_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command = "appium -p " + str(appium_post_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U " + device_list[i] + " --no-reset --session-override" command_list.append(command) self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]), str(appium_post_list[i])) return command_list def start_server(self, i): #启动服务 self.start_list = self.create_command_list(i) print self.start_list self.dos.excute_cmd(self.start_list[0]) def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('tasklist -f -PID node.exe') def main(self): self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start()
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.appium_port_list = self.create_port(4700, self.device_list) self.bootstrap_port_list = self.create_port(5700, self.device_list) def get_devices(self): ''' 获取设备信息 :return: ''' devices_list = [] result_list = self.dos.excute_cmd('adb devices') if len(result_list) > 2: for i in result_list: if 'List' in i or 'device' not in i: continue devices_info = i.split('\t') if 'device' in devices_info[1]: devices_list.append(devices_info[0]) return devices_list else: return None def port_is_used(self, port): self.dos = DosCmd() result = self.dos.excute_cmd('netstat -an | grep %s' % port) if len(result) > 0: flag = True else: flag = False return flag def create_port(self, start_port=4700, device_list=None): port_list = [] if len(device_list) > 0: while len(device_list) != len(port_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port += 1 return port_list else: return '不存在可用的设备信息,无法生成appium端口' def create_command(self): ''' android 启动appium服务 appium -p 4700 -bp 4701 -u 168.98.34.12:1234 :return: ''' command_list = [] for i in range(len(self.appium_port_list)): command_list.append( 'appium -p %s -bp %s -U %s' % (self.appium_port_list[i], self.bootstrap_port_list[i], self.device_list[i])) return command_list def start_server(self, i): command_list = self.create_command() self.dos.excute_cmd(command_list[i]) def start1(self): for i in range(len(self.create_command())): appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start()
class Server: def __init__(self): self.dos = DosCmd() self.port = CreatePort() self.write_yaml = WriteUserconfigCommand() self.device_list = self.get_devices() self.start_list = self.start_appium_command_list() def get_devices(self): """ 获取设备信息 :return: """ command = 'adb devices' result_list = self.dos.excute_cmd_result(command) # print(result_list) devices_list = [] if len(result_list) > 1: for i in result_list: if '\tdevice' in i: devices_info = i.split('\tdevice') devices_list.append(devices_info[0]) return devices_list else: return None def create_port(self, start_port): """ 生成端口号列表 :param start_port: :return: """ port = CreatePort() port_list = port.create_port_list(start_port, self.device_list) return port_list def start_appium_command_list(self): # appium -p 4700 -bp 4701 -U 4f368950 """ 生成启动appium服务的命令列表集合 :return: """ # 先清空掉yaml文件 然后再写入 self.write_yaml.clear_data() command_list = [] appium_port_list = self.create_port(4700) bootstrap_port_list = self.create_port(4900) if self.device_list is not None: for i in range(len(self.device_list)): command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str( bootstrap_port_list[i]) + ' -U ' + str(self.device_list[i]) + ' --no-reset --session-override --log D:/code/log/test01.log --local-timezone ' command_list.append(command) self.write_yaml.write_data_to_yaml(i, self.device_list[i], bootstrap_port_list[i], appium_port_list[i]) return command_list def start_appium_server(self, i): """ 通过启动命令集合 执行单个启动服务命令 :param i: :return: """ self.dos.excute_cmd(self.start_list[i]) def kill_appium_server(self): find_command = 'tasklist | find "node.exe"' server_list = self.dos.excute_cmd_result(find_command) if len(server_list) > 1 : kill_command = 'taskkill -F -PID node.exe' self.dos.excute_cmd(kill_command) def main(self): """ 使用多线程运行启动设备服务 :return: """ # 先把当前的进程全部kill掉 self.kill_appium_server() # 多线程开始启动服务 for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_appium_server, args=(i,)) appium_start.start() time.sleep(10)
class Server: ''' 获取设备信息 ''' def __init__(self): self.write_file = WriteUserCommand() self.dos = DosCmd() self.device_list = self.get_devices() def get_devices(self): device_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) > 1: for i in result_list: if 'List' in i: continue device_info = i.split('\t') if device_info[1] == 'device': device_list.append(device_info[0]) return device_list else: return None def create_port_list(self, start_port): port = Port() port_list = [] port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self,i): ''' appium -p 4700 -bp 4701 -U 127.0.0.1:62001 :return: ''' command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4800) deivce_list = self.device_list self.write_file.post_value(i,bootstrap_port_list[i],deivce_list[i],appium_port_list[i]) command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str(bootstrap_port_list[i]) + ' -U ' + \ deivce_list[i] + ' --no-reset --session-override' command_list.append(command) if len(command_list) == 0: print('当前无可用设备') else: print(command_list) return command_list # 启动程序服务 def start_server(self, i): self.start_list = self.create_command_list(i) self.dos.excute_cmd(self.start_list[0]) # 杀掉进程 def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') else: print('没有找到相关进程') # 主函数使用多线程去调用start_server def main_server(self): self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i,)) appium_start.start() time.sleep(5)
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_device_list() self.opera_yaml = OperaYaml() def get_device_list(self): #获取设备列表 print 'start get devices...' device_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'List' in i: continue device_info = i.split('\t') if device_info[1] == 'device': device_list.append(device_info[0]) return device_list else: return None def create_port_list(self, start_port): #创建可用端口列表 port = Port() port_list = port.create_port_list(start_port, len(self.device_list)) return port_list def create_command_i(self, i): #生成命令,非命令列表 #appium -p 4700 -bp 4900 -U deviceName appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command_i = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str( bootstrap_port_list[i] ) + ' -U ' + device_list[i] + ' --no-reset --session-override' print command_i self.opera_yaml.write_data(i, bootstrap_port_list[i], device_list[i], appium_port_list[i]) return command_i def start_server(self, i): #启动服务 self.start_command = self.create_command_i(i) print "start server..." self.dos.excute_cmd(self.start_command) def kill_server(self): print 'start kill server...' #停止服务 server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') def main(self): #主函数多线程 print 'start main server...' thread_list = [] self.kill_server() self.opera_yaml.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) thread_list.append(appium_start) for j in thread_list: print 'start thread ...' j.start() time.sleep(20)
class Server: def __init__(self): self.dos = DosCmd() def get_devices(self): ''' 获取设备信息 ''' self.dos = DosCmd() devices_list = [] result_list = self.dos.excute_cmd_get_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'List' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': devices_list.append(devices_info[0]) return devices_list else: return None def create_port_list(self, start_port): ''' 创建可用端口 ''' port = Port() port_list = [] port_list = port.create_port_list(start_port, self.get_devices()) return port_list def create_command_list(self): #appium -p 4700 -bp 4701 -U 9HQ0219524007103 command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.get_devices() for i in range(len(device_list)): #--no-reset 即可避免执行用例的时候再次安装app,--session-override 不必每次重启session command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U " + device_list[i] + " --no-reset --session-override" command_list.append(command) return command_list def start_server(self, i): self.start_list = self.create_command_list() self.dos.excute_cmd(self.start_list[i]) def kill_server(self): server_list = self.dos.excute_cmd_get_result( 'tasklist | find "node.exe"') print('server_list len:' + str(len(server_list))) if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') def main(self): self.kill_server() for i in range(len(self.create_command_list())): appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start()