Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TestCafe expect text to equal one of the following

I’m writing a test where if the user clicks a button, there should only be 2 scenarios (texts) that could happen.

For example

await t
    .click('button')
    .expect(Selector('div#result').textContent)
    .eql('My text A')
    // or 'My text B' ??

How can I test this in TestCafe?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First you await the user clicks the button, then save the textContent in a variable, and inside expect you can check on both conditions with or operator ||, if false it will output the message inside the ok.

test('Button click results in one of two texts', async t => {
    await t.click('button');
    
    const textContent = await Selector('div#result').textContent;

    await t.expect(textContent === 'My text A' || textContent === 'My text B')
        .ok('Wrong text.');
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading