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?
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` .
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').
eval(() => location. reload(true)); You are right, you should use the code provided above to reload your test page.
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.
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.
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.
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With