案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述appium常用封装相关的知识,希望能为你提供帮助。
一、Dos下执行adb devices获取udid封装
1 #coding=utf-8 2 import os 3 class DosCmd: 4def excute_cmd_result(self,command): 5result_list = [] 6result = os.popen(command).readlines() 7for i in result: 8if i ==‘ ‘: 9continue 10result_list.append(i.strip(‘ ‘)) 11return result_list 12 13def excute_cmd(self,command): 14os.system(command) 15 16 if __name__ == ‘__main__‘: 17dos = DosCmd() 18print dos.excute_cmd_result(‘adb devices‘)
二、判断端口是否被占用简单封装
1 #coding=utf-8 2 from dos_cmd import DosCmd 3 class Port: 4def port_is_used(self,port_num): 5‘‘‘ 6判断端口是否被占用 7‘‘‘ 8flag = None 9self.dos = DosCmd() 10command = ‘netstat -ano | findstr ‘+str(port_num) 11result = self.dos.excute_cmd_result(command) 12if len(result)> 0: 13flag = True 14else: 15flag = False 16return flag 17 18def create_port_list(self,start_port,device_list): 19‘‘‘start_port 4701 20生成可用端口 21@parameter start_port 22@parameter device_list 23‘‘‘ 24port_list = [] 25if device_list != None: 26while len(port_list) != len(device_list): 27if self.port_is_used(start_port) != True: 28port_list.append(start_port) 29start_port = start_port +1 30return port_list 31else: 32print "生成可用端口失败" 33return None 34 35 36 if __name__ == ‘__main__‘: 37port = Port() 38li = [1,2,3,4,5] 39print port.create_port_list(4722,li)
三、对yaml文件读写操作,获取命令行返回
文章图片
文章图片
1 #coding=utf-8 2 import yaml 3 class WriteUserCommand: 4def read_data(self): 5‘‘‘ 6加载yaml数据 7‘‘‘ 8with open("../config/userconfig.yaml") as fr: 9data = https://www.songbingjia.com/android/yaml.load(fr) 10return data 11 12def get_value(self,key,port): 13‘‘‘ 14获取value 15‘‘‘ 16data = self.read_data() 17value = data[key][port] 18return value 19 20def write_data(self,i,device,bp,port): 21‘‘‘ 22写入数据 23‘‘‘ 24data = self.join_data(i,device,bp,port) 25with open("../config/userconfig.yaml","a") as fr: 26yaml.dump(data,fr) 27 28def join_data(self,i,device,bp,port): 29data = https://www.songbingjia.com/android/{ 30"user_info_"+str(i):{ 31"deviceName":device, 32"bp":bp, 33"port":port 34} 35} 36return data 37 38def clear_data(self): 39with open("../config/userconfig.yaml","w") as fr: 40fr.truncate() 41fr.close() 42 43def get_file_lines(self): 44data = https://www.songbingjia.com/android/self.read_data() 45return len(data) 46 47 48 if __name__ == ‘__main__‘: 49write_file = WriteUserCommand() 50print write_file.get_value(‘user_info_2‘,‘bp‘)
View Code四、简单的获取设备信息、创建可用端口、生成command命令、多线程启动服务封装
文章图片
文章图片
1 #coding=utf-8 2 from dos_cmd import DosCmd 3 from port import Port 4 import threading 5 import time 6 from write_user_command import WriteUserCommand 7 class Server: 8def __init__(self): 9self.dos = DosCmd() 10self.device_list = self.get_devices() 11self.write_file = WriteUserCommand() 12def get_devices(self): 13‘‘‘ 14获取设备信息 15‘‘‘ 16 17devices_list = [] 18result_list = self.dos.excute_cmd_result(‘adb devices‘) 19if len(result_list)> =2: 20for i in result_list: 21if ‘List‘ in i: 22continue 23devices_info = i.split(‘ ‘) 24if devices_info[1] == ‘device‘: 25devices_list.append(devices_info[0]) 26return devices_list 27else: 28return None 29def create_port_list(self,start_port): 30‘‘‘ 31创建可用端口 32‘‘‘ 33port = Port() 34port_list = [] 35port_list = port.create_port_list(start_port,self.device_list) 36return port_list 37 38def create_command_list(self,i): 39‘‘‘ 40生成命令 41‘‘‘ 42command_list = [] 43appium_port_list = self.create_port_list(4700) 44bootstrap_port_list = self.create_port_list(4900) 45device_list = self.device_list 46command = "appium -p "+str(appium_port_list[i])+" -bp "+str(bootstrap_port_list[i])+" -U "+device_list[i]+" --no-reset --session-override --log D:/log/test02.log" 47command_list.append(command) 48self.write_file.write_data(i,device_list[i],str(bootstrap_port_list[i]),str(appium_port_list[i])) 49 50return command_list 51 52def start_server(self,i): 53‘‘‘ 54启动服务 55‘‘‘ 56self.start_list = self.create_command_list(i) 57print self.start_list 58 59self.dos.excute_cmd(self.start_list[0]) 60 61def kill_server(self): 62server_list = self.dos.excute_cmd_result(‘tasklist | find "node.exe"‘) 63if len(server_list)> 0: 64self.dos.excute_cmd(‘taskkill -F -PID node.exe‘) 65 66def main(self): 67thread_list = [] 68self.kill_server() 69self.write_file.clear_data() 70for i in range(len(self.device_list)): 71appium_start = threading.Thread(target=self.start_server,args=(i,)) 72thread_list.append(appium_start) 73for j in thread_list: 74j.start() 75time.sleep(25) 76 77 78 if __name__ == ‘__main__‘: 79server = Server() 80print server.main()
View Code【appium常用封装】
推荐阅读
- Appium失败截图及重试机制封装
- Android Jetpack 组建介绍——Lifecycler
- wap2app-- wap2app的原生标题头无法隐藏
- XSLT xsl(apply-template元素)
- XPath语法
- XPath相对路径
- XPath节点
- XPath表达式
- 什么是XPath