Пример #1
0
def save_http_response(response, content, save_as_group, variables, logger=logging.getLogger('py_test')):
    try:
        save_as_group = json.loads(save_as_group)
    except json.decoder.JSONDecodeError as e:
        raise ValueError('待保存内容无法解析。错误信息:{}'.format(getattr(e, 'msg', str(e))))
    success = True
    msg = ''
    for save_as in save_as_group:
        error_msg = None
        try:
            name = save_as['name']
            part = save_as['part']
            expression = save_as['expression']
        except KeyError:
            continue
        else:
            if part == 'header':
                response_json = json.dumps(response)
                if expression:
                    value = response.get(expression, None)
                    if not value:
                        find_result = vic_find_object.find_with_condition(expression, dict(response), logger=logger)
                        if find_result.is_matched and find_result.re_result:
                            value = find_result.re_result[0]
                        else:
                            error_msg = '响应头中没有找到满足表达式【{}】的内容'.format(expression)
                            if find_result.error_msg:
                                '{}\n在查找过程中出现错误:{}'.format(error_msg, find_result.error_msg)
                            logger.warning(error_msg)
                            value = ''
                            success = False
                else:
                    value = response_json
            else:
                if expression:
                    find_result = vic_find_object.find_with_condition(expression, content, logger=logger)
                    if find_result.is_matched and find_result.re_result:
                        value = find_result.re_result[0]
                    else:
                        error_msg = '响应体中没有找到满足表达式【{}】的内容'.format(expression)
                        if find_result.error_msg:
                            '{}\n在查找过程中出现错误:{}'.format(error_msg, find_result.error_msg)
                        logger.warning(error_msg)
                        value = ''
                        success = False
                else:
                    value = content
        msg_ = variables.set_variable(name, value)
        msg_ = '用例' + msg_
        if error_msg:
            msg_ = '{},所以{}'.format(error_msg, msg_)
        msg = msg + msg_ + '\n'
    return success, msg
Пример #2
0
def get_url(vic_step, print_=True):
    url = vic_step.dr.current_url
    condition_value = vic_step.ui_data

    if condition_value:
        find_result = vic_find_object.find_with_condition(
            condition_value, url, logger=vic_step.logger)
        if find_result.is_matched:
            if find_result.re_result:
                url = vic_find_object.get_first_text_in_re_result(
                    find_result.re_result)
            run_result = [
                'p', '找到符合条件【{}】的URL内容【{}】'.format(condition_value, url)
            ]
        else:
            msg = '找不到符合条件【{}】的URL内容'.format(condition_value)
            if find_result.error_msg:
                msg = '{}\n在查找过程中出现错误:{}'.format(msg, find_result.error_msg)
            run_result = ['f', msg]
    else:
        run_result = ['p', '获取到URL【{}】'.format(url)]
    if print_:
        vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str,
                                               run_result[1]))
    return run_result, url
Пример #3
0
def verify_db_test_result(expect, result, logger=logging.getLogger('py_test')):
    find_result = vic_find_object.find_with_condition(expect,
                                                      result,
                                                      logger=logger)
    if find_result.is_matched:
        run_result = ['p', find_result]
    else:
        run_result = ['f', find_result]
    return run_result
Пример #4
0
def wait_for_page_redirect(vic_step, print_=True):
    pause_time = 0
    _success = False
    _full_msg = ''
    msg = '新URL不符合期望【{}】'.format(vic_step.ui_data)
    msg_format = '经过{:.1f}秒 - {}'
    last_print_time = start_time = time.time()
    _error_msg = None
    while (time.time() - start_time -
           pause_time) <= vic_step.timeout and not vic_step.force_stop:
        current_url = vic_step.dr.current_url
        find_result = vic_find_object.find_with_condition(
            vic_step.ui_data, current_url, logger=vic_step.logger)
        if find_result.is_matched:
            msg = '新URL符合期望【{}】'.format(vic_step.ui_data)
            _success = True
        elif find_result.error_msg:
            _error_msg = find_result.error_msg
            msg = '新URL不符合期望【{}】\n在查找过程中出现错误:{}'.format(
                vic_step.ui_data, _error_msg)

        if time.time() - last_print_time >= 1:
            pause_time += vic_step.pause_()
            elapsed_time = time.time() - start_time - pause_time
            _full_msg = msg_format.format(elapsed_time, msg)
            if print_:
                vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str,
                                                       _full_msg))
            last_print_time = time.time()

        if _success or _error_msg:
            break

    elapsed_time = time.time() - start_time - pause_time
    full_msg = msg_format.format(elapsed_time, msg)
    if print_ and full_msg != _full_msg:
        vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str, full_msg))
    if _success:
        run_result = ['p', full_msg]
    else:
        run_result = ['f', full_msg]
    return run_result, elapsed_time, pause_time
Пример #5
0
# a = vic_method.replace_special_value('${t|2000-10-1,,,,date}$')
# print(a)
# a = vic_method.replace_special_value('${t|2020-02-29 11:12:13.1123,,year,-1,full}$')
# print(a)

# from py_test.vic_tools.vic_eval import EvalObject
#
# eo = EvalObject('admin', variable_dict={'x': 1})
#
# print(eo.get_eval_result())

from py_test.vic_tools.vic_find_object import find_with_condition, FindObject
from py_test.general import vic_log
import json
import logging
logger = logging.getLogger('debug')
logger.setLevel(10)
sh = logging.StreamHandler()
sh.setLevel(10)
sh.setFormatter(vic_log.format_standard)
logger.addHandler(sh)

j1 = '#{json}#{"b": "#{json|l}#[1, 2, \\"#{json}#[4, 5, \\"#{json|l}#[5,5]\\"]\\"]"}'
j1 = '#{json}#{"b": "#{json|l}#[1, 2, \\"#{json}#[4, 5, \\\\"#{json|l}#[5,5]\\\\"]\\"]"}'
o2 = {"a": "a1", "b": [1, 2, ["abc", 4, 5, [5, 5]]], "c": {"1": 1, "2": 2}}
j2 = json.dumps(o2)

r = find_with_condition(j1, j2, logger=logger)
print(r)
Пример #6
0
def wait_for_text_present(vic_step, print_=True):
    pause_time = 0
    elements = list()
    fail_elements = list()
    index_ = vic_step.ui_index
    text = vic_step.ui_data
    _success = False
    _full_msg = ''
    msg = '没找到期望文字【{}】'.format(text)
    msg_format = '经过{:.1f}秒 - {}'
    vic_step.dr.implicitly_wait(0)
    last_print_time = start_time = time.time()
    while (time.time() - start_time -
           pause_time) <= vic_step.timeout and not vic_step.force_stop:
        elements.clear()
        fail_elements.clear()
        if not vic_step.ui_by and not vic_step.ui_locator:
            _body = vic_step.dr.find_element_by_tag_name('body')
            find_result = vic_find_object.find_with_condition(
                text, _body.text, logger=vic_step.logger)
            if find_result.is_matched:
                elements_temp = vic_step.dr.find_elements_by_xpath(
                    '//body//*[contains(text(),"' + text + '")]')
                for element in elements_temp:
                    if element.is_displayed():
                        elements.append(element)
                if not elements:  # 如果文本不在元素的第一个子节点,用text()可能会找不到
                    elements.append(_body)
                msg = '在页面找到期望文字【{}】'.format(text)
                _success = True
            elif find_result.error_msg:
                msg = '没找到期望文字【{}】\n在查找过程中出现错误:{}'.format(
                    text, find_result.error_msg)
        else:
            _ui_data = vic_step.ui_data
            vic_step.ui_data = None  # 把ui_data置空,防止查找元素时被当做数量限制表达式
            _start_time = time.time()
            _pause_time = 0
            run_result_temp = ['f', 'N/A']
            elements_temp = None
            try:
                run_result_temp, elements_temp, elements_all, _, _pause_time = wait_for_element_visible(
                    vic_step, timeout=1, print_=False)
            except:
                _elapsed_time = time.time() - _start_time
                _pause_time = 0 if _elapsed_time < 1.5 else _elapsed_time - 1.5
            finally:
                pause_time += _pause_time
                vic_step.ui_data = _ui_data
            if run_result_temp[0] == 'f':
                msg = '未找到期望元素【By:{}|Locator:{}】或元素不可见'.format(
                    vic_step.ui_by, vic_step.ui_locator)
            else:
                _error_msg = None
                if index_ is None:
                    for element_temp in elements_temp:
                        # element_text = vic_step.dr.execute_script(
                        #     'return arguments[0].textContent||arguments[0].innerText||arguments[0].value',
                        #     element_temp)
                        element_text = element_temp.get_attribute(
                            'value') or element_temp.text
                        find_result = vic_find_object.find_with_condition(
                            text, element_text, logger=vic_step.logger)
                        if find_result.is_matched:
                            elements.append(element_temp)
                        else:
                            fail_elements.append(element_temp)
                            if find_result.error_msg:
                                if _error_msg:
                                    _error_msg = '{};{}'.format(
                                        _error_msg, find_result.error_msg)
                                else:
                                    _error_msg = find_result.error_msg
                elif index_ > (len(elements_temp) - 1):
                    raise ValueError('找到{}个元素,但指定的索引【{}】超出可用范围(0到{})'.format(
                        len(elements), index_,
                        len(elements) - 1))
                else:
                    element_text = vic_step.dr.execute_script(
                        'return arguments[0].textContent||arguments[0].innerText||arguments[0].value',
                        elements_temp[index_])
                    find_result = vic_find_object.find_with_condition(
                        text, element_text, logger=vic_step.logger)
                    if find_result.is_matched:
                        elements.append(elements_temp[index_])
                    else:
                        fail_elements.append(elements_temp[index_])
                        if find_result.error_msg:
                            _error_msg = find_result.error_msg
                msg = '找到期望元素【By:{}|Locator:{}】{}个,其中有{}个元素包含期望文字【{}】'.format(
                    vic_step.ui_by, vic_step.ui_locator, len(elements_temp),
                    len(elements), text)
                if _error_msg:
                    msg = '{}\n在查找过程中出现错误:{}'.format(msg, _error_msg)
            if elements and not fail_elements:
                _success = True

        if time.time() - last_print_time >= 1:
            pause_time += vic_step.pause_()
            elapsed_time = time.time() - start_time - pause_time
            _full_msg = msg_format.format(elapsed_time, msg)
            if print_:
                vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str,
                                                       _full_msg))
            last_print_time = time.time()

        if _success:
            break

    elapsed_time = time.time() - start_time - pause_time
    full_msg = msg_format.format(elapsed_time, msg)
    if print_ and full_msg != _full_msg:
        vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str, full_msg))
    if _success:
        run_result = ['p', full_msg]
    else:
        run_result = ['f', full_msg]
    vic_step.dr.implicitly_wait(vic_step.timeout)
    return run_result, elements, fail_elements, elapsed_time, pause_time