首页 > 解决方案 > Timeout value for getAllByTestId?

问题描述

  cy.wait(20 * 1000);

  cy.getAllByTestId('test-field-one')
    .first()
    .within(() => cy.getByTestId('test-field-two'))
    .should('contain', 'test');

I'm currently using the above code, but I don't like the wait. I'd much rather prefer a timeout value. Does anyone know if this is possible in cypress?

标签: cypress

解决方案


这些getAll...函数不能真正使用超时。

{ timeout: 20 * 1000 }和之间的区别在于cy.wait(20 * 1000)超时在满足条件时会提前返回。

但究竟有多少是“全部”?

进行“全部”查询的唯一方法是等待最长时间并在该时间点返回所有内容 - 这就是您已经使用 explicit 所做的cy.wait()


由于您正在使用.first(),只需切换到cy.getByTestId()

cy.getByTestId('test-field-one', { timeout: 20 * 1000 })
  .within(() => cy.getByTestId('test-field-two'))
  .should('contain', 'test')

另请注意

.should('contain', 'test')

正在测试test-field-one不是test-field-two因为.within()没有传递内部值,而是传递了前一个主题。


推荐阅读