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

Input 检查输入字段是否为空或在Cypress测试中工作不正常,input,field,cypress,is-empty,input-field,Input,Field,Cypress,Is Empty,Input Field,我在Cypress中得到了两个步骤定义,用于检查输入字段是否为空(取决于我如何使用RegEx构建句子) 首先我的问题是,cypress说测试失败是因为输入字段是空的,而不是空的 我定义的步骤: /** We check if the input field with the given name is empty */ Given(/^The input field "(.*)" is (not )?empty$/, (inputFieldName, negation) =&

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

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

我定义的步骤:

/** 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');
});
在我的特定测试中,cypress应检查填充值的输入字段,步骤定义如下: 输入字段“XYZ”不是空的

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

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

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

CypressTools.getByName(inputFieldName).invoke('val').should('not.be.empty');
它适用于第一步定义,但当我也适用于第二步定义时,cypress测试失败并告诉我:

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')//根据输入的文本进行更改
//(不是DOM元素)
.should('be.visible')//需要一个DOM元素
.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')//检查DOM元素,将其作为主题传递
.invoke('val')//根据输入的文本进行更改
.should('not.be.empty');//检查文本是否为空

您是否尝试过
cy.get(选择器).should('not.Have.value')
cy.get(选择器).should('Have.value','desiredValue')
这些对我来说适用于Vue应用程序。可能您使用的插件或您正在使用的框架有一些特定的问题。这里的问题是,我无法预测输入字段中的值-have.value是否有任何独立/通用的情况?我看到的唯一解决方案是`CypressTools.getByName(inputFieldName).invoke('val').should(val=>{expect(val.length).to.be.greaterThan(0)});