黄瓜硒 - 填写网页表格

时间:2018-01-16 21:13:07

标签: 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

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

修改

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

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           |

鉴于这样的情景大纲,是否仍然可以使用单一方法填写注册表格?我所想到的(我不确定这会产生什么影响)是:

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

然后,当测试按下&#34;提交&#34;按钮:

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

然而,这样做并不是&#34;对我来说感觉正确&#34; (虽然我可能会弄错 - 也许它是可以接受的?)< / p>

2 个答案:

答案 0 :(得分:3)

您可以使用数据表和场景大纲的组合来实现此目的。

..
..
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 |

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

public void enterDetails(DataTable tab) { }

或者使用datatable(firstname,surname ...)的标头作为实例变量创建数据对象(UserDetails)。

public void enterDetails(List<UserDetails> tab) { }

答案 1 :(得分:2)

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);
    ...
}

有一个问题需要解决,如何传递user的参数fillForm()的值,你需要考虑你的代码框架,它如何存储用户数据,它是如何得到的用户数据。解决方案依赖于自我代码框架。没有统一的解决方案。

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));
})

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
})
相关问题