示例#1
0
class Server:
    def __init__(self):
        self.cmd = DOScmd()
        self.port = Port()
        self.write_file = WriteUserCommand()
        self.device_list = self.get_devices()

    def get_devices(self):
        # """
        # 	通过adb devices命令查询当前PC所连接的设备数量,并将查询的设备信息储存为一个list
        # """
        devices_list = []
        result_list = self.cmd.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])
        else:
            print("找不到已连接设备")
            devices_list = None

        return devices_list

    def create_command_list(self, i, start_port, start_bootstrap_port):
        # """
        # 	拼接生成Appium的启动命令
        # 	start_port:-p后接的起始端口值
        # 	start_bootstrap_port:-bp后接的起始端口值
        # """
        command_list = []
        if self.device_list != None:
            t = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.time()))
            port_list = self.port.create_port_list(start_port,
                                                   self.device_list)
            # print(port_list)
            bootstrap_port_list = self.port.create_port_list(
                start_bootstrap_port, self.device_list)
            #拼接Appium启动命令
            command = "appium -p {} -bp {} -U {} --no-reset --session-override --log ../log/{}_log_{}".format(
                port_list[i], bootstrap_port_list[i], self.device_list[i],
                self.device_list[i], t)
            command_list.append(command)
            self.write_file.Write_data(i, self.device_list[i], port_list[i],
                                       bootstrap_port_list[i])
            print(command_list)
            return command_list
        else:
            print('生成Appium启动命令失败')
            return None

    def start_server(self, i):
        #封装一个启动Appium的方法
        start_list = self.create_command_list(i, 4700, 4900)
        self.cmd.excute_cmd(start_list[0])

    def kill_server(self, process):
        #封装一个杀掉Appium进程的方法
        server_list = self.cmd.excute_cmd_result(
            'tasklist | find "{}"'.format(process))
        # print(server_list)
        if len(server_list) > 0:
            self.cmd.excute_cmd("taskkill -F -PID {}".format(process))

    def main(self):
        self.kill_server('node.exe')  #清除进程
        self.write_file.clear_data()  #清除历史数据
        for i in range(len(self.device_list)):
            # 通过多线程方法,同时启动多个Appium
            appium_start = threading.Thread(target=self.start_server,
                                            args=(i, ))
            appium_start.start()
        time.sleep(20)
示例#2
0
class Server:
    def __init__(self):
        self.dos = DosCmd()
        self.device_list = self.get_devices()
        self.write_file = WriteUserCommand()
        self.base_driver = BaseDriver()

    def get_devices(self):
        """
        获得设备信息
        :return: 设备list
        """
        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_list = port.create_port_list(start_port, self.device_list)
        return port_list

    def create_command(self, i):
        """
        根据设备个数,生成最终启动appium的命令行list
        :return:
        """
        # appium -p 4700 -bp 4701 -U 127.0.0.1:21503
        command_list = []
        port_list = self.create_port_list(4700)
        bp_list = self.create_port_list(4900)
        device_list = self.device_list

        command = "appium -p " + str(port_list[i]) + " -bp " + str(
            bp_list[i]
        ) + " -U " + device_list[i] + " --no-reset --session-override"
        command_list.append(command)
        self.write_file.write_data(i, device_list[i], str(bp_list[i]),
                                   str(port_list[i]))
        return command_list

    def start_server(self, i):
        """
        启动服务
        :param i:控制执行启动服务的次数,如果只有一个设备,则只启动一次服务,以此类推
        :return:
        """
        self.command_list = self.create_command(i)
        self.dos.excute_cmd(self.command_list[0])

    def kill_server(self):
        """
        杀掉appium进程
        :return:
        """
        server_list = self.dos.excute_cmd_result("ps aux |  grep appium")
        if len(server_list) > 0:
            self.dos.excute_cmd("pkill -f appium")

    def main(self):
        """
        多线程启动appium服务
        :return:
        """
        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, ))
            appium_start.start()
            time.sleep(10)