Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Testcafe, how can I wait for a 2nd element of the same selector to appear?

I have a scenario in which multiple elements of the same className appear one after the other (it depends on a server response).

What I'm trying to achieve is passing the test only after 2 elements of the same selector are present, but currently, it seems like the test fails because it keeps recognizing 1 element and then straight up fails without waiting for a 2nd one.

This is my code (called from the outside with a count argument of, say, 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;
};

I assume this happens because I'm checking whether msgHeadline exists, and it sees the first element when it gets rendered, and immediately fails. I'd like to wait for a 2nd one.

Any ideas?

like image 343
tkore Avatar asked Jun 25 '19 21:06

tkore


People also ask

How do you wait for an element to appear in Testcafe?

When TestCafe executes a Selector query, it waits for the target element to appear in the DOM. The query fails if it cannot find the target within the Selector timeout. The target element doesn't have to be visible for the Selector to succeed. import { Selector } from 'testcafe'; fixture `My fixture` .

How do you find an element in Testcafe?

If you need to access element properties not included in the selector's API, use the selector. addCustomDOMProperties method to retrieve them from DOM. import { Selector } from 'testcafe'; fixture `My fixture` . page `https://example.com`; test('Check Label HTML', async t => { const label = Selector('label').

How do I refresh my Testcafe page?

eval(() => location. reload(true)); You are right, you should use the code provided above to reload your test page.

What is Nth in Testcafe?

nth Method. Returns elements with the specified index. The index starts at 0. Negative index values indicate the element's location relative to the final element of the array.

How does testcafe evaluate selectors?

TestCafe tries to evaluate the specified selector multiple times within the timeout . If the element does not appear, the test will fail. The t.setFilesToUpload and t.clearUpload actions are exceptions because they do not require a visible target element. When evaluating a selector, TestCafe automatically waits for the element to appear in the DOM.

How do I force testcafe to wait for an element to appear?

When evaluating a selector, TestCafe automatically waits for the element to appear in the DOM. TestCafe keeps trying to evaluate the selector until the element appears in the DOM or the timeout passes. You can additionally require that TestCafe should wait for an element to become visible. Use the visibilityCheck selector option for this.

How do I identify an element in testcafe?

TestCafe selectors should use element identifiers that persist between test runs. However, JavaScript frameworks may generate dynamic IDs for page elements. To identify elements whose id attribute changes, use selectors based on the element’s class, content, tag name, or position: prevSibling.

How do I test actions in testcafe with multiple nodes?

If the selector matches multiple DOM nodes, the action is executed for the first node. Before an action is performed, TestCafe waits for the target element to be in the DOM and become visible. If this does not happen within the selector timeout, the test fails. You can also pass DOM element snapshots to test actions.


2 Answers

Just remove the .exists from your selector it returns boolean and then calling .count on it will fail the test.

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;

You can read more here https://devexpress.github.io/testcafe/documentation/test-api/selecting-page-elements/selectors/using-selectors.html#check-if-an-element-exists

like image 158
Dvir Yamin Avatar answered Sep 25 '22 01:09

Dvir Yamin


If both elements have same text and only this elements have this specific className then you can use nth() function

const msgHeadline = Selector('.myClassName')..withText(headlineText).nth(1);
await t
    .expect(msgHeadline.exists).ok(`Received less than ${count} desired messages with headline ${headlineText}`)

Here you take second element with headlineText and then assert, that it exists. Though i think you should check that it exists and displayed(visible)

like image 32
RedVarVar Avatar answered Sep 23 '22 01:09

RedVarVar