自动化测试——异常截图和page_source

在这里插入图片描述


一、场景

1、增加自动化测试代码的可测性
2、丰富报告

二、实现代码异常的时候,实现截图和打印page_source

实现方法:try except 配合截图和page_source操作

1、特别注意1:

1、在保存截图和页面源码时,一定先创建好images、source_path路径
2、保存截图:driver.save_screenshot(路径名称)
3、获取页面源码:driver.page_source()
4、异常处理会影响用例本身的结果;解决办法:在except之后,再把异常抛出
代码最后加上:raise Exception;如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过

2、特别注意2:

将截图保存到allure报告中
allure.attach.file(截图路径,name=‘image’,attachment_type=allure.attachment_type.PNG)

将页面源码保存到allure中,以文本的形式存储
allure.attach.file(源码路径,name=‘text’,attachment_type=allure.attachment_type.TEXT)

import sys
import time

import allure
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


class TestBaidu:

    def setup_class(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(2)

    def teardown_class(self):
        self.driver.quit()

    def test_baidu(self):
        self.driver.get('https://www.baidu.com')
        try:
            self.driver.find_element(By.ID, 'su1')
        except Exception:
            #时间戳
            time_stamp=int(time.time())
            # 注意:一定要创建好images路径、source_path路径
            image_path=f'./images/image_{time_stamp}.PNG'
            page_source_path=f'./page_source/page_source_{time_stamp}.html'
            #保存截图
            self.driver.save_screenshot(image_path)
            #保存获取到的页面源码
            with open(page_source_path,'w',encoding='utf-8') as f:
                f.write(self.driver.page_source)

            #将截图添加到allure报告中
            allure.attach.file(image_path,
                               name='image',
                               attachment_type=allure.attachment_type.PNG)

            #将页面源码添加到allure报告中
            allure.attach.file(page_source_path,
                               name='text',
                               attachment_type=allure.attachment_type.TEXT)

            #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
            raise Exception

三、代码优化

异常捕获处理代码是公共方法和业务代码无关,不能耦合。
解决办法,使用装饰器装饰用例或者相关方法。

1、思路

a、先把装饰器架子搭建好
b、把相关逻辑嵌套进来

2、特别注意

使用装饰器执行用例,被装饰函数还没有执行,所以还没有self.driver
1、获取被装饰方法的self,也就是实例对象
通过self就可以拿到声明的实例变量driver
driver = args[0].driver
前提条件:被装饰的方法是一个实例方法,实例需要有实例变量self.driver
解决方法1:获取driver放到函数执行之后

import sys
import time

import allure
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

#采用装饰器

def ui_exception_record(func):

    def inner(*args,**kwargs):

        try:
            return func(*args, **kwargs)

        except Exception:
            driver = args[0].driver
            #时间戳
            time_stamp=int(time.time())
            # 注意:一定要创建好images路径、source_path路径
            image_path=f'./images/image_{time_stamp}.PNG'
            page_source_path=f'./page_source/page_source_{time_stamp}.html'
            #保存截图
            driver.save_screenshot(image_path)
            #保存获取到的页面源码
            with open(page_source_path,'w',encoding='utf-8') as f:
                f.write(driver.page_source)

            #将截图添加到allure报告中
            allure.attach.file(image_path,
                               name='image',
                               attachment_type=allure.attachment_type.PNG)

            #将页面源码添加到allure报告中
            allure.attach.file(page_source_path,
                               name='text',
                               attachment_type=allure.attachment_type.TEXT)

            #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
            raise Exception

    return inner


class TestBaidu2:

    def setup_class(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(2)

    def teardown_class(self):
        self.driver.quit()

    @ui_exception_record
    def test_baidu(self):
        self.driver.get('https://www.baidu.com')
        self.driver.find_element(By.ID, 'su1')

2、解决方法2:保证使用装饰器的时候,driver已经声明:driver = args[0].driver

import sys
import time

import allure
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

#采用装饰器

def ui_exception_record(func):

    def inner(*args,**kwargs):

        driver = args[0].driver

        try:
            return func(*args, **kwargs)

        except Exception:
            #driver = args[0].driver
            #时间戳
            time_stamp=int(time.time())
            # 注意:一定要创建好images路径、source_path路径
            image_path=f'./images/image_{time_stamp}.PNG'
            page_source_path=f'./page_source/page_source_{time_stamp}.html'
            #保存截图
            driver.save_screenshot(image_path)
            #保存获取到的页面源码
            with open(page_source_path,'w',encoding='utf-8') as f:
                f.write(driver.page_source)

            #将截图添加到allure报告中
            allure.attach.file(image_path,
                               name='image',
                               attachment_type=allure.attachment_type.PNG)

            #将页面源码添加到allure报告中
            allure.attach.file(page_source_path,
                               name='text',
                               attachment_type=allure.attachment_type.TEXT)

            #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
            raise Exception

    return inner


class TestBaidu2:

    def setup_class(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(2)

    def teardown_class(self):
        self.driver.quit()

    @ui_exception_record
    def test_baidu(self):
        self.driver.get('https://www.baidu.com')
        self.driver.find_element(By.ID, 'su1')

3、一旦被装饰的方法有返回值,会丢失返回值
解决方案:return func(*args, **kwargs)

当用例执行失败allure报告中可以查看截图
在这里插入图片描述
当用例执行失败allure报告中可以查看page_source源码
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敲代码敲到头发茂密

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

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

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

打赏作者

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

抵扣说明:

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

余额充值