class Server: def __init__(self): self.cmd = Command() self.port = Port() self.write_user_command = WriteUserCommand() self.device_list = self.get_devices() def get_devices(self): """ 获取设备信息 :return: """ result_list = self.cmd.excute_command_result("adb devices") device_list = [] if len(result_list) > 1: for d in result_list: if d == 'List of devices attached': continue elif '\tdevice' in d: device_list.append(d.replace('\tdevice', '')) return device_list else: return None def create_port_list(self, start_port): port_list = self.port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self, i): command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_list = self.create_port_list(4900) command = "appium -p " + str(appium_port_list[i]) + " -bp " + str(bootstrap_list[i]) + " -U " + self.device_list[i] + \ " --no-reset --session-override --log appium.log" command_list.append(command) self.write_user_command.write_data(i, self.device_list[i], bootstrap_list[i], appium_port_list[i]) # port device 配对关系写入配置文件,base文件读取 return command_list def start_server(self, i): """ 启动服务 :return: """ appium_command = self.create_command_list(i) self.cmd.excute_command(appium_command) def kill_server(self): """ 杀掉之前的进程 :return: """ server_list = self.cmd.excute_command_result("ps -ef | grep node") if server_list: self.cmd.excute_command("killall node") def main(self): """ 多线程启动appium :return: """ self.kill_server() self.write_user_command.clear_data() appium_list = [] for i in range(len(self.device_list)): appium_t = threading.Thread(target=self.start_server, args=(i, )) print("id: ", i) appium_list.append(appium_t) [i.start() for i in appium_list]
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: def __init__(self): #获取设备信息 self.dos = Doscmd() self.device_list = self.get_device() self.write_file = WriteUserCommand() def get_device(self): #存储设备信息 device_list = [] result_list = self.dos.execute_cmd_result( '/Users/kang/Library/Android/sdk/platform-tools/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, startPort): port_list = [] port = Port() port_list = port.create_port_list(startPort, 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' command_list.append(command) self.write_file.write_data(i, str(bootstrap_port_list[i]), str(device_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.execute_cmd(self.start_list[0]) #杀死appium服务 def kill_server(self): server_list = self.dos.execute_cmd_result( 'ps -ef | grep node | grep -v grep') if len(server_list) > 0: self.dos.execute_cmd('killall -9 node') #启动 def main(self): # 一进来先杀进程再清楚yaml文件 self.kill_server() thread_list = [] self.write_file.clear_data() for i in range(len(self.device_list)): 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: ''' 几个手机创建几个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.device_list = self.get_devices() self.write_file = WriteUserCommand() def get_devices(self): """获取设备信息""" devices_list = [] result_list = self.dos.execute_cmd_result('adb devices') if len(result_list) >= 1: for i in result_list: if 'daemon' in i or 'offline' in i or 'unauthorized' in i: logger.info("❗️❗❗Device Not Found❗❗❗") continue devices_list.append(i.split('\t')[0]) return devices_list else: return None def create_port_list(self, start_port): """创建可用端口""" port = Port() port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self, i): # appium -p 4700 -bp 4900 -U deviceName command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str(bootstrap_port_list[i]) + ' -U ' + \ self.device_list[i] + ' --no-reset --session-override' command_list.append(command) self.write_file.write_data(i, self.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) logger.info("{0} Start Appium Server: {1}".format(success, self.start_list)) self.dos.execute_cmd(self.start_list[0]) def main(self): """多线程启动appium""" self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = multiprocessing.Process(target=self.start_server, args=(i,)) appium_start.start() time.sleep(8) def get_process_pid(self, command): """获取程序进程pid window → tasklist | find "node.exe" mac → ps -ef | grep node """ pid_list = [] process_list = self.dos.execute_cmd_result(command) if len(process_list) > 2: for i in range(2, len(process_list)): pid = process_list[i].split()[1] pid_list.append(pid) return pid_list else: return None def kill_server(self): """杀掉进程 window → taskkill -F -PID node.exe mac → kill -9 pid """ process_list = self.get_process_pid('ps -ef | grep node') if process_list: for i in process_list: self.dos.execute_cmd('kill -9 ' + i) logger.info("{0} Appium Process PID {1} completed cleanup".format(success, i)) else: logger.info("Appium no startup, no need to clean up")