首页 > 解决方案 > 赛普拉斯获取兄弟姐妹输入并更改值?

问题描述

我是赛普拉斯的新手,我想获取输入字段来修改值。

我可以选择行然后是兄弟姐妹,但我不知道如何选择<input ..... 在下图中,我们可以看到siblings选择行元素,现在我想选择输入元素,但我做不到:

<td>
  <input
   className="form-control"
   name="labelPred1"
   type="text"
   value={this.state.jsondata.prediction[k]}
   onChange={e => this.onPred1Change(k, e.target.value)}
 />
</td>


  it('modify prediction', () => {
    cy.get('table').contains('td', 'jour son facebook')
     .siblings()
     .contains('input[name=labelPred1]  ','0')
     .type('2')
  })

在上面的代码中,您可以看到我输入的 HTML 结构,然后是我的赛普拉斯测试,我也尝试contains(...).get('input')

标签: reactjscypress

解决方案


Siblings 需要一个选择器,因此您可以将输入选择器移入其中,

cy.get('table')
  .contains('td', 'jour son facebook')
  .siblings('td input[name=labelPred1]')  // specify td + child input
  .type('2')

或者下一个 td

cy.get('table')
  .contains('td', 'jour son facebook')
  .next()
  .find('input[name=labelPred1]')   // within the next td
  .type('2')

.contains('input[name=labelPred1]','0')不起作用,因为元素是 aninput并且'0'不在 text 属性上,它在 value 属性上。

.contains()只查找标签之间的文本

这可能有效

cy.get('table')
  .contains('td', 'jour son facebook')
  .siblings()
  .find('input[name="labelPred1"][value="0"]')
  .type('2')

推荐阅读