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

淩亂°似流年 2024-03-26 13:19 132阅读 0赞

在这里插入图片描述


文章目录

  • 一、场景
  • 二、实现代码异常的时候,实现截图和打印page_source
    • 1、特别注意1:
    • 2、特别注意2:
  • 三、代码优化
    • 1、思路
    • 2、特别注意

一、场景

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)

  1. import sys
  2. import time
  3. import allure
  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. class TestBaidu:
  8. def setup_class(self):
  9. self.driver=webdriver.Chrome()
  10. self.driver.implicitly_wait(2)
  11. def teardown_class(self):
  12. self.driver.quit()
  13. def test_baidu(self):
  14. self.driver.get('https://www.baidu.com')
  15. try:
  16. self.driver.find_element(By.ID, 'su1')
  17. except Exception:
  18. #时间戳
  19. time_stamp=int(time.time())
  20. # 注意:一定要创建好images路径、source_path路径
  21. image_path=f'./images/image_{
  22. time_stamp}.PNG'
  23. page_source_path=f'./page_source/page_source_{
  24. time_stamp}.html'
  25. #保存截图
  26. self.driver.save_screenshot(image_path)
  27. #保存获取到的页面源码
  28. with open(page_source_path,'w',encoding='utf-8') as f:
  29. f.write(self.driver.page_source)
  30. #将截图添加到allure报告中
  31. allure.attach.file(image_path,
  32. name='image',
  33. attachment_type=allure.attachment_type.PNG)
  34. #将页面源码添加到allure报告中
  35. allure.attach.file(page_source_path,
  36. name='text',
  37. attachment_type=allure.attachment_type.TEXT)
  38. #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
  39. raise Exception

三、代码优化

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

1、思路

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

2、特别注意

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

  1. import sys
  2. import time
  3. import allure
  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. from selenium.webdriver.common.keys import Keys
  8. #采用装饰器
  9. def ui_exception_record(func):
  10. def inner(*args,**kwargs):
  11. try:
  12. return func(*args, **kwargs)
  13. except Exception:
  14. driver = args[0].driver
  15. #时间戳
  16. time_stamp=int(time.time())
  17. # 注意:一定要创建好images路径、source_path路径
  18. image_path=f'./images/image_{
  19. time_stamp}.PNG'
  20. page_source_path=f'./page_source/page_source_{
  21. time_stamp}.html'
  22. #保存截图
  23. driver.save_screenshot(image_path)
  24. #保存获取到的页面源码
  25. with open(page_source_path,'w',encoding='utf-8') as f:
  26. f.write(driver.page_source)
  27. #将截图添加到allure报告中
  28. allure.attach.file(image_path,
  29. name='image',
  30. attachment_type=allure.attachment_type.PNG)
  31. #将页面源码添加到allure报告中
  32. allure.attach.file(page_source_path,
  33. name='text',
  34. attachment_type=allure.attachment_type.TEXT)
  35. #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
  36. raise Exception
  37. return inner
  38. class TestBaidu2:
  39. def setup_class(self):
  40. self.driver=webdriver.Chrome()
  41. self.driver.implicitly_wait(2)
  42. def teardown_class(self):
  43. self.driver.quit()
  44. @ui_exception_record
  45. def test_baidu(self):
  46. self.driver.get('https://www.baidu.com')
  47. self.driver.find_element(By.ID, 'su1')

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

  1. import sys
  2. import time
  3. import allure
  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. from selenium.webdriver.common.keys import Keys
  8. #采用装饰器
  9. def ui_exception_record(func):
  10. def inner(*args,**kwargs):
  11. driver = args[0].driver
  12. try:
  13. return func(*args, **kwargs)
  14. except Exception:
  15. #driver = args[0].driver
  16. #时间戳
  17. time_stamp=int(time.time())
  18. # 注意:一定要创建好images路径、source_path路径
  19. image_path=f'./images/image_{
  20. time_stamp}.PNG'
  21. page_source_path=f'./page_source/page_source_{
  22. time_stamp}.html'
  23. #保存截图
  24. driver.save_screenshot(image_path)
  25. #保存获取到的页面源码
  26. with open(page_source_path,'w',encoding='utf-8') as f:
  27. f.write(driver.page_source)
  28. #将截图添加到allure报告中
  29. allure.attach.file(image_path,
  30. name='image',
  31. attachment_type=allure.attachment_type.PNG)
  32. #将页面源码添加到allure报告中
  33. allure.attach.file(page_source_path,
  34. name='text',
  35. attachment_type=allure.attachment_type.TEXT)
  36. #如果用例失败,抛出异常;否则即使捕获到异常,用例也会通过
  37. raise Exception
  38. return inner
  39. class TestBaidu2:
  40. def setup_class(self):
  41. self.driver=webdriver.Chrome()
  42. self.driver.implicitly_wait(2)
  43. def teardown_class(self):
  44. self.driver.quit()
  45. @ui_exception_record
  46. def test_baidu(self):
  47. self.driver.get('https://www.baidu.com')
  48. self.driver.find_element(By.ID, 'su1')

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

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

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读