Set custom device dates in XCUITest tests
Set custom dates on a real iOS device from your XCUITest tests on BrowserStack App Automate.
You can set custom dates on BrowserStack real iOS devices to test date-dependent app behavior, such as scheduled banners, trial or subscription expiry, and calendar-based flows. To change the date, call changeDate from the BrowserStackTestHelper framework in your test.
Prerequisites
- Your build runs on real iOS devices with iOS 16 or later.
- You have the
BrowserStackTestHelper.frameworkfile. To get the framework, contact BrowserStack support.
Set custom device dates in your tests
Complete the following steps to add the framework to your test suite and set custom device dates in your XCUITest tests:
Add the framework to your UI test target
In Xcode, add BrowserStackTestHelper.framework to your project’s UI test target folder to link the framework to your test suite.
If you don’t yet have the framework, contact BrowserStack support.
Update Build Phases to include the framework
Complete the following steps to ensure the framework is listed under your UI Test Target’s Build Phases:
Open project settings and select the UI Test Target.
Go to the Build Phases tab, click +, and then select New Copy Files Phase.
Set the Destination to Frameworks.
Drag BrowserStackTestHelper.framework into this section. Use one of the following framework versions:
- For simulators, use the framework built for simulators.
- For real devices, use the framework built for physical devices.
Change the date in your test
To set a custom date, open your test file and perform the following steps:
- Import the framework.
- Create an injector with
InjectorFactory.createInstance(). - Before the steps that depend on the date, call
changeDatewith the day, month, and year that you want the device to use.
You can set a custom device date up to 21 days from the current date. The limit applies to the whole build, not to each test. For details, see How the 21-day limit works.
Sample code
The following code sets the device date to October 16, 2026. Replace the day, month, and year with a date up to 21 days from the current date:
Run your tests on BrowserStack
Upload your app and the updated test suite, and then start the build. For detailed steps, see Run your first automated app test using XCUITest.
How the 21-day limit works
You can set a custom device date up to 21 days from the current date. The following rules apply when you set custom dates:
- The 21-day limit applies to the whole build, not to each test.
- The device keeps a custom date for the rest of the build.
- If you set another custom date in a later test in the same build, BrowserStack calculates the 21-day limit from the current date, not from the device date. For an example, see Example scenario.
- You can’t set a date in the past.
- BrowserStack resets the device date only after the build ends.
If a custom date is more than 21 days from the current date, the changeDate call fails with the error message You can change date only upto 21 days in the future.
Example scenario
If the current date is October 1 and your build runs three tests that each set a custom date, BrowserStack counts the days from October 1 to each custom date:
- In the first test, you set the custom date to October 6. The count is 5 days.
- In the second test, you set the custom date to October 20, which is 14 days from the device date of October 6. The count is 19 days.
- In the third test, you set the custom date to October 27, which is 7 days from the device date of October 20. The count is 26 days, so the
changeDatecall fails.
Each test sets a date fewer than 21 days from the device date, but the count goes past 21 days in the third test. Plan the custom dates in a build so that no custom date is more than 21 days from the current date.
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!