Passing values from keyword to SHOULD BE EQUAL

710 views
Skip to first unread message

ant...@gmail.com

unread,
Jan 13, 2016, 9:51:28 AM1/13/16
to robotframework-users
Hello fellow rf users!

I have a question for which (I hope!) you know the anwser.

Scenario:
I have a form, on which there are several fields. Each field is filled out by user. After clicking 'accept' all fields are stored in database. I need to check if values from form are exactly the same as the ones stored in database.

Steps I'm taking:
1. Fill the form.
2. Save all field values to variables.
3. Click 'accept'
4. Query database for corresponding columns.
5. Run keyword : should be equal for each corresponding pair.

It's pretty simple scenario. In reality it's more complicated but I'll just leave it like that.

What I did: I created keyword that did point 2 (save all field values to variables) and as an return value I passed all those values. I did the same in step 4 - I queried database, saved all column values I needed into variables and then passed them in return value from keyword.


For example, my test cases looks like this:
test1
    $
{fieldsValues}    MyFields
    $
{columnValues}    MyQueries
    SHOULD BE EQUAL    $
{fieldsValues[0]}    ${columnValues[0][0]}
....

    SHOULD BE EQUAL    $
{fieldsValues[10]}    ${columnValues[0][10]}



My keywords file:
*** Keywords ***
MyQueries
    $
{id}    query    select 10
....
    $
{row}    query    select 20
   
[Return]    ${id} .... MANY VARIABLES....   ${row}


MyFields
    $
{fieldOneValue}    GetValueFromField    ${windowName}    ${fieldOneName}
.....
    $
{fieldTenValue}    GetValueFromField    ${windowName}    ${fieldTenName}
   
[Return]    ${fieldOneValue}  ..MANY VARIABLES...  ${fieldTenValue}

Is there a better way to easily access variables passed from keywords? Maybe some kind of object notation (like : ${fieldsValues.filedOneValue}) ? Getting through notation [0][1] is really tiring and I'm prone to make mistake somewhere (I have over 30 fields on my form). And I couldn't find any other solution myself.

Any help will be much apprecieated!


Kevin O.

unread,
Jan 13, 2016, 1:10:02 PM1/13/16
to robotframework-users
First, I feel your pain. I have had to build keywords that deal with queries with many columns and it gets dizzying.

Depending on the DB driver, you may be able to reference column values by name

Should Be Equal ${columnValues[0]['StreetAddress']} ${expected street address}

This has worked for me using MSSQL via adodbapi(pywin32).
Not so for MySQL or Oracle :(

You can return a dictionary from MyQueries keyword to make things easier. Here's an example I found:

${result}    Query    Select ch.CUSTOMER_ID,ch.PHONE_NUMBER...
${business customer}    Create Dictionary
Set To Dictionary ${business customer}    customer id    ${result[0][0]}
Set To Dictionary ${business customer}    phone number    ${result[0][1]}
...
[Return]    ${business customer}

If you also build a dictionary in your MyFields keyword with the same keys, comparing them could be as easy as:
Collections.Dictionaries Should Be Equal    ${fieldsValues}    ${columnValues}

If your query is simple like getting many columns from one table (likely not the case), you could do it all based on a list...
${columns}    Create List    COLA_NAME    COLB_NAME   etc.
${column names}    Catenate    SEPARATOR=,${SPACE}    @{columns}    
${rs}    Query    SELECT ${column names} FROM MY_TABLE fit WHERE ID = 5  
${results}    Create Dictionary            
${column count}    Get Length    ${columns}        
:FOR    ${index}    IN RANGE    ${column count}    
    Set To Dictionary    ${results}    ${columns[${index}]}    ${rs[0][${index}]}

Kevin

m m

unread,
Jan 14, 2016, 1:40:42 AM1/14/16
to korm...@gmail.com, robotframework-users
Ugh

When formatting results from database, Oracle for me, the Python zip[1] has been quite useful. Also I tend to format data as list and then use List Should Be Equal keyword.

-Tatu

[1] https://docs.python.org/2/library/functions.html#zip

From: Kevin O.
Sent: ‎13/‎01/‎2016 20:10
To: robotframework-users
Subject: Re: Passing values from keyword to SHOULD BE EQUAL

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Pekka Klärck

unread,
Jan 15, 2016, 5:17:29 AM1/15/16
to ant...@gmail.com, robotframework-users
2016-01-13 16:51 GMT+02:00 <ant...@gmail.com>:
> Is there a better way to easily access variables passed from keywords? Maybe
> some kind of object notation (like : ${fieldsValues.filedOneValue}) ?
> Getting through notation [0][1] is really tiring and I'm prone to make
> mistake somewhere (I have over 30 fields on my form). And I couldn't find
> any other solution myself.

Others already provided good tips to mitigate the problem. If you want
to make data really easily accessible, you can create a custom object
that has data as attributes that you can access like
`${object.attribute}`. If possible, such objects should be returned
directly be the library you use, but you can also create a custom
library that just wraps the data you got elsewhere to custom objects.

If the data you got is already in a dictionary, you can also assign it
to a dictionary variable (new in RF 2.9) to convert it into a
dot-accessible dictionary:

&{result} = Keyword
Log ${result.key}

For more information see:
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-dictionary-variables
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#return-values-from-keywords

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

ant...@gmail.com

unread,
Jan 15, 2016, 7:32:37 AM1/15/16
to robotframework-users
I'd like to thank all of you for your valuable input! You helped me a lot! Cheers guys!

Thank you guys! :)
Reply all
Reply to author
Forward
0 new messages