コード例 #1
0
class Port:
    def prot_is_used(self, port_num):
        '''
        判断端口是否被占用
        '''
        flag = None
        self.dos = DosCmd()
        command = 'netstat -ano | findstr ' + port_num
        result = self.dos.excute_cmd_get_result(command)
        if len(result) > 0:
            flag = True
        else:
            flag = False
        return flag

    def create_port_list(self, start_prot, device_list):
        '''
        生成可以端口
        @parameter start_prot
        #parameter device_list
        '''
        port_list = []
        print(device_list)
        if device_list != None:
            while len(port_list) != len(device_list):
                if self.prot_is_used(str(start_prot)) != True:
                    port_list.append(start_prot)
                start_prot = start_prot + 1
            return port_list
        else:
            print('生成可用端口失败')
            return None
コード例 #2
0
class Server:
    def __init__(self):
        self.dos = DosCmd()

    def get_devices(self):
        '''
        获取设备信息
        '''
        self.dos = DosCmd()
        devices_list = []
        result_list = self.dos.excute_cmd_get_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.get_devices())
        return port_list

    def create_command_list(self):
        #appium -p 4700 -bp 4701 -U 9HQ0219524007103
        command_list = []
        appium_port_list = self.create_port_list(4700)
        bootstrap_port_list = self.create_port_list(4900)
        device_list = self.get_devices()
        for i in range(len(device_list)):
            #--no-reset 即可避免执行用例的时候再次安装app,--session-override 不必每次重启session
            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)
        return command_list

    def start_server(self, i):
        self.start_list = self.create_command_list()
        self.dos.excute_cmd(self.start_list[i])

    def kill_server(self):
        server_list = self.dos.excute_cmd_get_result(
            'tasklist | find "node.exe"')
        print('server_list len:' + str(len(server_list)))
        if len(server_list) > 0:
            self.dos.excute_cmd('taskkill -F -PID node.exe')

    def main(self):
        self.kill_server()
        for i in range(len(self.create_command_list())):
            appium_start = threading.Thread(target=self.start_server,
                                            args=(i, ))
            appium_start.start()