微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查输入字段是否为空在赛普拉斯测试中无法正常工作

如何解决检查输入字段是否为空在赛普拉斯测试中无法正常工作

我在 Cypress 中有 2 个步骤定义,用于检查输入字段是否为空(取决于我如何构建我使用 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 会测试输入字段的输入标签间的值,但不会检查输入标签中的值属性

至少,我尝试在步骤定义中添加一个 invoke('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() 有效还是有更好的解决方案来覆盖所有情况?

非常感谢您的帮助。

解决方法

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

>>> mat = [[None]*2]*2
>>> id(mat[0])
76447616
>>> id(mat[1])
76447616
>>> grid = [0,0]
>>> x = [[None]*len(grid) for _ in range(len(grid))]
>>> id(x[0])
76457744
>>> id(x[1])
76391968

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。