예제 #1
0
    def run_server(self, server_name=''):
        '''运行测试桩进程,创建服务端
        '''
        if server_name == '': server_name = self.service_name
        from qt4a.androiddriver.androiddriver import AndroidDriver
        port = AndroidDriver.get_process_name_hash(server_name, self.get_device_id())
        logger.info('[DeviceDriver] port is %d' % port)
        addr = '127.0.0.1'
        if not self._is_local_device: addr = self.adb.host_name
        if self._client: self._client.close()
        self._client = None
        
        server_type = 'localabstract'
        if self.adb.is_rooted() and self.adb.is_selinux_opened():
            # 创建TCP服务端
            server_name = str(self.service_port)
            server_type = 'tcp'

        time0 = time.time()
        timeout = 20
        
        while time.time() - time0 < timeout:
            self._client = self._create_client(server_name, server_type)
            if self._client: return self._client
            ret = self._run_server(server_name)
            logger.debug('[DeviceDriver] Server %s process created: %s' % (server_name, ret))
            time.sleep(0.1)
        raise RuntimeError('连接系统测试桩超时')
예제 #2
0
 def _get_driver(self, process_name):
     '''获取AndroidDriver实例
     '''
     from qt4a.androiddriver.androiddriver import AndroidDriver
     if not process_name in self._driver_dict:
         driver = AndroidDriver.create(process_name, self._device)
         self._driver_dict[process_name] = driver
     return self._driver_dict[process_name]
예제 #3
0
파일: androidapp.py 프로젝트: wolf937/QT4A
 def get_driver(self, process_name=''):
     '''根据进程名获取driver对象,默认为主程序driver
     '''
     if not process_name:
         if not self._process_name: raise RuntimeError('主程序名为空')
         process_name = self._process_name
     if not self._drivers.has_key(process_name):
         # 只有尚未创建的时候才需要互斥
         # logger.debug('wait for %s create driver' % process_name)
         # 创建driver
         driver = AndroidDriver.create(process_name, self._device)
         self._drivers[process_name] = driver
         # logger.debug('%s create driver success' % process_name)
     driver = self._drivers[process_name]
     return driver
예제 #4
0
 def get_driver(self, process_name=None):
     driver = AndroidDriver(self._device._device_driver, None)
     AndroidDriver.send_command = mock.Mock(side_effect=mock_send_command)
     return driver
예제 #5
0
 def _create_driver(self):
     adb_backend = LocalADBBackend('127.0.0.1', '')
     adb = ADB(adb_backend)
     return AndroidDriver(DeviceDriver(adb), None)
예제 #6
0
    def run_server(self, server_name=''):
        '''运行测试桩进程,创建服务端
        '''
        if server_name == '': server_name = self.service_name
        from qt4a.androiddriver.androiddriver import AndroidDriver
        port = AndroidDriver.get_process_name_hash(server_name,
                                                   self.get_device_id())
        logger.info('[DeviceDriver] port is %d' % port)
        addr = '127.0.0.1'
        if not self._is_local_device: addr = self.adb.host_name
        if self._client: self._client.close()
        self._client = None

        time0 = time.time()
        timeout = 20
        kill_server = False

        server_type = 'localabstract'
        if self.adb.is_rooted() and self.adb.is_selinux_opened():
            # 创建TCP服务端
            server_name = str(self.service_port)
            server_type = 'tcp'

        while time.time() - time0 < timeout:
            if not kill_server and time.time() - time0 >= timeout // 2:
                # server进程存在问题,强杀
                self._kill_server()
                if self._client: self._client.close()
                self._client = None
                kill_server = True
            ret = self._run_server(server_name)
            logger.debug('[DeviceDriver] Server %s process created: %s' %
                         (server_name, ret))

            while port < 65536:
                self._client = self._create_client(addr, port)
                port_is_opened = AndroidSpyClient.server_opened(port)
                if not port_is_opened:
                    new_port = self.adb.forward(port, server_name, server_type)
                    if new_port != port:
                        self._client = self._create_client(addr, new_port)
                        logger.info('[AndroidDevice] new port=%d' % new_port)
                        port = new_port

                try:
                    hello_rsp = self.hello()
                    if hello_rsp == None:
                        if port_is_opened:
                            # 需要释放端口
                            ret = self.adb.remove_forward(port)
                            logger.info(
                                '[AndroidDevice] remove %d forward: %s' %
                                (port, ret))
                        break
                except RuntimeError:
                    port += 1
                    logger.info('[AndroidDevice] new port is %d' % port)
                    continue

                return self._client

        raise RuntimeError('连接系统测试桩超时')