class Port: def port_is_used(self, port_num): #判断端口是否被占用 self.dos = DosCmd() flag = None command = 'netstat -ano | findstr ' + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, devices_list): #生成可用端口 port_list = [] if devices_list != None: #直到端口数等于设备数之前,运行下面代码 while len(port_list) != len(devices_list): #找到没被占用的端口 if self.port_is_used(start_port) == False: #添加到端口列表 port_list.append(start_port) #端口加1 start_port = start_port + 1 return port_list else: print('生成可用端口失败') return None
class Port: ''' 检查端口是否被暂用 netstat -ano | findstr **** 有结果即有占用 ''' def port_is_used(self, port): flag = None self.dos = DosCmd() result = self.dos.excute_cmd_result('netstat -ano | findstr ' + str(port)) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, device_list): ''' 生成可用端口 :param start_port: 起始端口 :param device_list: 获取到的设备数 :return:根据设备数返回一个可用端口的列表 ''' port_list = [] if device_list is not None: while len(port_list) != len(device_list): if self.port_is_used(start_port) is not True: port_list.append(start_port) start_port = start_port + 1 return port_list else: print("生成可用端口失败") return None
class Port: def port_is_used(self, port_num): ''' 判断端口是否被占用 :param port_num int :return: True被占用、False没有被占用 ''' self.dos = DosCmd() command = 'netstat -ano | findstr ' + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: return True else: return False def create_port_list(self, start_port, device_list): ''' 生成可用端口 :param start_port:int :param device_list: list :return: port_list ''' if device_list != None: port_list = [] port = start_port while len(port_list) != len(device_list): if self.port_is_used(port) != True: port_list.append(port) port += 1 return port_list else: print('==端口生成失败==') return None
class Port: def port_is_used(self,port_num): ''' 判断端口是否被占用 ''' flag = None self.dos = DosCmd() command = 'netstat -ano | findstr '+str(port_num) result = self.dos.excute_cmd_result(command) if len(result)>0: flag = True else: flag = False return flag def create_port_list(self,start_port,device_list): '''start_port 4701 生成可用端口 @parameter start_port @parameter device_list ''' port_list = [] if device_list != None: while len(port_list) != len(device_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port = start_port +1 return port_list else: print("生成可用端口失败") return None
class Port: def __init__(self): self.dos = DosCmd() def port_is_used(self, port_number): """ #判断端口是否被调用 :param port_number: :return: """ command = 'netstat -ano | findstr ' + str(port_number) # 运行dos命令 判断端口是否被占用:如果有值(结果大于0) 表示被占用 结果返回True dos_result = self.dos.excute_cmd_result(command) if len(dos_result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, devices_list=None): """ 生成可用端口 :param start_port: :param devices_list: :return: """ port_list = [] if devices_list is not None: while len(port_list) != len(devices_list): if not self.port_is_used(start_port): # 如果端口不为True 即端口被占用 port_list.append(start_port) # 将端口添加到port_list start_port = start_port + 1 # 因为添加端口之后,端口已经被占用,所以start_port就应该增加1 return port_list else: print('生成可用端口失败') return None
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.appium_port_list = self.create_port_list(4700) self.bootstrap_port_list = self.create_port_list(4900) self.command_list = self.create_command_list() self.wuc = WriteUserCommand() def get_devices(self): ''' 获取设备信息 :return: ''' result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: devices_list = [ i.split('\t')[0] for i in result_list if (not i.startswith('List') and i.split('\t')[1] == 'device') ] return devices_list else: return None def create_port_list(self, start_port): ''' 创建可用端口 :param start_port: :return: port_list ''' port = Port() port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self): #appium -p 4700 -bp 4800 -U 127.0.0.1:21503 --no-reset --session-override --log C:/log.log print(self.appium_port_list, self.bootstrap_port_list, self.device_list) command_list = ['appium -p {0} -bp {1} -U {2} --no-reset --session-override --log ./log/Phone{3}.log'.format( self.appium_port_list[i],self.bootstrap_port_list[i],self.device_list[i], i) \ for i in range(len(self.device_list))] return command_list def start_server(self, i): ''' 启动appium服务 :param i: command_list[i] :return: ''' self.dos.excute_cmd(self.command_list[i]) 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 write_user_command(self): i = 0 for port, bp, deviceName in zip(self.appium_port_list, self.bootstrap_port_list, self.device_list): self.wuc.write_data(i, port, bp, deviceName) i += 1 def main(self): self.kill_server() self.wuc.clear_data() self.write_user_command() for i in range(len(self.command_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.write_file = WriteUserCommand() def get_devices(self): ''' 获取设备信息 ''' devices_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 1: for i in result_list: devices_list.append(i) print(devices_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" command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str( bootstrap_port_list[i] ) + ' -U ' + device_list[i] + ' --no-reset --session-override' #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') else: return '当前无 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: ''' 几个手机创建几个appium用来连接手机使用 ''' def __init__(self): #执行控制台的命令使用 self.dos = DosCmd() #获取设备devices的集合 self.device_list = self.get_devices() #yaml的操作类 self.write_file = WriteUserCommand() def get_devices(self): ''' #获取设备devices的集合 ''' devices_list = [] #执行adb devices命令来获取 devices list result_list = self.dos.excute_cmd_result('adb devices') #取出devicees存入devices_list中 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 "+rootPath+"/log/test0"+str(i)+".log" command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U" + device_list[ i] + " --no-reset --session-override --log " + rootPath + "/log/test0" + str( i) + ".log" # command = "appium -p "+str(appium_port_list[i])+" -bp "+str(bootstrap_port_list[i])+" -U"+device_list[i]+" --session-override --log "+rootPath+"/log/test0"+str(i)+".log" #appium -p 4723 -bp 4726 -U 127.0.0.1:62001 --no-reset --session-override --log /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) self.dos.excute_cmd(self.start_list[0]) def kill_server(self): # 这里是windows 命令行 linux 使用命令 ps ef | grep node 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() #清除上一次userconfig1.yaml里面的数据 self.write_file.clear_data() for i in range(len(self.device_list)): #有几个drivaer创建几个线程 appium_start = multiprocessing.Process(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.clear_data = WriteUserCommand() self.device_list = self.get_device() # self.port = Port() def get_device(self): """ #获取设备信息 :return: """ 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): """ 创建可用端口 :param start_port: :return: """ port = Port() port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self, i): """ 启动appium 拼接 appium -p -bp -U :argument :return: """ write_file = WriteUserCommand() command_list = [] appium_port_list = self.create_port_list(4700) # 传入start_port,生成p端口列表 bootstrap_port_list = self.create_port_list(4900) # 生成bp端口列表 devices_list = self.device_list # 获取设备信息列表 # for i in range(len(devices_list)): # 拼接命令 command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( bootstrap_port_list[i]) + " -U " + str( devices_list[i]) + " --no-reset --session-override" # 把命令添加到command_list command_list.append(command) write_file.write_data(i, devices_list[i], str(bootstrap_port_list[i]), str(appium_port_list[i])) # 返回拼接好的命令,后续只需要循环这些命令 return command_list def start_server(self, i): """ 获取start_list 然后循环 :return: """ self.start_list = self.create_command_list(i) print(self.start_list) self.dos.excute_cmd(self.start_list[0]) def kill_server(self): """ 查找:tasklist | find "node.exe" 杀进程:taskkill -F -PID node.exe :return: """ # 查找结果列表 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): """ 多线程启动服务 :return: """ self.kill_server() self.clear_data.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(20)
class Server: def __init__(self): self.dos = DosCmd() self.write_file = WriteDeviceCommand() self.devices_list = self.get_devices() def get_devices(self): #获取设备信息device 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: print('有没检测到设备') return None def get_ports(self, start_port): #获取端口port port = Port() port_list = port.create_port_list(start_port, self.devices_list) return port_list def create_command_list(self, i): #拼接启动命令:appium -p 4700 -bp 4900 -U 127.0.0.1:21503 --no-reset --session-override --log D:/AppiumPython/log/log01.log if self.devices_list != None: appium_port_list = self.get_ports(4700) bootstrap_port_list = self.get_ports(4900) command_list = [] command = ( "appium -p " + str(appium_port_list[i]) + " -bp " + str(bootstrap_port_list[i]) + " -U " + self.devices_list[i] + " --no-reset --session-override --log D:/AppiumPython/log/log" + str(i) + ".log") command_list.append(command) self.write_file.write_data(i, self.devices_list[i], str(appium_port_list[i]), str(bootstrap_port_list[i])) return command_list else: print('创建启动命令失败') return None 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): self.kill_server() self.write_file.clear_data() if self.devices_list != None: for i in range(len(self.devices_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start() else: return '启动失败'