Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing if toasts are shown #4436

Open
1 of 2 tasks
SamWhitby opened this issue Apr 1, 2024 · 8 comments
Open
1 of 2 tasks

Testing if toasts are shown #4436

SamWhitby opened this issue Apr 1, 2024 · 8 comments

Comments

@SamWhitby
Copy link

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
image

Example Test
image

Result
image

What was the expected behaviour?

Test should find the toast element by text.

Was it tested on latest Detox?

  • I have tested this issue on the latest Detox release and it still reproduces.

Did your test throw out a timeout?

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

@gosha212
Copy link
Contributor

gosha212 commented Apr 2, 2024

Can you reproduce it on iOS? @SamWhitby

@gosha212
Copy link
Contributor

gosha212 commented Apr 2, 2024

Probably will be explained here -> #4363

@SamWhitby
Copy link
Author

@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 #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.

@d4vidi
Copy link
Collaborator

d4vidi commented Apr 9, 2024

@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.

@SamWhitby
Copy link
Author

@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);

@d4vidi
Copy link
Collaborator

d4vidi commented Apr 10, 2024

@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)?

@d4vidi d4vidi closed this as completed Apr 10, 2024
@d4vidi d4vidi reopened this Apr 10, 2024
@d4vidi
Copy link
Collaborator

d4vidi commented Apr 10, 2024

@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)

@d4vidi
Copy link
Collaborator

d4vidi commented Apr 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants