I make automatization react native test with detox, It has the next screen sequence A -> B -> C
and i wish to go back to the screen B <- C.
Is there a solution for this?
![](../../users/profiles/2004530.webp)
- 1,598
- 17
- 30
![](../../users/profiles/6351062.webp)
- 81
- 5
6 Answers
There's a testId on the back button, so you can do this:
await element(by.id("header-back")).tap();
![](../../users/profiles/587955.webp)
- 47
- 2
-
This worked for me on the following environment: Xcode: 11.5 react-native: 0.61.5 "detox": "^16.8.2", – Olivier Jun 18 '20 at 12:45
sometimes
await element(by.id("header-back")).tap();
does not work and
await element(by.traits(['button']))
.atIndex(0)
.tap();
selects another button. In that case, you can try to use swipe right on ios assuming that it is a stack navigator. Use the outer container view
await element(by.id('containerView')).swipe('right', 'fast', 0.1);
![](../../users/profiles/2625231.webp)
- 180
- 1
- 8
You could go ahead and create a utility
export const pressBack = async () => {
if (device.getPlatform() === 'android') {
await device.pressBack(); // Android only
} else {
await element(by.traits(['button']))
.atIndex(0)
.tap();
}
};
- Android: device.pressBack()
- iOS: go back last screen #828
![](../../users/profiles/1998086.webp)
- 7,914
- 2
- 35
- 46
If you are using react-native-navigation
you can use:
const backButton = () => {
if (device.getPlatform() === 'ios') {
return element(by.type('_UIBackButtonContainerView'));
} else {
return element(by.label('Navigate Up'));
}
};
...
await backButton().tap();
For iOS in detox@17.3.6
& react-native-navigation@6.10.1
you can use:
return element(by.id('pop'));
![](../../users/profiles/4759619.webp)
- 1,940
- 1
- 14
- 36
Another way that works is
await element(by.id('header-back')).atIndex(0).tap()
This uses the built in testID that the default back button that comes with react-navigation v5. You may need to mess with the atIndex() number since for me it seems to match 2 back buttons but the first one was the one I was looking for.
![](../../users/profiles/7513316.webp)
- 815
- 12
- 15
the solution was to use traits button as follows:
await element(by.traits(['button'])).atIndex(0).tap();
![](../../users/profiles/6351062.webp)
- 81
- 5