Robot Framework - Using Resource Files

Liviu P picture Liviu P · Jul 4, 2016 · Viewed 8k times · Source

I have a test file which checks for the presence of all key elements on every page of the app (one Scenario per page). However, the app is fairly complex and has different types of users (admin, regular, etc.) and I want to be able to go through the same pages.robot file with every type of user (and maybe have some if statements in that pages.robot file for every type of user) but I'm not sure how I should do it. I'm guessing I should be using a Resource File and set a global userType variable with admin, regular, etc. and run the pages.robot file multiple times (once per user type) but I'm not sure how to set up the Resource File and the userType variable.

Any ideas on how the Resource File should look like and then how to run the same file for every type of user?

Answer

Mukesh Takhtani picture Mukesh Takhtani · Jul 6, 2016

You can store your test user configuration/properties in a resource file (say test_properties.txt) as below:

=== test_properties.txt ===

| *** Variables *** |
| ${tp_app_url}  | http://yourapp.com |
| ${tp_browser}  | firefox |
| ###### user roles to test with - admin, non-admin, regular |
| ${tp_user_type} | admin |
| ###### test users   |
| ${tp_admin_user} | [email protected] |
| ${tp_admin_password} | admin@123 |
| ${tp_regular_user} | [email protected] |
| ${tp_regular_password} | regular@123 |

Here, the user role/type with which you want to test your application is defined as:

| ###### user roles to test with - admin, regular |
| ${tp_user_type} | admin |

Your test suite file can then import above resource file as below:

=== testsuite.txt ===

| *** settings *** |
| Library        | Selenium2Library |
| Resource       | test_properties.txt |

| *** test cases *** |
| Validate Page Controls |
|    | Open Browser To Login Page | ${tp_user_type} |
|    | Page Controls Should be Visible | ${tp_user_type} |

| *** keywords *** |
| Open Browser To Login Page |
|    | [Arguments] | ${user_type} |
|    | Open Browser | ${tp_app_url} | ${tp_browser} |
|    | Input Username | ${tp_${user_type}_user} |
|    | Input Password | ${tp_${user_type}_password} |
|    | Submit Credentials |
|    | Title Should Be | Welcome Page |

| Input Username |
|    | [Arguments] | ${username} |
|    | Input Text | username_field | ${username} |

| Input Password |
|    | [Arguments] | ${password} |
|    | Input Text | password_field | ${password} |

| Submit Credentials |
|    | Click Button | login_button |

| Page Controls Should be Visible |
|    | [Arguments] | ${user_type} |

Your code related to validating the page controls could reside in the keyword Page Controls Should be Visible which will perform the checks based on the user type argument.

Note: Test user's userId and password variables are formed here by embedding the user type variable as: ${tp_${user_type}_user} which in turn gets evaluated as ${tp_admin_user} in our case.

During execution, you can pass the value of ${tp_user_type} on command line, and it override the value set in the resource file.

pybot --variable tp_user_type:non-admin path/to/your/testfile

If you want to run the same tests with multiple user types, you can create a batch file like:

pybot --variable tp_user_type:non-admin path/to/your/testfile
pybot --variable tp_user_type:admin path/to/your/testfile
pybot --variable tp_user_type:regular path/to/your/testfile

I'm sure there will be a better solution than this for your problem. Ideally, the keywords defined above in the test suite file should reside in a resource file. You can also create a data-driven test to run your template keyword (which validates page controls) for each user type.