Java 黄瓜硒-填写网页表格
如果项目使用Cucumber和页面对象模型,用大量文本输入填充web表单的最佳方法是什么 例如,假设要素文件如下所示:Java 黄瓜硒-填写网页表格,java,selenium,automated-tests,cucumber,Java,Selenium,Automated Tests,Cucumber,如果项目使用Cucumber和页面对象模型,用大量文本输入填充web表单的最佳方法是什么 例如,假设要素文件如下所示: Scenario: As someone who wants to sign up When I visit the homepage And I click on the Register button And I enter my firstname And I enter my surname And I enter my emai
Scenario: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I enter my firstname
And I enter my surname
And I enter my email address
And I enter a new password
And I re-enter my new password
And I agree to the terms and conditions
And I click the Submit button
Then I should see a welcome page
Scenario Outline: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I enter my "<firstname>" in the firstname input
And I enter my "<surname>" in the surname input
And I enter my "<emailaddress>" in the email input
And I enter my "<password>" in the password input
And I enter my "<password>" in the password confirmation input
And I agree to the terms and conditions
And I click the Submit button
Then I expect the registration to "<ExpectedResult>"
Examples:
| firstname | surname | emailaddress | password | ExpectedResult |
| First | User | first@somewhere.com | | Fail |
| Second | User | second@somewhere.com | . | Fail |
| Third | User | third@somewhere.com | toofew | Fail |
| Fourth | User | fourth@somewhere.com | weakpassword | Fail |
| Fifth | User | fifth@somewhere.com | MissingNumber | Fail |
| Sixth | User | sixth@somewhere.com | m1ssingc4pital | Fail |
| seventh | User | seventh@somewhere.com | CapsAndNumb3r | Pass |
我知道Cucumber生成的step def将为每个步骤生成一个单独的方法,这很好。那么,我是否必须在“RegistrationPage”中以自己的方法实现这些步骤?我想了解的是:有没有一种方法可以实现一种“fillInform()”方法,而不是在RegistrationPage中实现单独的方法
编辑:
我问的问题可能是错的(我尽量简短)。我的目标是能够做到这一点:
Scenario: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I enter my firstname
And I enter my surname
And I enter my email address
And I enter a new password
And I re-enter my new password
And I agree to the terms and conditions
And I click the Submit button
Then I should see a welcome page
Scenario Outline: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I enter my "<firstname>" in the firstname input
And I enter my "<surname>" in the surname input
And I enter my "<emailaddress>" in the email input
And I enter my "<password>" in the password input
And I enter my "<password>" in the password confirmation input
And I agree to the terms and conditions
And I click the Submit button
Then I expect the registration to "<ExpectedResult>"
Examples:
| firstname | surname | emailaddress | password | ExpectedResult |
| First | User | first@somewhere.com | | Fail |
| Second | User | second@somewhere.com | . | Fail |
| Third | User | third@somewhere.com | toofew | Fail |
| Fourth | User | fourth@somewhere.com | weakpassword | Fail |
| Fifth | User | fifth@somewhere.com | MissingNumber | Fail |
| Sixth | User | sixth@somewhere.com | m1ssingc4pital | Fail |
| seventh | User | seventh@somewhere.com | CapsAndNumb3r | Pass |
然后,当测试按下“提交”按钮时:
然而,这样做对我来说并不“感觉正确”(尽管我可能弄错了-也许可以接受?1)功能文件
Scenario: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I fill the register form
And I agree to the terms and conditions
And I click the Submit button
Then I should see a welcome page
2) 步骤定义
Then("^I fill the register form$", () -> {
registrationPage.fillForm(user);
})
3) 注册页
public fillForm(User user) {
driver.findElement(xxx).sendKeys(user.firstName);
driver.findElement(xxx).sendKeys(user.surName);
...
}
有一个问题需要解决,如何传递参数<代码>用户<代码> <代码> fILFFAMEL()/代码>,你需要考虑你的代码框架,它是如何存储用户数据的,它是如何获取用户数据的。解决方案依赖于自编码框架。没有统一的解决方案
1) 此场景仅测试一个用户时的解决方案示例:Scenario: As someone who wants to sign up
When I visit the homepage
And I click on the Register button
And I fill the register form with user <userId>
And I agree to the terms and conditions
And I click the Submit button
Then I should see a welcome page
HashMap<String, User> users = xxxxx;
Then("^I fill the register form with user (.+)?$", (String userId) -> {
registrationPage.fillForm(users.get(userId));
})
场景:作为想要注册的人
当我访问主页时
我点击注册按钮
我在登记表上填上用户
我同意这些条款和条件
然后我点击提交按钮
然后我会看到欢迎页面
HashMap用户=xxxxx;
然后(“^I使用用户(+)?$”填写注册表,(字符串userId)->{
registrationPage.fillForm(users.get(userId));
})
2) 此场景需要测试多个用户时的解决方案示例:
Scenario Outlines: As someone who wants to sign up
When I visit the homepage
And I register user <userId>
Examples:
| userId |
| 001 |
| 002 |
| ... |
HashMap<String, User> users = xxxxx;
Then("^I register user (.+)?$", (String userId) -> {
registrationPage.registerUser(users.get(userId));
// inside registerUser(), should includes the whole proceduce:
// click register button
// fill form
// agree to the terms
// click submit button
// verify see welcome page
})
场景概述:作为想要注册的人
当我访问主页时
我注册用户
示例:
|用户ID|
| 001 |
| 002 |
| ... |
HashMap用户=xxxxx;
然后(“^I注册用户(+)?$”,(字符串userId)->{
registrationPage.registerUser(users.get(userId));
//在registerUser()中,应包括整个过程:
//点击注册按钮
//填表
//同意条款
//单击提交按钮
//请查看欢迎页面
})
您可以使用datatable和场景大纲的组合来完成此任务
..
..
And Enter form details
| firstname | surname |.........| password | reppwd |
| <fname> | <sname> |.........| <pwd> | <rpwd> |
..
..
Examples:
| fname | sname |.........| pwd | rpwd |
| Name1 | Title1 |.........| pwd1 | pwd1 |
| Name2 | Title2 |.........| pwd2 | pwd2 |
| Name3 | Title3 |.........| pwd3 | pwd3 |
或者使用datatable(名字、姓氏…)的头作为实例变量创建dataobject(UserDetails)
public void enterDetails(List<UserDetails> tab) { }
public void enterDetails(列表选项卡){
我在cucumber方面没有太多经验,但是您有没有注意到这一点:我希望避免做类似的事情,因为我最终希望将我的场景转换为场景大纲。我可能应该在我的问题中提到这一点(对不起!)。如果我将我的场景转换成一个带有“示例:”表的场景大纲,这个解决方案将不再有效,是吗?对不起,我已经给出了@Grasshopper的答案,因为这正是我想要的。这意味着数据存在于功能文件中,而不是Java代码中,这使得任何人(如产品经理)都更容易修改。这看起来非常有趣-我不知道cucumber中有“datatable”功能。将尝试使用您在此处描述的内容编写一个测试。
public void enterDetails(List<UserDetails> tab) { }