在Testcafe中,如何等待同一选择器的第二个元素出现?

Tal*_*ren 5 automated-tests web-testing e2e-testing testcafe

我有一个场景,其中同一元素的多个元素一个接一个地className出现(这取决于服务器的响应)。

我要实现的目标是仅在存在相同选择器的2个元素之后才通过测试,但是目前看来,该测试失败了,因为它不断识别1个元素,然后直接失败而没有等待第二个元素。

这是我的代码(从外部调用,count参数为2)-

import { Selector } from 'testcafe';

export const validateMsg = async (t, headlineText, count = 1) => {
  const msgHeadline = Selector('.myClassName').withText(headlineText).exists;

  const msgHeadLineExists = await t
    .expect(msgHeadline.count)
    .gte(count, `Received less than ${count} desired messages with headline ${headlineText}`);
  return msgHeadLineExists;
};

Run Code Online (Sandbox Code Playgroud)

我认为发生这种情况是因为我正在检查是否msgHeadline存在,并且呈现它时会看到第一个元素,并立即失败。我想等第二个。

有任何想法吗?

小智 9

只需.exists从选择器中删除,它返回布尔值,然后对其调用.count将会使测试失败。

const msgHeadline = Selector('.myClassName').withText(headlineText);

const msgHeadLineExists = await t
  .expect(msgHeadline.count)
  .gte(count, `Received less than ${count} desired messages with headline ${headlineText}`);
return msgHeadLineExists;
Run Code Online (Sandbox Code Playgroud)

您可以在这里阅读更多内容 https://devexpress.github.io/testcafe/documentation/test-api/selecting-page-elements/selectors/using-selectors.html#check-if-an-element-exists