检查输入字段是否为空或在Cypress测试中工作不正常



我在Cypress中得到了两个步骤定义,用于检查输入字段是否为空(取决于我如何使用RegEx构建句子(。

首先,我的问题是,赛柏说测试失败是因为输入字段为空,而不是。

我定义的步骤:

/** We check if the input field with the given name is empty */
Given(/^The input field "(.*)" is (not )?empty$/, (inputFieldName, negation) => {
if (negation === 'not ') {
CypressTools.getByName(inputFieldName).should('not.be.empty');
} else {
CypressTools.getByName(inputFieldName).should('be.empty');
}
});
/** We check if the input field with the given name is visible and empty */
Given(/^The input field "(.*)" is visible and empty$/, (inputFieldName) => {
CypressTools.getByName(inputFieldName).should('be.visible').should('be.empty');
});

在我的特定测试中,柏树应该检查一个值填充的输入字段,步骤定义如下:输入字段";XYZ";不为空

我可以看到,if条件运行良好,因此在定义或RegEx站点上没有问题。但是测试失败了,因为Cypress说输入字段是空的,但它不是。

我怀疑Cypress测试输入字段中输入标记之间的值,但不检查输入标记中的值属性。

至少,我尝试在步骤定义中添加调用('val'(

CypressTools.getByName(inputFieldName).invoke('val').should('not.be.empty');

它适用于第一步定义,但当我为第二步定义时,柏树测试失败了,并告诉我:

Timed out retrying: You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
> visible
The invalid subject you asserted on was:
> 
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.

我不明白这里的问题。这个方法对invoke((有效吗?或者有更好的解决方案来覆盖所有情况吗?

非常感谢你的帮助。

错误消息指向的问题是,沿着命令链传递的主题不适合下一步

CypressTools.getByName(inputFieldName)
.invoke('val')                      // changes subject to the text of the input
// (not a DOM element)
.should('be.visible')               // needs a DOM element
.should('not.be.empty');

最可靠的方法是将测试分为两个步骤

CypressTools.getByName(inputFieldName).should('be.visible');
CypressTools.getByName(inputFieldName)
.invoke('val')
.should('not.be.empty');

但我认为一个简单的重新排序也可以

CypressTools.getByName(inputFieldName)
.should('be.visible')              // check the DOM element, passes it on as subject
.invoke('val')                     // changes subject to the text of the input
.should('not.be.empty');           // check the text is not empty

最新更新