Selenium-Gherkin-Java:验证必填字段上的文本

时间:2018-09-13 13:14:20

标签: java selenium bdd gherkin

简短版本:我有一个必填字段,显示“此字段是必填字段”的元素可以包含多个消息。我正在努力设置BDD功能步骤,页面和步骤文件以验证是否显示了正确的消息。

长版: 对Java / Selenium来说相对较新,对这种形式的BDD Automation测试来说还是较新的,但是我掌握了这个框架,并且一直在努力。我将场景添加了一步,因为其中有两个是必填字段,但尚未进行测试。

该网站正在开发中,它将有20多个区域版本,因此我已尝试将任何硬性限制降至最低/

场景

Scenario: 'Do you want to Join an Existing Company?' is an optional radio button selection field which displays 'Company Account Number' and 'Company Name' fields if the 'Yes' option is selected and is defaulted to option 'No'
Given The user is on the Lite Registration page
Than The 'Do you want to Join an Existing Company' radio button selection field is displayed
And the default selection is 'No'
And 'Company Account Number' and 'Company Name' fields are displayed for selection 'Yes'
And A 'Join request' message is displayed for a selection of 'Yes'
And 'Company Account Number' is a numeric field with max 30 char
And 'Company Name' is an alphanumeric filed with max 60 char
And 'Company Account Number' and 'Company Name' are mandatory fields <-- My new step

问题在于包含错误消息的元素(“此字段是必填字段。”)还会显示其他消息,所以我不能仅仅确认显示错误的元素。

我一直在尝试使用asserts和该元素的.getText命令来做一些事情,但是我要么在不应该通过它时通过,要么由于断言错误而忽略了该步骤。

页面文件的相关部分

@FindBy(css = "#accountNumber-error")
private WebElement JECAccountNumberCharError;


//Methods


public boolean Try1() {

    String m = JECAccountNumberCharError.getText();
    return m.equals("This field is required");
} here

Step文件的相关部分

 @And("^'Company Account Number' and 'Company Name' are mandatory fields$")
public void companyAccountNumberAndCompanyNameAreMandatoryFields() throws Throwable {
    Assert.assertTrue(liteRegistration_page.Try1());
}

在上述情况下,测试将被忽略,并在输出中出现断言错误

> java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52) 
at steps.registration.LiteRegistration_Steps.companyAccountNumberAndCompanyNameAreMandatoryFields(LiteRegistration_Steps.java:481)
at ✽.And 'Company Account Number' and 'Company Name' are mandatory fields(Registration/LiteRegistration_FeildValidation.feature:78) 

我要用想要的解决方案吠叫错误的树吗?非常感谢您偷看帮助

编辑:元素的HTML

<div class="label-floating form-group error-group">
                    <label for="accountNumber" class="control-label">Company account number</label>
                    <input type="text" class="form-control error" name="account.accountNumber" id="accountNumber" value="" aria-invalid="true" required=""><label id="accountNumber-error" class="error" for="accountNumber">This field is required.</label>

                    <input type="hidden" id="accountNumberInexistent" value="This account number does not exist">
                </div>

0 个答案:

没有答案