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.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 D:/work/AppiumTest/log/test02.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(10)
def __init__(self): self.dos_cmd = DosCmd() self.port = Port() self.write_yaml = WriteDeviceYaml() self.get_devices_num = self.get_devices_list()
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()
def __init__(self): self.dos_cmd = DosCmd()
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)
def __init__(self): self.dos = DosCmd() self.clear_data = WriteUserCommand() self.device_list = self.get_device()
def __init__(self): self.cmd = DosCmd()
def __init__(self): self.p = Port() self.dev = Devices() self.doc = DosCmd() self.oy = OperateYaml() self.devices_list = self.dev.get_devices()
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(object): def __init__(self): self.p = Port() self.dev = Devices() self.doc = DosCmd() self.oy = OperateYaml() self.devices_list = self.dev.get_devices() def create_appium_command(self, i): ''' 根据设备列表拼接出对应appium启动命令 :return: ''' command_list = [] appium_port_list = self.p.create_port_list(4000) # 根据获取设备信息创建对应端口列表 appium_bp_port_list = self.p.create_port_list(4500) if self.devices_list != None: # for i in range(len(devices_list)): # 根据设备信息长度,对3个list进行for循环,依次去除并拼接好敏灵 command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( appium_bp_port_list[i] ) + " -U " + str( self.devices_list[i] ) + " --no-reset --session-override" + " --log D:\Job\python\Script\Lipei_app\\report\log\\" + str( self.devices_list[i]) + ".log" #将log写入对应文件 command_list.append(command) self.oy.write_yaml(i, str(self.devices_list[i]), str(appium_bp_port_list[i]), str(appium_port_list[i])) return command_list def start_server(self, i): ''' 启动appium服务,变量i通过多线程appium_start传入 :param i: :return: ''' self.start_list = self.create_appium_command( i) # 带self的就表示对象本身,属于该py全局的,这里就可以认为定义全局变量 self.doc.cmd_no_result(self.start_list[0]) #注意这里不能传i,仅能是0 time.sleep(20) def appium_start(self): ''' 根据接入设备多线程启动服务 :return: ''' self.kill_servee() # self.oy.clear_data() for i in range(len(self.devices_list)): # 根据得到的启动命令列表长度,确认该启动几个线程 appium_server = multiprocessing.Process(target=self.start_server, args=(i, )) appium_server.start() def kill_servee(self): ''' 查找appium进程:tasklist | find "node.exe"; 杀死所有node进程:taskkill -F -PID "node.exe" :return: ''' command = 'tasklist | find "node.exe"' task_msg = self.doc.cmd_result(command) if len(task_msg) > 0: self.doc.cmd_no_result('taskkill -F -PID node.exe') def close_driver(self, i): dr = BaseDriver() dr.close_driver(i)
def __init__(self): self.dos = DosCmd() self.write_file = WriteDeviceCommand() self.devices_list = self.get_devices()
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 '启动失败'
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")
def __init__(self): self.dos = DosCmd()