"""Environment file for behave tests."""importfunctoolsimporttracebackfromcollections.abcimportCallablefrombehaveimportgiven,runner,step,then,when#from selenium import webdriver# Function to wrap steps and enforce failure on any exception
[docs]deffail_on_exception(func:Callable)->Callable:"""Wrapper that ensures uncaught exceptions fail the scenario and are logged to the HTML report."""@functools.wraps(func)defwrapper(context:runner.Context,*args:tuple,**kwargs:dict)->Callable:try:returnfunc(context,*args,**kwargs)exceptAssertionError:raiseexceptExceptionase:traceback.print_exc()# Print full traceback for debuggingexc_msg=f'Step failed due to exception: {e}'raiseAssertionError(exc_msg)fromereturnwrapper
# def before_all(context):# """Initializes browser before all tests."""# # Initialize browser (e.g., Chrome, Firefox)# context.browser = webdriver.Chrome() # or webdriver.Firefox()# context.browser.implicitly_wait(10) # optional: wait for elements to load# def after_all(context):# """Quit the browser at the end of all tests."""# context.browser.quit()# Monkey-patch Behave's step decorators to automatically wrap all steps
[docs]defpatched_step(*args:tuple,**kwargs:dict)->Callable:"""Monkey-patched step decorator that wraps the step function to fail on any exception."""defdecorator(func:Callable)->Callable:returnoriginal_step(*args,**kwargs)(fail_on_exception(func))returndecorator
[docs]defpatched_given(*args:tuple,**kwargs:dict)->Callable:"""Monkey-patched given decorator that wraps the step function to fail on any exception."""defdecorator(func:Callable)->Callable:returnoriginal_given(*args,**kwargs)(fail_on_exception(func))returndecorator
[docs]defpatched_when(*args:tuple,**kwargs:dict)->Callable:"""Monkey-patched when decorator that wraps the step function to fail on any exception."""defdecorator(func:Callable)->Callable:returnoriginal_when(*args,**kwargs)(fail_on_exception(func))returndecorator
[docs]defpatched_then(*args:tuple,**kwargs:dict)->Callable:"""Monkey-patched then decorator that wraps the step function to fail on any exception."""defdecorator(func:Callable)->Callable:returnoriginal_then(*args,**kwargs)(fail_on_exception(func))returndecorator