Detox uses Matchers to find UI elements
in your app, Actions to emulate user interaction with those elements
and Expectations to verify values on those elements
.
Matchers find elements in your app that match one or more properties.
NOTE: Whenever possible we recommend to match elements by.id
, these are more resilient to layout restructuring and text/language changes
by.id
will match an id that is given to the view via testID
prop.
In a React Native component add testID like so:
<TouchableOpacity testID={'tap_me'}}>
...
Then match with by.id
:
await element(by.id('tap_me'));
For other cases, and only if you can't use by.id
there is a variety of options:
Find an element by text, useful for text fields, buttons.
await element(by.text('Tap Me'));
Find an element by accessibilityLabel
on iOS, or by contentDescription
on Android.
await element(by.label('Welcome'));
Find an element by native view type.
await element(by.type('RCTImageView'));
Find an element with an accessibility trait. (iOS only)
await element(by.traits(['button']));
await element(by.id('Grandson883').withAncestor(by.id('Son883')));
await element(by.id('Son883').withDescendant(by.id('Grandson883')));
-
To find the view with the id
Son883
<View testID='Grandfather883' style={{padding: 8, backgroundColor: 'red', marginBottom: 10}}> <View testID='Father883' style={{padding: 8, backgroundColor: 'green'}}> <View testID='Son883' style={{padding: 8, backgroundColor: 'blue'}}> <View testID='Grandson883' style={{padding: 8, backgroundColor: 'purple'}} /> </View> </View> </View>
Use:
// any of the following will work await element(by.id('Son883')) await element(by.id('Son883').withAncestor(by.id('Father883'))) await element(by.id('Son883').withDescendant(by.id('Grandson883')))
await element(by.id('UniqueId345').and(by.text('some text')));
The first valid index is 0.
await element(by.text('Product')).atIndex(2);
Tip: To find the back button on iOS use:
await element(by.traits(['button']).and(by.label('Back')));