wix / Detox

Gray box end-to-end testing and automation framework for mobile apps
https://wix.github.io/Detox/
MIT License
11.01k stars 1.9k forks source link

iOS: WaitFor Method using withTimeout is setting test into constant Loop and it will not go on #4449

Open alexandravychytill opened 1 month ago

alexandravychytill commented 1 month ago

What happened?

await waitFor(element(by.id(elementId))) .toBeVisible() .withTimeout(timeout); sets test into constant loop, for android current implementation working all fine.

Message:

The app is busy with the following tasks: • There are 2 work items pending on the dispatch queue: "Main Queue ()". • Run loop "Main Run Loop" is awake.

What was the expected behaviour?

only should go on till as defined amount of seconds

Was it tested on latest Detox?

Did your test throw out a timeout?

Help us reproduce this issue!

await waitFor(element(by.id(""))) .toBeVisible() .withTimeout(5000);

In what environment did this happen?

Detox version: 20.20.1 React Native version: 0.72.0 Has Fabric (React Native's new rendering system) enabled: yes Node version: >=16 Device model: iPhone 15 iOS version: 17.2 macOS version: 14.4 Xcode version: 15.2 Test-runner (select one): jest

Detox logs

Detox logs ``` The app is busy with the following tasks: • There are 2 work items pending on the dispatch queue: "Main Queue ()". After a while: The app has not responded to the network requests below: (id = 6) invoke: {"type":"expectation","predicate":{"type":"id","value":"home-tab","isRegex":false},"expectation":"toBeVisible","timeout":2000} Unresponded network requests might result in timeout errors in Detox tests. • Run loop "Main Run Loop" is awake. ```

Device logs

Device logs ``` paste your device.log here! ```

More data, please!

No response

asafkorem commented 1 month ago

@alexandravychytill I suspect that the app's main thread is blocked even before the wait-for assertion. To verify, you can try the assertion without the wait-for and timeout parts and see if you get the same result.

The issue with the main thread can happen for various reasons, and you might find our troubleshooting documentation helpful for this

If you continue to face difficulties with this issue, consider using the following workaround flag for DetoxSync to bypass the main run-loop sync issue:

await device.launchApp({
  ...
  launchArgs: {
    DTXDisableMainRunLoopSync: true,
  }
});
alexandravychytill commented 1 month ago

So I tried the launchArgs, but still when using the setTimout function it is looping with this message:

The app is busy with the following tasks:
• There are 2 work items pending on the dispatch queue: "Main Queue (<OS_dispatch_queue_main: com.apple.main-thread>)".
• Run loop "Main Run Loop" is awake.

The app on it's own is loading a few seconds due to internet connection after login, thats why I use the setTimout function, so all AC are visible after internet connection is established. On Android setTimout is working fine as said, but on iOS still there is always a loop when using it.

mroswald commented 1 month ago

@asafkorem Is there anything else to do other than adding the launchArgs? I updated the detox version to the latest in my package.json, but I have the feeling this flag doesn't change anything.

await device.launchApp({
  ...
  launchArgs: {
    DTXDisableMainRunLoopSync: true,
  }
});
alexandravychytill commented 1 month ago

@asafkorem Is it possible, that .withTimeout(timeout) is just not working currently for iOS?

alexandravychytill commented 1 month ago

Feel like my issue is related to https://github.com/wix/Detox/issues/4282, was there ever a good solution found @asafkorem?

Additional log:

The app has not responded to the network requests below:
  (id = 11) invoke: {"type":"expectation","predicate":{"type":"id","value":"home-tab","isRegex":false},"expectation":"toBeVisible","timeout":5000}

That might be the reason why the test "Login display Home view" has timed out.
asafkorem commented 1 month ago

@alexandravychytill are you sure the app didn't crash or something? Can you please provide the full logs? preferably with --loglevel trace

asafkorem commented 1 month ago

@mroswald are you sharing the same app and issue with @alexandravychytill? if not, can you please open a different issue and provide the error message + logs you're getting? If that's not the same case it might be a different issue..

mroswald commented 1 month ago

No, just wanted to know more about this flag as it's not documented anywhere.

asafkorem commented 1 month ago

@mroswald This is not-official or documented on purpose, I'm testing this new behaviour (experimental). It bypasses the main run loop check in Detox's synchronization mechanism on iOS when there's no other busy resource we can identify (like animations, network requests, timers etc.). In such scenarios, it becomes difficult to understand why the app seems busy and why Detox appears to be "stuck," even though something is pending in the main run-loop which is a real issue. Usually, there's no good reason to delay Detox from continuing the execution..

alexandravychytill commented 1 month ago

@asafkorem so after some investigation I found out, that the webview is causing my synchronization issue. As in the issue https://github.com/wix/Detox/issues/4438 described, when sharedCookiesEnabled is set to true, the synchronization problem on iOS is happening. My current fix is to disable sharedCookiesEnabled when testing with detox. But I would appreciate if you could investigate further and find a fix, so detox tests on iOS do not crash when sharedCookiesEnabled is true 😊