如何设置网站根目录,网站开发岗位说明书,wordpress安装主题之后首页不变,网站推广app自动化测试过程中#xff0c;是否遇到过脚本执行中途出错却不知道原因的情况#xff1f;测试人员面临的不仅是问题的复现#xff0c;还有对错误的快速定位和分析。而异常截图与页面源码#xff08;Page Source#xff09;的结合#xff0c;正是解决这一难题的利器。
在实…自动化测试过程中是否遇到过脚本执行中途出错却不知道原因的情况测试人员面临的不仅是问题的复现还有对错误的快速定位和分析。而异常截图与页面源码Page Source的结合正是解决这一难题的利器。
在实际的自动化测试中您是否清楚异常发生时的具体页面状态单凭日志信息往往无法全面还原错误背景。通过自动化实现异常截图和保存页面源码的功能您可以大幅提升定位问题的效率和准确性。
在UI自动化测试行业随着业务复杂性增加定位问题的效率和深度成为测试团队竞争力的核心。以异常截图和页面源码为代表的增强调试工具已成为优秀测试框架的标配。例如 Allure 的报告集成截图功能TestNG 的详细异常日志都表明这种趋势在逐步标准化。 为什么需要异常截图和Page Source
截图直观定位问题UI界面异常、元素定位失败等问题可通过截图直接查看。源码辅助排查页面源码保存可帮助分析DOM结构变化、动态加载等问题。联动使用效果更佳截图展示表面源码剖析细节二者结合为测试提供全方位视角。 实际案例分享
例如使用 Selenium 进行电商网站的自动化测试时在支付页面的验证中偶尔会出现按钮未加载的问题。通过启用异常捕获机制截图显示支付按钮缺失而源码内容揭示了JS脚本未执行的原因最终确认问题出在接口超时。 01 场景 增加自动化测试代码的可测性 丰富报告 02 实现代码异常时 实现代码异常的时候实现截图和打印page_source 实现方法try except 配合截图和page_source操作 特别注意1 在保存截图和页面源码时一定先创建好images、source_path路径 保存截图driver.save_screenshot(路径名称) 获取页面源码driver.page_source() 异常处理会影响用例本身的结果 解决办法在except之后再把异常抛出 代码最后加上raise Exception 如果用例失败抛出异常否则即使捕获到异常用例也会通过 特别注意2 将截图保存到allure报告中
allure.attach.file(截图路径,name‘image’,attachment_typeallure.attachment_type.PNG) 将页面源码保存到allure中,以文本的形式存储
allure.attach.file(源码路径,name‘text’,attachment_typeallure.attachment_type.TEXT) import sys
import timeimport allure
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChainsclass 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}.PNGpage_source_path f./page_source/page_source_{time_stamp}.html# 保存截图self.driver.save_screenshot(image_path)# 保存获取到的页面源码with open(page_source_path, w, encodingutf-8) as f:f.write(self.driver.page_source)# 将截图添加到allure报告中allure.attach.file(image_path,nameimage,attachment_typeallure.attachment_type.PNG)# 将页面源码添加到allure报告中allure.attach.file(page_source_path,nametext,attachment_typeallure.attachment_type.TEXT)# 如果用例失败抛出异常否则即使捕获到异常用例也会通过raise Exception 03 代码优化 异常捕获处理代码是公共方法和业务代码无关不能耦合。
解决办法使用装饰器装饰用例或者相关方法。 思路 先把装饰器架子搭建好 把相关逻辑嵌套进来 特别注意
使用装饰器执行用例被装饰函数还没有执行所以还没有self.driver获取被装饰方法的self也就是实例对象通过self就可以拿到声明的实例变量driver
driver args[0].driver 前提条件被装饰的方法是一个实例方法实例需要有实例变量self.driver 解决方法1获取driver放到函数执行之后
import sys
import timeimport 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}.PNGpage_source_path f./page_source/page_source_{time_stamp}.html# 保存截图driver.save_screenshot(image_path)# 保存获取到的页面源码with open(page_source_path, w, encodingutf-8) as f:f.write(driver.page_source)# 将截图添加到allure报告中allure.attach.file(image_path,nameimage,attachment_typeallure.attachment_type.PNG)# 将页面源码添加到allure报告中allure.attach.file(page_source_path,nametext,attachment_typeallure.attachment_type.TEXT)# 如果用例失败抛出异常否则即使捕获到异常用例也会通过raise Exceptionreturn 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_recorddef test_baidu(self):self.driver.get(https://www.baidu.com)self.driver.find_element(By.ID, su1) 解决方法2保证使用装饰器的时候driver已经声明driver args[0].driver
import sys
import timeimport 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].drivertry:return func(*args, **kwargs)except Exception:# 时间戳time_stamp int(time.time())# 注意一定要创建好images路径、source_path路径image_path f./images/image_{time_stamp}.PNGpage_source_path f./page_source/page_source_{time_stamp}.html# 保存截图driver.save_screenshot(image_path)# 保存获取到的页面源码with open(page_source_path, w, encodingutf-8) as f:f.write(driver.page_source)# 将截图添加到allure报告中allure.attach.file(image_path,nameimage,attachment_typeallure.attachment_type.PNG)# 将页面源码添加到allure报告中allure.attach.file(page_source_path,nametext,attachment_typeallure.attachment_type.TEXT)# 如果用例失败抛出异常否则即使捕获到异常用例也会通过raise Exceptionreturn inner
return innerclass TestBaidu2:def setup_class(self):self.driver webdriver.Chrome()self.driver.implicitly_wait(2)def teardown_class(self):self.driver.quit()ui_exception_recorddef test_baidu(self):self.driver.get(https://www.baidu.com)self.driver.find_element(By.ID, su1) 一旦被装饰的方法有返回值会丢失返回值 解决方案return func(*args, **kwargs) 当用例执行失败allure报告中可以查看截图 当用例执行失败allure报告中可以查看page_source源码 异常截图和Page Source的结合不仅帮助测试人员快速还原问题背景更为问题定位提供了关键依据。这种看似简单的操作却是提升测试效率的关键一步。 测试的目标从来不是证明程序正确而是帮助发现问题。学会利用工具还原问题真相才是真正的测试之道