Example #1
0
 def __getitem__(self, key):
     '''操作符"[]"重载
     
     :param key: 控件名
     :type key: str
     :rtype: object
     :raises: Exception
     '''
     if not isinstance(key, basestring) or not self._locators.has_key(key):
         raise Exception('子控件名错误: %s' % key)
     params = self._locators.get(key)
     if isinstance(params, dict):
         cls = params.get('type')
         root = params.get('root')
         locator = params.get('locator')
         title = params.get('title')
         url = params.get('url')
         usr_name = params.get('name')
         mt_instance = params.get('instance')
         params = {'root': root, 'locator': locator}
         if INS_IOS_DRIVER:
             while True:
                 if not isinstance(root, basestring): break
                 if re.match('^@', root):
                     root_key = re.sub('^@', '', root)
                     if not self._locators.has_key(root_key):
                         raise ControlNotFoundError('未找到父控件: %s' % root_key)
                     root_params = self._locators.get(root_key)
                     params = {
                         'root':
                         root_params.get('root'),
                         'locator':
                         str(root_params.get('locator')) +
                         str(params.get('locator'))
                     }
                     root = root_params.get('root')
         else:
             if isinstance(root, basestring) and re.match('^@', root):
                 root_key = root[1:]
                 if not self._locators.has_key(root_key):
                     raise ControlNotFoundError('未找到父控件: %s' % root_key)
                 params['root'] = self[root_key]
         if usr_name:
             params['name'] = usr_name
         params['id'] = key
         if title:
             params['title'] = title
         if url:
             params['url'] = url
         if mt_instance:
             params['instance'] = mt_instance
         return cls(**params)
     raise Exception('控件定义的结构异常: [%s]' % key)
Example #2
0
 def _find(self, parent_id=None):
     print '--- ' * 9
     print 'locator : %s' % self._locator
     print 'timeout : %s' % self.timeout.timeout
     # --- --- --- --- --- --- --- --- ---
     result = self._app.driver.element.find_elements(
         (str(self._locator) if self._locator else None),
         self.timeout.timeout, self.timeout.interval, self.strategy,
         parent_id)
     elements = result.get('elements', [])
     element = elements[0].get('element') if len(elements) > 0 else None
     # --- --- --- --- --- --- --- --- ---
     if element is None:
         if self.strategy == "id":
             err_msg = '\n未找到控件 : 耗时[%s]毫秒尝试了[%s]次查找 [%s]' % (result.get(
                 'find_time'), result.get('find_count'), self._locator)
             err_msg += '\n无效控件ID : %s' % self._locator
             err_msg += '\nControlNotFoundError 建议用 device.print_uitree() 重新分析UI树'
         else:
             err_msg = '\n未找到控件 : 耗时[%s]毫秒尝试了[%s]次查找 [%s]' % (result.get(
                 'find_time'), result.get('find_count'), self._locator)
             err_msg += '\n有效Path  : %s' % result.get('valid_path_part')
             err_msg += '\n无效Path  : %s' % result.get('invalid_path_part')
             err_msg += '\nControlNotFoundError 建议用 device.print_uitree() 重新分析UI树'
         raise ControlNotFoundError(err_msg)
     if len(elements) > 1 and not INS_IOS_DRIVER:
         err_msg = '\n找到多个控件: 耗时[%s]毫秒尝试了[%s]次查找,找到[%s]个控件' % (result.get(
             'find_time'), result.get('find_count'), len(elements))
         for e in elements:
             err_msg += '\n  %s' % e
         err_msg += '\nControlAmbiguousError 建议用 device.print_uitree() 重新分析UI树'
         raise ControlAmbiguousError(err_msg)
     print 'element : 耗时[%s]毫秒尝试了[%s]次查找到[ element id: %s ]' % (
         result.get('find_time'), result.get('find_count'), element)
     return element
Example #3
0
 def _find(self, parent_id=None):
     print '--- ' * 9
     print 'locator : %s' % self._locator
     print 'timeout : %s' % self.timeout.timeout
     # --- --- --- --- --- --- --- --- ---
     result = self._decode_find_result(
         self._app.driver.uia.element.find_elements(
             self._app.device.udid,
             (str(self._locator) if self._locator else None),
             self.timeout.timeout, self.timeout.interval, 'qpath',
             parent_id))
     elements = result.get('elements', [])
     element = elements[0].get('element') if len(elements) > 0 else None
     # --- --- --- --- --- --- --- --- ---
     if element:
         print 'element : 耗时[%s]毫秒尝试了[%s]次查找到[ element id: %s ]' % (
             result.get('find_time'), result.get('find_count'), element)
     # --- --- --- --- --- --- --- --- ---
     if element is None:
         err_msg = '\n未找到控件 : 耗时[%s]毫秒尝试了[%s]次查找 [%s]' % (result.get(
             'find_time'), result.get('find_count'), self._locator)
         err_msg += '\n有效Path  : %s' % result.get('valid_path_part')
         err_msg += '\n无效Path  : %s' % result.get('invalid_path_part')
         err_msg += '\nControlNotFoundError 建议用 device.print_uitree() 重新分析UI树'
         raise ControlNotFoundError(err_msg)
     # --- --- --- --- --- --- --- --- ---
     # 有较多脚本中的父窗口封装 /classname='UIAWindow' 没有使用 instance,暂不启用 --- cherry
     # if len(elements) > 1 :
     #     err_msg = '\n找到多个控件: 耗时[%s]毫秒尝试了[%s]次查找,找到[%s]个控件' % (result.get('find_time'), result.get('find_count'), len(elements))
     #     for item in elements: err_msg += '\n  %s' % item.get('attributes')
     #     err_msg += '\nControlAmbiguousError 建议用 device.print_uitree() 重新分析UI树'
     #     raise ControlAmbiguousError(err_msg)
     # --- --- --- --- --- --- --- --- ---
     return element
Example #4
0
 def __getitem__(self, key):
     '''操作符"[]"重载
     
     :param key: 控件名
     :type key: str
     :return: object
     :raises: Exception
     '''
     if not isinstance(key, basestring) or not self._locators.has_key(key):
         raise Exception('子控件名错误: %s' % key)
     params = self._locators.get(key)
     # -*- -*- -*- -*- -*- -*-
     # print 'key     : [%s]' % key
     # old_obj = self._controls.get(key, None)
     # if old_obj:
     #    if old_obj.is_valid():
     #        print 'element.locator       : %s' % old_obj._locator
     #        print 'element.id            : %s' % old_obj._id
     #        print '-' * 10
     #        return old_obj
     #    else:
     #        del self._controls[key]
     # -*- -*- -*- -*- -*- -*-
     if isinstance(params, basestring):
         new_obj = Element(self, params)
         # self._controls.update({key:new_obj})
         return new_obj
     # -*- -*- -*- -*- -*- -*-
     if isinstance(params, dict):
         cls = params.get('type')
         root = params.get('root')
         locator = params.get('locator')
         params = {'root': root, 'locator': locator}
         while True:
             if not isinstance(root, basestring): break
             if re.match('^@', root):
                 root_key = re.sub('^@', '', root)
                 if not self._locators.has_key(root_key):
                     raise ControlNotFoundError('未找到父控件: %s' % root_key)
                 root_params = self._locators.get(root_key)
                 params = {
                     'root':
                     root_params.get('root'),
                     'locator':
                     str(root_params.get('locator')) +
                     str(params.get('locator'))
                 }
                 root = root_params.get('root')
         new_obj = cls(**params)
         # self._controls.update({key:new_obj})
         return new_obj
     # -*- -*- -*- -*- -*- -*-
     raise Exception('控件定义的结构异常: [%s]' % key)