How can I pass a variable from the Python-Behave step?

Basically I wrote a step called @When("I go to {url}")

Then I called it from the feature file using When I go to http://youtube.com and it worked

But I want to call it using When I go to YouTube

Same would happen with css selectors (For Then logo is visible looks prettier than Then div#id.class is visible)

How can I link a map file containing this css selectors and URL's as variables for my steps to use? Something like this:

YouTube = "http://youtube.com"
logo = "div#id.class"

I tried this

def before_all(context):
    global YouTube
    YouTube = "http://youtube.com"

And then I would eval(url) inside the step but it kept saying YouTube was not defined

1 answer

  • answered 2017-06-17 19:22 Klaus D.

    You should use a dictionary of predefined URLs instead of variables. Add this to your steps implementation file:

    websites = {'youtube': 'http://youtube.com', 'somesite': 'http://somesite.com'}
    
    @When("I go to {website}")
    def when_i_go_to_website(context, website):
        context.url = websites[website]
    

    context.url will be available in all followings steps.

    You might want to surround the code line with try / except to catch KeyErrors.