Search code examples
pythonpython-decoratorspython-behave

How to add wait/sleep decorator to steps functions (Behave)?


Using Python/Selenium/Behave:

I'm wondering how to correctly add wait/sleep decorators to steps functions?

I have setup a helper.py with my decorator function:

import time

def wait(secs):
    def decorator(func):
        def wrapper(*args, **kwargs):
            ret = func(*args, **kwargs)
            time.sleep(secs)
            return ret
        return wrapper
    return decorator

class Helper(object):
    pass

In my steps file, I'm calling the wait decorator after the behave decorator to match the step:

from behave import step
from features.helper import wait

@step('I observe a login error')
@wait(3)
def step_impl(context):
    #time.sleep(3)
    assert context.login_page.check_login_error() is not None

But when I execute the step from my feature file, there is no wait/sleep being executed, and the assert fails. How can I execute the wait decorator in this case?


Solution

  • I guess the problem is due to call the target function before sleep. So you can just swap func and time.sleep calling:

    def wait(secs):
         def decorator(func):
             def wrapper(*args, **kwargs):
                 time.sleep(secs)
                 return func(*args, **kwargs)
             return wrapper
         return decorator