class Server(): def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.write_file = WriteUserCommand() self.obj_port = Port() def get_devices(self): """ 获取设备信息且进行处理 例如:127.0.0.1:21503/t device :return:127.0.0.1:21503 device """ devices_list = [] result_list = self.dos.execute_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 kill_server(self): """ 查看服务命令:tasklist | find "node.exe" :return: """ server_list = self.dos.execute_cmd_result( 'tasklist | find "node.exe"') # 查看是否开启node.exe服务 if len(server_list) > 0: self.dos.execute_cmd('taskkill -F -PID "node.exe"') def create_port_list(self, start_port): """ 创建可用端口 :return: """ port_list = self.obj_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 :param i: :return: """ 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 {0} -bp {1} -U {2} --no-reset --session-override".format( appium_port_list[i], bootstrap_port_list[i], device_list[i]) 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): """ 执行命令,启动服务 :param i: :return: """ self.start_list = self.create_command_list(i) self.dos.execute_cmd(self.start_list[0]) def main(self): self.kill_server() # 杀进程 self.write_file.clear_data() # 清空yaml中数据 print(len(self.device_list)) try: for i in range(len(self.device_list)): # 连接这台机器的所有设备 appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start() time.sleep(15) except Exception as error: print("没有可用设备")
class Server(): ''' 对获取的设备信息再做一次处理,结果如下: ['127.0.0.1:62001', '127.0.0.1:62025'] ''' def __init__(self): self.dos_cmd = DosCmd() self.result = self.dos_cmd.execute_cmd_result('adb devices') #['List of devices attached ', '127.0.0.1:62001\tdevice', '127.0.0.1:62025\tdevice'] self.write_user_command = WriteUserCommand() self.port = Port() self.pport = self.port.create_port_list('4705', self.get_devices()) self.bpport = self.port.create_port_list('4900', self.get_devices()) def get_devices(self): # 处理完成后例如:['127.0.0.1:62001', '127.0.0.1:62025'] new_result_list = [] if len(self.result) >= 2: for i in range(1, len(self.result)): new_result = self.result[i].split('\t')[0] new_result_list.append(new_result) return new_result_list else: return None def create_start_cmd_list(self, i): # ['appium -p 4700 -bp 4900 -u 设备名 --...',''] # main函数传入的i 决定将哪个-p、-bp和设备组合 append到list中 cmd_list = [] cmd = 'appium -p ' + str(self.pport[i]) + ' -bp ' + str( self.bpport[i]) + ' -U ' + str( self.get_devices()[i]) + ' --no-reset --session-override' cmd_list.append(cmd) self.write_user_command.write_command(i, str(self.pport[i]), str(self.bpport[i]), str(self.get_devices()[i])) print(cmd_list) return cmd_list def start_server(self, i): # 将main函数传入的i,传给create_start_cmd_list() self.dos_cmd.execute_cmd(self.create_start_cmd_list(i)[0]) # time.sleep(5) # self.basedriver = BaseDriver(i) # self.basedriver.Android_driver() def kill_server(self): # Windows杀node.exe进程 # 1)tasklist | find "node.exe" # 2)taskkill -F -PID node.exe server_list = self.dos_cmd.execute_cmd_result( 'tasklist | find "node.exe"') if len(server_list) > 0: self.dos_cmd.execute_cmd("taskkill -F -PID node.exe") def main(self): ''' 多线程启动 ''' self.kill_server() self.write_user_command.clean_yaml() if self.get_devices() != None: for i in range(len(self.get_devices())): thread = threading.Thread(target=self.start_server, args=(i, )) thread.start() else: return None # server = Server() # server.main() # server.kill_server()
class Server: def __init__(self): self.dos_cmd = DosCmd() self.port = Port() self.write_yaml = WriteDeviceYaml() self.get_devices_num = self.get_devices_list() # 获取所有设备的列表 def get_devices_list(self): device_list = [] devices = self.dos_cmd.execute_result_cmd("adb devices") if len(devices) >= 2: for device_name in devices: if "List" in device_name: continue flag = device_name.split("\t")[1] if flag == "device": device_list.append(device_name.split("\t")[0]) return device_list else: return None # 创建可用的端口 def create_use_port(self, start_port): return self.port.create_use_port(start_port, self.get_devices_list()) # 生成命令 def create_command_list(self, i): # appium -p 4700 -bp 4900 -U 127.0.0.1:62001 command_list = [] appium_list = self.create_use_port(4700) bootstrap_list = self.create_use_port(4900) devices = self.get_devices_list() command = " appium -p " + str(appium_list[i]) + " -bp " + str( bootstrap_list[i] ) + " -U " + str( devices[i] ) + " --no-reset --session-override --log E:/pycharmProject/appium/appiumProject/log/appium_log" + str( i) + ".log" command_list.append(command) # 把设备的端口,bootstrap端口和设备名称写入yaml文件中,方便在初始化driver时候使用 self.write_yaml.write_value(i, str(appium_list[i]), str(bootstrap_list[i]), str(devices[i])) return command_list # 启动服务 def start_server(self, i): start_list = self.create_command_list(i) print(start_list) # 因为是在多线程中,每次启动,只有第一个命令 self.dos_cmd.execute_cmd(start_list[0]) # 关闭服务 def kill_server(self): node_list = self.dos_cmd.execute_result_cmd( 'tasklist | find "node.exe"') # print(node_list) if len(node_list) > 0: self.dos_cmd.execute_cmd('taskkill -F -PID node.exe') # 启动服务的主入口,几个设备就启动几个服务 def main(self): # 启动程序的时候,先把已开启的node.exe程序杀死 self.kill_server() # 再把数据写入yaml文件前先把yaml文件中数据清空 self.write_yaml.clear_value() for i in range(len(self.get_devices_num)): appium_thread = threading.Thread(target=self.start_server, args=(i, )) appium_thread.start() # 因为是多个机器,这个地方一定要等待一下,让所有的设备都能运行起来 sleep(5)
class Server_ios(): ''' 对获取的设备信息再做一次处理,结果如下: ['127.0.0.1:62001', '127.0.0.1:62025'] ''' def __init__(self): self.dos_cmd = DosCmd() self.result = self.dos_cmd.execute_cmd_result('idevice_id --list') #['List of devices attached ', '127.0.0.1:62001\tdevice', '127.0.0.1:62025\tdevice'] self.write_user_command = WriteUserCommand() self.port = Port() self.pport = self.port.create_port_list('4705', self.get_devices()) self.bpport = self.port.create_port_list('4900', self.get_devices()) def get_devices(self): # 处理完成后例如:['127.0.0.1:62001', '127.0.0.1:62025'] new_result_list = [] if len(self.result) >= 1: for i in range(0, len(self.result)): new_result = self.result[i].split('\t')[0] new_result_list.append(new_result) return new_result_list else: return None def create_start_cmd_list(self, i): # ['node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js --port 4725 --bootstrap-port 4726',''] # main函数传入的i 决定将哪个-p、-bp和设备组合 append到list中 cmd_list = [] cmd = 'node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js --port ' + str( self.pport[i]) + ' --bootstrap-port ' + str( self.bpport[i]) + ' --session-override' cmd_list.append(cmd) self.write_user_command.write_command_ios(i, str(self.pport[i]), str(self.bpport[i]), str(self.get_devices()[i])) print(cmd_list) return cmd_list def start_server(self, i): # 将main函数传入的i,传给create_start_cmd_list() self.dos_cmd.execute_cmd(self.create_start_cmd_list(i)[0]) # time.sleep(5) # self.basedriver = BaseDriver(i) # self.basedriver.Android_driver() def kill_server(self): # MAC杀node.exe进程---sudo lsof -c node # kill -9 pid pass def main(self): ''' 多线程启动 ''' self.kill_server() self.write_user_command.clean_yaml() if self.get_devices() != None: for i in range(len(self.get_devices())): thread = threading.Thread(target=self.start_server, args=(i, )) thread.start() else: return None # # server = Server_ios() # # # server.get_devices() # server.main() # # server.kill_server()
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")