您可以将测试用户配置/属性存储在资源文件(例如 test_properties.txt)中,如下所示:
=== 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} | admin@app.com |
| ${tp_admin_password} | admin@123 |
| ${tp_regular_user} | regular@app.com |
| ${tp_regular_password} | regular@123 |
在这里,您想用来测试您的应用程序的用户角色/类型定义为:
| ###### user roles to test with - admin, regular |
| ${tp_user_type} | admin |
然后您的测试套件文件可以导入以上资源文件,如下所示:
=== 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} |
与验证页面控件相关的代码可能位于关键字Page Controls Should be Visible 中,它将根据用户类型参数执行检查。
注意:测试用户的用户 ID 和密码变量在这里通过将用户类型变量嵌入为:${tp_${user_type}_user} 在我们的例子中被评估为${tp_admin_user} 来形成。
在执行过程中,你可以在命令行传递${tp_user_type}的值,它会覆盖资源文件中设置的值。
pybot --variable tp_user_type:non-admin path/to/your/testfile
如果您想对多个用户类型运行相同的测试,您可以创建一个批处理文件,如:
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
我相信会有比这更好的解决方案来解决您的问题。理想情况下,上面在测试套件文件中定义的关键字应该驻留在资源文件中。您还可以创建一个data-driven test 来为每个用户类型运行您的模板关键字(用于验证页面控件)。