Callback and Polling for Cypress tests
Learn how to run your tests in CI/CD successfully and get build status.
The CLI v1.6.0 and higher now supports both sync
and async
modes of running tests. The sync
mode is the default mode that executes commands serially, while the async
mode lets you execute tests modularly. You can set these modes as follows:
browserstack-cypress run --async
You may observe some idle time before you start seeing the test status on the terminal as we might still be installing the npm packages that your tests need.
Running tests in async
Running your Cypress tests on BrowserStack is completely async until v1.6.0. We recommend that you upgrade to the latest CLI version. If your CLI version is v1.5.0 or lower, note the following:
- Your CI/CD job will be automatically marked as success as the CLI exits with a success code, and doesn’t reflect the actual build status.
- If you’re using Local testing, your Local connection might get closed after the command to run the tests is done executing.
- If you configured to get build completion notifications from your CI, you will get a notification after test submission, and not after build completion.
Poll build status to handle build updates
You can keep polling the build status using build-info
command in a loop
to monitor the build status, and then close the Local connection, and fail the
build if the tests failed.
The following sample script uses the build-info
command through CLI at regular intervals to poll the status of the build. The build status
is set to running
when the build is running and is changed to done
when the build is done running.
#!/bin/sh
# Get the build ID from the run command's output
BUILD_ID=$(browserstack-cypress run | grep 'build id:' | rev | cut -d" " -f1 | rev)
# Or, you can read this from the log/build_results.txt file as well
# Say, it takes around 60 seconds on an average to finish your tests
# You can wait for 60 seconds, and then start polling for build status every 10 seconds
BUILD_TIME_AVG=60
POLLING_INTERVAL=10
BUILD_STATUS="running"
$(sleep $BUILD_TIME_AVG)
while [ $BUILD_STATUS == "running" ]
do
# Get the build status
$(browserstack-cypress build-info $BUILD_ID > temp.txt)
BUILD_STATUS=$(awk '/"status":/ {print $2} ' temp.txt | head -n1 | cut -d"," -f1 | cut -d"\"" -f2)
$(rm temp.txt)
# Sleep until next poll
$(sleep $POLLING_INTERVAL)
done
Use callback URLs to get updates
Executing the run
command with --async
exits successfully when the test builds are successfully started on BrowserStack. Since the test execution is async
in nature, you can use the callback feature to get updates as the build is running and after the build is complete.
You can use the callback URL to get the notifications on Slack, HipChat etc., or to mark the build as failed / passed in your CI.
You can use the callback_url
key in the run_settings
option in the browserstack.json
file to get an update after that build is complete. The payload sent to this callback URL is the same as the one you get with the build-info BUILD_ID
command, along with a text
field that you can directly use as a message text in any communication tool such as, Slack, MS Teams, HipChat, etc.,
Get build completion updates on Slack & Microsoft Teams
You can create an app that can create incoming webhooks and use it in the run_settings
configuration.
A feature-rich integration for Slack is in works and will be released soon.
You can accept incoming webhooks in Slack, and then use it in the run_settings
configuration as follows:
Check out the Slack guide to configure incoming webhook.
as follows:
{
"run_settings": {
...
"callback_url": "https://hooks.slack.com/TXXXXX/BXXXXX/XXXXXXXXXX",
...
}
}
After every build completion (success or failure), a message is sent in your selected channel with your build name, project name, and a link to the test report for that build as follows:
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
- RESOURCES
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!