Web自动化测试【分层架构设计与实现】五:异常处理层

一、异常处理主要是用来统一处理测试过程中的各种异常;【没有详细研究……主要还是就装饰器的运用……】

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import traceback
from selenium.common.exceptions import NoSuchElementException
from Result.Result import Result
from Result.DBResult import DBResult
result = Result()
dbResult = DBResult()
def element_not_found_exception(func):
    def not_found(self, locator):
        try:
            return func(self, locator)
        except NoSuchElementException:
            msg = 'element not found for locator:'+locator+'At method:'+func.__name__
            dbResult.log_error(self, msg)
            result.log_error(msg)
            return None
    return not_found

def assert_logger(func):
    def assertlogger(self):
        try:
            msg = 'Test Case is:'+func.__name__
            dbResult.log_info(self, msg)
            result.log_info(msg)
            return func(self)
        except AssertionError as ex:
            msg = 'AssertError in: %s.%s' % (self.__class__.__name__, func.__name__)
            dbResult.log_error(self, msg)
            dbResult.log_fail(self)
            result.log_error(msg)
            result.log_fail()
    assertlogger.__name__=func.__name__
    return assertlogger

def exception_logger(func):
    def exceptionlogger(self):
        try:
            return func(self)
        except Exception as ex:
            # print sys.exc_info()
            if True: ##debug is Ttrue?
                print(traceback.print_exc())  ##打印完整堆栈用于定位代码行
            msg = "%s:%s At method:%s" % (Exception, ex, func.__name__)
            result.log_error(msg)
            dbResult.log_error(self, msg)
    return exceptionlogger

def name_logger(func):
    def namelogger(self):
        self.__class__.test_result_id = dbResult.log_init('', self.__class__.__name__, func.__name__) ##设置test_result_id
        result.log_info('Test Class is:'+self.__class__.__name__)
        return func(self)
    namelogger.__name__=func.__name__
    return namelogger
#自定义
class NOTESTDATAERROR(Exception):
    def __init__(self, module, test_case):
        self.value = '%s: Module: %s, TestCase: %s' % (self.__class__.__name__, module, test_case)
    def __str__(self):
        return repr(self.value)

class NOLOCATORERROR(Exception):
    def __init__(self, module, test_case, name):
        self.value = '%s: Module: %s, TestCase: %s' % (self.__class__.__name__, module, test_case)
    def __str__(self):
        return repr(self.value)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest

from selenium import webdriver
from Errors.ExceptionWarpper import *
from Data.DataPool import DBDataPool
from Pages.BaiduHome import BaiduHome
from Result.Result import Result
class TestDemo(unittest.TestCase):
    @exception_logger
    @name_logger
    def setUp(self):
        self.wd = webdriver.Chrome()
        self.dp = DBDataPool(self.__class__.__name__)
        self.wd.get(self.dp.get('BAIDU_HOME_URL'))
        self.page = BaiduHome(self.wd)
        self.result = Result()

    @exception_logger
    @assert_logger
    def test_sample(self):
        self.page.key_worlds_input(self.dp.get('SELENIUM'))
        self.page.search_click()
        self.result.log_info('The search button is clicked')
        assert True
        self.result.log_pass()

    @exception_logger
    def tearDown(self):
        self.wd.close()

if __name__=='__main__':
    unittest.main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷凝娇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值