黄瓜硒 - 填写网络表格

Van*_*gan 2 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 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
Run Code Online (Sandbox Code Playgroud)

我知道 Cucumber 生成的 step defs 会为其中的每一个生成一个单独的方法,这很好。那么我是否必须以自己的方法在“RegistrationPage”中实现这些步骤中的每一个?我想要了解的是:有没有办法在 RegistrationPage 中实现一种“fillInform()”方法而不是单独的方法?

编辑:

我问的问题可能是错误的(我试图保持简短)。我的目标是能够做这样的事情:

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           |
Run Code Online (Sandbox Code Playgroud)

在这样的场景大纲中,是否还可以使用单一方法填写注册表?我想到的(我不确定这会带来什么影响)是:

@When("^I enter \"([^\"]*)\" in the firstname input$")
public void enterFirstname(String firstname) {
    registrationPage.firstname = firstname;
}
Run Code Online (Sandbox Code Playgroud)

然后,当测试按下“提交”按钮时:

@When("^I click the Submit button$")
public void clickSubmitButton() {
    registrationPage.fillInForm();
    registrationPage.clickJoinButton();
}
Run Code Online (Sandbox Code Playgroud)

但是,这样做对我来说并不“感觉正确”(尽管我可能会误会 - 也许这是可以接受的?)

Gra*_*per 5

您可以结合使用数据表和场景大纲来完成此操作。

..
..
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 |
Run Code Online (Sandbox Code Playgroud)

您可以使用原始数据表来获取步骤定义中的数据。

public void enterDetails(DataTable tab) { }
Run Code Online (Sandbox Code Playgroud)

或者使用数据表的标题(名字,姓氏...)作为实例变量创建一个数据对象(UserDetails)。

public void enterDetails(List<UserDetails> tab) { }
Run Code Online (Sandbox Code Playgroud)