【问题标题】:Getting the text from an input field returns object values instead of text value从输入字段获取文本返回对象值而不是文本值
【发布时间】:2021-04-19 17:26:33
【问题描述】:

我在做最简单的事情时遇到问题:验证字段中的值。

标记:

<input class="hb-inputfelt data-e2e-selector="boligbetegnelse" ng-reflect-form="[object Object]" id="finansieringsobjekt-boligbetegnelse-211-H01-0">

赛普拉斯:

.should('have.text', 'value');

.should('have.value', '123'):

返回

expected '<div>' to have value '123', but the value was ''

.should('have.text', '123');

返回

expected '<div>' to have text '123', but the text was 
  'a bunch of text - probably values from the object in ng-reflect-from=[object Object]

我猜问题出在字段中的对象,因为它返回的数据由来自前端“后面”对象的一长串对象值组成。

但我正在检查的值 - “123” - 是字段中的可见文本。上面的方法不行怎么办?

【问题讨论】:

  • 你能添加更多的标记来覆盖这个值吗?我猜invoke('attr', 'value') 会得到想要的结果。 Invoke reference
  • 用 cy.log() 给出结果 '[object Object]';
  • 请显示完整的 Cypress 命令,否则您的问题不完整且晦涩。

标签: cypress input-field


【解决方案1】:

您需要的文档是How-do-I-get-an-input-s-value

// make an assertion on the value
cy.get('input').should('have.value', 'abc')

【讨论】:

  • 我同意 - 如果您想验证值,这是最好的方法。如果他想验证可见文本,有一些插件
  • 这会产生相同的结果。预期 '
    ' 的值为 '123',但值为 ''
  • 这可能是因为当您应该定位问题顶部显示的input 时,您得到了div
  • 我想知道,是的。但是,如果我使用 .type() 我可以在输入字段中设置值。
猜你喜欢
相关资源
最近更新 更多
热门标签