Detox icon indicating copy to clipboard operation
Detox copied to clipboard

(WIP) Support regex in matchers

Open asafkorem opened this issue 3 years ago • 7 comments

Describe your idea

Support regular expression in Detox matchers

Specifically in by.text(), by.label(). This can be useful since text and labels may include dynamic content (e.g. date).

Also, I suggest to change the current API, from: https://github.com/wix/Detox/blob/246317d3ee5b7587bb966c78204b5def5b72dfaf/detox/index.d.ts#L804

To:

text(text: string | RegExp): NativeMatcher;

Usage example

This code will find an element with the text "The year is 2023", if exists:

const someElement = element(by.text(/The year is: \d{4}/));

While this code won't:

const someElement = element(by.text('The year is: \d{4}'));

asafkorem avatar May 02 '22 07:05 asafkorem

Why not dynamically recognize instanceof RegExp versus string?

noomorph avatar May 02 '22 08:05 noomorph

Why not dynamically recognize instanceof RegExp versus string?

Sounds great, let's do it this way.

asafkorem avatar May 02 '22 08:05 asafkorem

@asafkorem the only pitfall I see is that we might need to standardize RegExp mechanisms between iOS/Android/JS...

noomorph avatar May 02 '22 08:05 noomorph

@noomorph that is spot on. We've somehow manged to overcome that in the context of the URL's blacklist - we could revisit and hopefully replicate.

d4vidi avatar May 03 '22 07:05 d4vidi

Nevertheless I'd like this kind of an API to come with a warning of some sort, that would explain that ideally, whichever text we trying to match should be deterministic to the point it can be predetermined down to the character. Of course, this is not always the case in unmocked tests, but generally this should be the preferred situation.

d4vidi avatar May 03 '22 07:05 d4vidi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back.

Thank you for your contributions!

For more information on bots in this reporsitory, read this discussion.

stale[bot] avatar Jun 10 '22 20:06 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Jun 18 '22 16:06 stale[bot]

This is super useful when working with iOS elements because you can't seem to expose testIDs (or equivalent) so I'm using accessibilityLabel to perform the function of both an identifier and additional state.

boxabirds avatar Dec 01 '22 09:12 boxabirds

Is someone working on this enhancement?

mariushorvat avatar Apr 21 '23 21:04 mariushorvat

Not really, @asafkorem 's been busy last months trying to release Detox 21 with XCUITest.

@asafkorem would you mind to take this issue to the final mile? Looks like there is demand for it, 10*👍 already

noomorph avatar Apr 22 '23 07:04 noomorph

Side note (because I couldn't find it easily beforehand): The work that's been put into see is available in this old branch - optimize-app-fix-regression

d4vidi avatar May 30 '23 14:05 d4vidi

This feature was released in version 20.11.0, thanks to @andrius-girdzius!

asafkorem avatar Jul 05 '23 07:07 asafkorem