Testcafe: Unable to find element by exact matching inner text

Created on 3 Mar 2017  路  10Comments  路  Source: DevExpress/testcafe

Are you requesting a feature or reporting a bug?

feature

What is the current behavior?

Selector.withText(text) method creates a selector that filters a matching set by a containing the specified text.

What is the expected behavior?

Sometimes it is necessary to find element by exact matching inner text

How would you reproduce the current behavior (if this is a bug)?

Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code


Specify your

  • operating system:
  • testcafe version:
  • node.js version:
Auto-locked enhancement

Most helpful comment

May be withExactText

All 10 comments

Hello, Dmitry! I guess, you want something likeSelector('*').withText(theText). It matches every element on the page, and then filters it with the specified text.

I need a strong match, that means "equal", not "contains".

For example, a have two divs "Object 1" and "Object 11". I want to find only "Object 1", but with .withText('Object 1') I will get both these divs.

I have solved this problem by the following approach:

`var innerTextEquals = ClientFunction((element, text) => {
return $.trim(element.innerText) === $.trim(text);
});

//...
Selector('.dx-navigation-item').filter((node, idx) => {
return innerTextEquals(node, itemCaption)
}, { innerTextEquals, itemCaption });`

I guess it would be nice to have this feature. As a temporary workaround you can represent your text as regexp and set boundaries:

import escapeRegExp from 'lodash/escapeRegExp';

function getWholeTextRe(text) {
    return new RegExp(`^${escapeRegExp(text)}$`);
}

// use it as t.expect(smthg).contains(getWholeTextRe('myText'));
// or Selector('div').withText(getWholeTextRe('myText'))

We need to figure out API for that. How about Selector.withWholeText()? cc @DevExpress/testcafe @VasilyStrelyaev

what about withTextStrictly or withStrictText

May be withExactText

May be better to change withText to do exact search by default

and add a new method withTextContains or withTextIncludes or withPartialText

I wouldn't like to make a breaking change here. It can break a lot of existing tests. The withText method can take a regexp so it's quite flexible for any cases. So I'd like to leave it as is

closed in #2124

This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.

Was this page helpful?
0 / 5 - 0 ratings