wix / Detox

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

How to simulate change of Date on Simulator/Emulator #4088

Closed abajajct closed 11 months ago

abajajct commented 1 year ago

Description

Hello, I am trying to put up a test case which requires me to change the date and then open the app again to test my case. I did some research and see that iOS simulator date depends upon the HOST on which it runs. This makes it impossible to test such scenarios without impacting the machine running the simulator. I wanted to check if there is something I am missing here. Any suggestions will be appreciated.

Your environment

Detox version: 19.2 React Native version: 0.71 Node version: 16 Device model: iOS OS: 16.4 Test-runner (select one): jest / other Jest

noomorph commented 1 year ago

If this is a visual test, I can suggest: https://wix.github.io/Detox/docs/api/device/#devicesetstatusbar-ios-only

But if you really need to have another time (e.g. you use it in network requests or business logic), well, I can suggest just only mocking: https://wix.github.io/Detox/docs/guide/mocking .

You need to be creative here: create some TimeService class (or some timeUtils module) and use it instead of just calling Date.now or new Date(). Create an alternative implementation based on a hardcoded value, mock server or command-line argument (e.g. parse it via react-launch-arguments) using e.g. .e2e.ts extension. Build your app (or run Metro) with that extension override and that should be it.

I hope my team allows someday to merge backdoor feature here: https://github.com/wix/Detox/pull/3245 If it were functioning, you could implement a small handler inside your app and set time right from Detox tests as you go. Unfortunately, you'll have to make a bit longer way (I told you your current options above), but if you think that would be nice to have, please put thumbs up in #3245. I hope one day Amit and Asaf get convinced that the feature is very nice.

d4vidi commented 1 year ago

@abajajct have you found the elaborate answer by @noomorph sufficient? Could you nevertheless explain what changing the date is required for? Is it due to the visibility of the time (which is programmable in other ways) or is it related to the app itself / back-end service of some sort?

abajajct commented 1 year ago

@d4vidi Hello, I want to test a date based application where from the day you start up, each day unlocks a new part of the application. @noomorph's answer is helpful and I really appreciate that but is there a more straightforward way to acheive this ?

abajajct commented 1 year ago

@noomorph The approach you mentioned, would it allow me to change the date in between a running tests. For example: I start with today's date -> Perform fews steps -> Change Date -> Perform steps > Change date again

noomorph commented 1 year ago

@abajajct, yes, why not? But you'd have to rewire your code dependent on Date to your "proxy-like" module with its override for e2e tests.

asafkorem commented 1 year ago

@abajajct @noomorph the setStatusBar API changes only the date in the status bar only (visually). Having a date-provider method and mocking it as Yarik mentioned sounds like a reasonable approach here.

However, there might be another solution for that, a simple one, but I haven't tried it yet. Please try to set a TZ (timezone) arg under the launchArgs parameter on the launchApp API. This API is not documented by Apple and I can't guarantee it will work. See this S.O. answer: https://stackoverflow.com/a/60172985

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back.

Thank you for your contributions!

For more information on bots in this repository, read this discussion.

asafkorem commented 1 year ago

@abajajct did my answer help you?

stale[bot] commented 11 months ago

The issue has been closed for inactivity.