Search code examples
testingdomautomated-testse2e-testingtestcafe

Select element by containing text in TestCafe


How can I select an HTML element containing specific text?

In Selenium Xpath selectors are used, but TestCafe doesn't support Xpath.

How do I do it in TestCafe?


Solution

  • According to the official documentation, in order to select an element containing a certain text, I should use the .withText() method with the selected element, example given is:

    import { Selector } from 'testcafe';
    
    fixture `Example`
        .page `https://devexpress.github.io/testcafe/example/`;
    
    test('Click test', async t => {
        const selectBasedOnText = Selector('label').withText('I have tried TestCafe');
    
        await t
            .click(selectBasedOnText);
    });
    

    for selecting a label element with the text "I have tried TestCafe".