Detox
Detox copied to clipboard
Testing if toasts are shown
What happened?
Is it possible to check if a toast is visible that is shown using the "ToastAndroid.show" api?
I have put together a simple test to illustrate. A toast is shown using ToastAndroid.show after clicking a button, and then trying to assert the toast text is shown, but the test fails trying to find the toast text.
I'm just after clarification if this is something detox is not able to test, or is there is something obvious I'm missing.
Example react component code
Example Test
Result
What was the expected behaviour?
Test should find the toast element by text.
Was it tested on latest Detox?
- [X] I have tested this issue on the latest Detox release and it still reproduces.
Did your test throw out a timeout?
- [ ] I have followed the instructions under Identifying which synchronization mechanism causes us to wait too much.
Help us reproduce this issue!
No response
In what environment did this happen?
Detox version: 20.19.4 React Native version: 0.73.6 Has Fabric (React Native's new rendering system) enabled: (yes/no) no Node version: 20.11.1 Device model: SM-T636B Android version: 14 Test-runner (select one): jest
Detox logs
Detox logs
paste logs here!
Device logs
Device logs
paste your device.log here!
More data, please!
No response
Can you reproduce it on iOS? @SamWhitby
Probably will be explained here -> #4363
@gosha212, I'm actually using the package react-native-simple-toast and this is working as expected in iOS, its just Android that has the problem (it uses the ToastAndroid.show api internally).
I saw https://github.com/wix/Detox/issues/4363 which gave me hope this is possible in android and not just a limitation with the package, but i cant find anything anywhere showing how to make this work.
@SamWhitby which Detox API do you use for testing, that works on iOS?
I strongly believe a waitFor().withTimeout()
should do the job for both platforms.
@d4vidi below is the detox api i'm using that works in iOS and doesn't work in android.
await expect(
element(by.text('testToastMessaage')),
).toBeVisible();
I have also tried what you suggested in android and still doesn't work. e.g.
await waitFor(element(by.text('testToastMessaage')))
.toBeVisible()
.withTimeout(5000);
@SamWhitby Could you share whether the test fails on the waitFor()
(i.e. due to the timeout) or rather on the follow-up action (e.g. tapping on the toast / dismiss)?
@SamWhitby As we happen to come across related things today, it appears that Android's Toast
is a system element that cannot be detected by Detox.
I think you could resort to Detox's UIAutomator
delegation API in order to find a way to deal with it nonetheless. Please post back if you find a way. Here's a lead: https://automationchronicles.com/testing-toast-messages-with-ui-automator/
(Thanks @asafkorem)
Apparently matching Toasts is a bit of a pain in pure Android as well - https://www.browserstack.com/guide/test-toast-message-using-espresso; We might be able to integrate a solution for that in Detox but for now UIAutomator is the best best