Exemplo n.º 1
0
 def __init__(self, report, resmgr_backend=None):
     '''构造函数
     
     :param report: 测试报告
     :type report: ITestReport
     '''
     self.__report = report
     if resmgr_backend is None:
         resmgr_backend = LocalResourceManagerBackend()
     self._resmgr = TestResourceManager(resmgr_backend)
Exemplo n.º 2
0
        """遍历全部资源(可以按照优先级顺序返回来影响申请资源的优先级)

        :param res_group: 资源分组
        :type res_group: str
        :param condition: 资源属性匹配
        :type condition: dict
        :returns: iterator of resource, dict type with key 'id'
        :rtypes: iterator(dict)
        """
        device_list = LocalDeviceProvider.list()
        
        for device in device_list:
            yield {'id': device}
            
# 注册android本地资源
LocalResourceManagerBackend.register_resource_type("android", AndroidDeviceResourceHandler())    

class IDeviceProvider(object):

    def connect_device(self, dev_prop):
        '''连接设备

        :param dev_prop: 设备属性
        :type  dev_prop: dict

        :return: Device实例
        '''
        pass

    def release_device(self, dev_id):
        '''释放设备
Exemplo n.º 3
0
    def iter_resource(self, res_group=None, condition=None):
        DeviceManager.update_local_devices()
        if isinstance(condition, six.string_types):
            condition = {'udid': condition}
        for dev in DeviceManager.devices:
            if condition:
                for k in condition:
                    if condition[k] == dev[k]:
                        break
                else:
                    if condition:
                        continue
            yield dev


LocalResourceManagerBackend.register_resource_type('ios',
                                                   IOSDeviceResourceHandler())


class DeviceManager(object):
    '''设备管理类,用于多个设备的申请、查询和管理(此类仅用于Device内部,测试用例中请勿直接使用)
    '''

    devices = []

    @staticmethod
    def get_devices_from_driver(driver_ip, driver_port=12306):
        driver = RPCClientProxy('http://%s:%d/host/' %
                                (driver_ip, driver_port),
                                allow_none=True,
                                encoding=Encoding)
        driver_devices = driver.list_devices()
Exemplo n.º 4
0
    def __init__(self, attrs={}, devicemanager=None):
        '''Device构造函数

        :param attrs: 设备UDID字符串|设备属性字典
                                    keys: udid            - 设备UDID
                                          host            - 设备主机ip
                                          is_simulator    - 是否为模拟器
        :type attrs: str|dict
        :param devicemanager: 设备管理类
        :type devicemanager: DeviceManager
        '''
        cond = {}
        if isinstance(attrs, six.string_types):
            cond['udid'] = attrs
        elif isinstance(attrs, dict):
            cond = attrs.copy()
        else:
            raise Exception('Device attributes type error: %s' % type(attrs))
        ct = None
        if devicemanager:
            self._device_resource = devicemanager.acquire_device(cond)
        else:
            ct = context.current_testcase()
            if ct is None:
                self._test_resources = TestResourceManager(
                    LocalResourceManagerBackend()).create_session()
            else:
                self._test_resources = ct.test_resources
            self._device_resource = self._test_resources.acquire_resource(
                "ios", condition=cond)

        if self._device_resource is None:
            raise Exception('无可用的真机和模拟器: %s' % str(cond))

        props = self._device_resource
        if 'properties' in self._device_resource:  # online mode
            devprops = self._device_resource['properties']
            props = {
                p['name'].encode(Encoding): p['value'].encode(Encoding)
                for p in devprops
            }
            props['id'] = self._device_resource['id']

        if 'csst_uri' not in props or props['csst_uri'] == 'None':
            props['csst_uri'] = None

        self._device_resource = DeviceResource(props['host'], int(
            props['port']), props['udid'], props['is_simulator'],
                                               props['name'], props['version'],
                                               props['csst_uri'], props['id'])

        self._base_url = self._device_resource.driver_url
        self._ws_uri = self._device_resource.ws_uri
        self._host = RPCClientProxy('/'.join([self._base_url, 'host/']),
                                    self._ws_uri,
                                    allow_none=True,
                                    encoding=Encoding)
        self._device_udid = self._device_resource.udid
        self._device_name = self._device_resource.name
        self._device_ios = self._device_resource.version
        if isinstance(self._device_resource.is_simulator, bool):
            self._device_simulator = self._device_resource.is_simulator
        else:
            self._device_simulator = self._device_resource.is_simulator == str(
                True)

        if self._device_simulator:
            self._host.start_simulator(self._device_udid)
        self._app_started = False
        Device.Devices.append(self)
        url = '/'.join([self._base_url, 'device', '%s/' % self._device_udid])
        self._driver = RPCClientProxy(url,
                                      self._ws_uri,
                                      allow_none=True,
                                      encoding=Encoding)
        self._keyboard = Keyboard(self)
        logger.info('[%s] Device - Connect - %s - %s (%s)' %
                    (datetime.datetime.fromtimestamp(
                        time.time()), self.name, self.udid, self.ios_version))
        # 申请设备成功后,对弹窗进行处理
        rule = settings.get('QT4I_ALERT_DISMISS', DEFAULT_ALERT_RULE)
        if rule:
            try:
                self._dismiss_alert(rule)
            except:
                logger.exception('dismiss alert %s' % rule)
Exemplo n.º 5
0
# -*- coding: utf-8 -*-

from testbase.plan import TestPlan
from testbase.resource import LocalResourceHandler, LocalResourceManagerBackend


class HelloResourceHandler(LocalResourceHandler):
    def iter_resource(self, res_type, res_group=None, condition=None):
        for i in range(2):
            yield {"id": i + 1}


LocalResourceManagerBackend.register_resource_type("hello",
                                                   HelloResourceHandler())


class HelloTestPlan(TestPlan):
    tests = "test.sampletest.runnertest"
    test_target_args = {}

    def test_setup(self, report):
        report.info("plan", "test_setup")

    def test_teardown(self, report):
        report.info("plan", "test_teardown")

    def resource_setup(self, report, res_type, resource):
        report.info("plan",
                    "resource_setup-%s-%s" % (res_type, resource["id"]))

    def resource_teardown(self, report, res_type, resource):