• 关键数据记录的作用
内容 作用
执行日志

1. 记录代码的执行记录,方便复现场景

2. 可以作为bug依据

截图

1. 断言失败或成功截图

2. 异常截图达到丰富报告的作用

3. 可以作为bug依据

page source 协助排查报错时元素当时是否存在页面上

执行日志

  • 日志配置
  • 脚本日志级别
    • debug记录步骤信息
    • info记录关键信息,比如断言等
 1 # 日志配置
 2 import logging
 3 # 创建logger实例
 4 logger = logging.getLogger('simple_example')
 5 # 设置日志级别
 6 logger.setLevel(logging.DEBUG)
 7 # 流处理器
 8 ch = logging.StreamHandler()
 9 ch.setLevel(logging.DEBUG)
10 # 日志打印格式
11 formatter = logging.Formatter\
12 ('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
13 # 添加格式配置
14 ch.setFormatter(formatter)
15 # 添加日志配置
16 logger.addHandler(ch)
日志配置
 1 # 日志与脚本结合
 2 class TestDataRecord:
 3     def setup_class(self):
 4         self.driver = webdriver.Chrome()
 5         self.driver.implicitly_wait(5)
 6     
 7     def teardown_class(self):
 8         self.driver.quit()
 9 
10     def test_log_data_record(self):
11         # 实例化self.driver
12         search_content = "测试开发"
13         # 打开搜狗首页
14         self.driver.get("https://www.sogou.com/")
15         logger.debug("打开搜狗首页")
16         # 输入测试开发
17         self.driver.find_element(By.CSS_SELECTOR, "#query").\
18         send_keys(search_content)
19         # 步骤级别是使用 debug
20         logger.debug(f"搜索的内容为{search_content}")
21         # 点击搜索
22         self.driver.find_element(By.CSS_SELECTOR, "#stb").click()
23         # 搜索结果
24         search_res = self.driver.find_element(By.CSS_SELECTOR, "em")
25         logger.info(f"搜索结果为{search_res.text},预期结果为{search_content}")
26         assert search_res.text == search_content    
日志与脚本结合

执行的截图

  • save_screenshot(截图路径+名称)
  • 记录关键页面
    • 断言页面
    • 重要的业务场景页面
    • 容易出错的页面
1 # 调用save方法截图并保存保存在当前路径下的images文件夹下
2 driver.save_screenshot('./images/search00.png')
调用save方法截图

page source(页面源代码)

  • 在调试过程中,如果有找不到元素的错误可以保存当时的page_source调试代码

最好是保存在文件里,更好查看

# 在报错行前面添加保存page_source的操作
with open("record.html", "w", encoding="u8") as f:
    f.write(self.driver.page_source)
保存page_source

封装异常数据记录

 1 import os.path
 2 import time
 3 import allure
 4 from selenium.webdriver.support import expected_conditions
 5 
 6 from utils.log_utils import logger
 7 
 8 def ui_exception_record(func):
 9     def inner(*args, **kwargs):
10         driver = args[0].driver
11         try:
12             # 被装饰的函数执行过程中,如果出现异常就捕获,并且完成数据记录操作
13             return func(*args, **kwargs)
14         except Exception:
15             # 如果查找元素时抛出异常,则截图/日志/pagesource
16             # 截图记录,双重保障
17             logger.warning("查找元素出现异常")
18             timestamp = int(time.time())
19             # 注意:!! 一定要提前创建好images 路径
20             image_path = f"./data/image_data/image_{timestamp}.PNG"
21             page_source_path = f"./data/page_source_data/page_source_{timestamp}.html"
22             driver.save_screenshot(image_path)
23             with open(page_source_path, "w", encoding="u8") as f:
24                 f.write(driver.page_source)
25                 # 涉及报告信息,所以使用命令行执行才能看到报告效果
26             allure.attach.file(image_path, name="picture",
27                                attachment_type=allure.attachment_type.PNG)
28             # allure.attach.file(page_source_path, name="page_source",
29             #                    attachment_type=allure.attachment_type.HTML)
30             # 如果想看的是转换页面前的html 源码,那么需要使用text 格式
31             allure.attach.file(page_source_path, name="page_source",
32                                attachment_type=allure.attachment_type.TEXT)
33             raise Exception
34     return inner
封装异常数据记录

 

笔记2023-04-15

posted on 2023-04-16 23:34  三天乐趣  阅读(33)  评论(0编辑  收藏  举报