gpt4 book ai didi

python - Python-Behave 中屏幕截图的自定义错误处理程序

转载 作者:行者123 更新时间:2023-12-01 03:19:00 31 4
gpt4 key购买 nike

我创建了一个自定义错误处理程序来处理失败时的屏幕截图

#error_handler.py

def screenshot_handler(func):
def func_wrapper(self):
try:
return func(self)
except Exception as e:
print("screenshot")
return func(self)
return func_wrapper
<小时/>
#page.py

@screenshot_handler
def assert_login(self,a):
self.find_element(*DashboardPageLocators.AUTOREFRESH_BUTTON)
return True
<小时/>
#steps.py
"""
i forced this to fail
"""

@then('map should display')
def step_impl(context):
page = LoginPage(context)
page.assert_login()
<小时/>

我想获取步骤参数“ map 应显示”,以便我从中创建文件名。最明显的选择是复制字符串,但这效率很低,是否有一个我可以调用的行为函数来处理这个问题

最佳答案

我在environments.py的after_step函数中执行此操作:

def after_step(context, step):
if step.status == 'failed':
step_str = step.name
# Take screen shot and upload to s3

您可以立即访问测试状态和步骤名称,并且不必包含自己的错误处理装饰器。

step 变量中有许多有趣的东西:

(Pdb) pp(dir(step))

['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'duration',
'error_message',
'exc_traceback',
'exception',
'filename',
'keyword',
'line',
'location',
'name',
'replay',
'reset',
'run',
'set_values',
'status',
'step_type',
'store_exception_context',
'table',
'text',
'type']

步骤字符串只是test.name

关于python - Python-Behave 中屏幕截图的自定义错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42162823/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com