【问题标题】:How to wait until only one row shows from autocomplete entry如何等到自动完成条目只显示一行
【发布时间】:2019-09-23 12:56:53
【问题描述】:

我正在针对我无法以任何方式更改的 PHP 站点运行 Cypress。

它有一个自动完成输入字段,可以将 XHR 调用流发送到服务器。

结果填充一个表格,其中每一行都有一个可以单击的按钮。

我需要等到只有一行,然后点击它的按钮。

cy.get('#dataTables_clients_filter > label > .form-control').type(client).then(() => {
  cy.get('table#dataTables_clients > tbody > tr > td:nth-child(6) > i').click();
}

上面的结果是错误cy.click() can only be called on a single element. Your subject contained 100 elements.

XHR 调用的数量取决于客户的价值。

困难在于:theInput.type(client) 一次插入一个字母,页面会为每个字母发送一个新的 XHR。这意味着最初会有一个很长的行列表,其中一个将始终包含“客户端”。随着处理的字母越来越多,列表越来越短,最终只剩下一行。

我怎样才能等到以下任何一项为真?

  • tbody > tr:nth-child(0) > td:nth-child(4) 等于 client
  • 不再发送 XHR 调用
  • 表格行数组的长度等于1

【问题讨论】:

    标签: cypress


    【解决方案1】:

    最简单的方法是确保所有 3 个都有效。你可以这样做:

    // wait until the XHR call is done:
    cy.server()
    cy.route('/api-call/which/should/be/finished').as('xhr')
    cy.wait('@xhr') // will wait until xhr has appeared.
    // Be sure that "tbody > tr:nth-child(0) > td:nth-child(4) equals 'client'"
    cy.get('tbody')
      .contains('td', 'client') // will search for a td with 'client' in it.
    // The length of the tables array of rows is equal to 1
    cy.get('tbody')
      .find('tr') // this will be valid if there is at least 1 row.
    

    【讨论】:

    • 表面上看起来没问题,但是...当 theInput.type(client) 一次插入一个字母时,页面会为每个字母发送一个新的 XHR。这意味着最初会有一长串行,其中一行将始终包含“客户端”。随着越来越多的字母被处理,列表变得越来越短,最终只剩下一行。因此,您的所有三个测试都将在发送第一封信时成功。同时,我非常感谢您有兴趣提供帮助。谢谢。
    • 检查一下,你期望的行为是什么?因为如果该断言对于每个输入都是正确的,那么您甚至不需要执行它。
    • 我列出了三个条件。他们中的任何一个都可以解决我的问题。您的 3 条建议中没有一条能给出必要的结果。
    • 啊,检查,所以三个断言之一应该是真的?这会很有趣,因为赛普拉斯无法检查是否不再有 XHR 调用。它只能检查指定的 XHR 调用。
    • 感谢您提供一些相互考虑。 :-)
    【解决方案2】:
    cy.waitUntil(() => {
      return cy.get('@Clientes').eq(0).children().eq(4).then(($cl) => {
        const res = Boolean($cl[0].innerText === client);
        return res;
      });
    }, {
      errorMsg: 'Autocomplete failed to find client',
      timeout: 15000,
      interval: 500
    });
    

    这个! Add the Cypress waiting power to virtually everything.

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2010-10-23
      • 1970-01-01
      • 2012-03-10
      • 2011-07-09
      • 1970-01-01
      • 2014-03-04
      • 2020-11-26
      • 2017-03-09
      相关资源
      最近更新 更多