Richard C
Richard C

Reputation: 783

Calling a step within a step that has a parameter set

I know that I can use the

context.execute_steps('given I have logged in')

syntax to run a step within step

and if the step has a variable parameter within it I can hard code this in the step eg

@when('the user selects {value}')

becomes

context.execute_steps('when the user selects blue')

However is there a way of keeping the variable and passing that in from the step being run eg

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects {value}')

Upvotes: 0

Views: 969

Answers (2)

automationleg
automationleg

Reputation: 323

or as per documentation. Below example with f'strings.

@when(the user does a thing and then selects {value}) 
def step_impl(context, value):
    context.execute_steps(
        f'''
        given the user does a thing
        when the user selects "{value}"
        '''
    )

Upvotes: 0

Vikas Mulaje
Vikas Mulaje

Reputation: 765

Yes, you can simply use python string formatting.

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects {0}'.format(value))

or using old style python string formatting

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects %s'%value)

Upvotes: 1

Related Questions