自动化测试——异常截图和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放到函数执行之后

扫描二维码关注公众号,回复: 14648466 查看本文章
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源码
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YZL40514131/article/details/129192733