2

I have written a python function for fetching database credentials for different environments

def database_creds(env):
    if env == 'staging' or env == 'qa':
        hostname = 'host1'
        username = 'user1'
        password = 'pass11'
        database = 'TestDb'
    elif env == 'production':
        hostname = 'host2'
        username = 'user2'
        password = 'pass22'
        database = 'ProdDb'
    return hostname, username, password, database

My doubt is how we can use each returned values in robot file?

If we are returning only one value from a python function

def getApiFullUrl(env):
    if env== 'production':
        url = 'production url'
    else:
        url = 'other environment url'
    return url

we can use like this in robot file:

${url}  ${getApiFullUrl('${env}')}
2
  • 1
    ${result1} ${result2} = ${database_creds('${env}')? Commented Apr 22, 2019 at 7:34
  • Your indentation for database_creds() is incorrect. Commented Apr 22, 2019 at 8:32

1 Answer 1

4

Either assign them to the same number of variables (that's "automatic unpacking"):

${hostname}   ${username}   ${password}   ${database}     database_creds    production

, or assign it to a single variable and treat it as a list:

${data}     database_creds    qa
Log    This is the hostname - ${data}[0], and this the database - ${data}[3]
Sign up to request clarification or add additional context in comments.

7 Comments

The output of ${data} [0] is ('TestDb', 'user1', 'pass11', 'host1')[0]. I think it is not considering it as a list. I need the output as database='TestDb', user='user1', password='pass11', host='host1'....How will I get the output like that
There is no space between ${data} and [0] - it must be ${data}[0], which translates to "get the value of the 1st element in the list data"
"I need the output as database='TestDb', user='user1', password='pass11', host='host1'" -> Log database= '${data}[3]', user= '${data}[1]', password='${data}[2]', host='${data}[0]', if it's only for output. Between "Log" and "database" there are at least 2 white spaces (preferably - 4).
I'm sorry I accidentally put that space in the comment.
Even if we are giving ${data}[0] or ${data}[1] or ${data}[2] or ${data}[3] i am getting the output as ('TestDb', 'user1', 'pass11', 'host1')[0] or ('TestDb', 'user1', 'pass11', 'host1')[1] or ('TestDb', 'user1', 'pass11', 'host1')[2] or ('TestDb', 'user1', 'pass11', 'host1')[3]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.