Callback and Polling for Cypress tests

Learn how to run your tests in CI/CD successfully and get build status.

Starting v1.6.0, the CLI supports both sync and async behaviour (async being the default to not break existing workflows). You can run the tests in sync by using the --sync arg as follows:

Command Line
browserstack-cypress run --sync
Note: You may observe some idle time before you start seeing the test statuses as we might still be installing the npm packages your tests might need.

Running tests in async

Running your Cypress tests on BrowserStack is completely async until v1.6.0. This means that when you use a CLI version that’s older than 1.6.0:

  1. 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.
  2. If you’re using Local testing, your Local connection might get closed after the command to run the tests is done executing.
  3. If you configured to get build completion notifications from your CI, you will get a notification after test submission, and not after build completion.

Polling and callback functionality will help you in all the above 3 scenarios.

Poll build status to handle build updates

Protip: 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.

You can use the build-info command using the 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. For example:

poll-build-status.sh
#!/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

Running the run command exits successfully when the test builds are successfully started on BrowserStack. Since the test execution is async in nature, you can use the callback functionality to get updates as the build is running and when the build is done running.

Protip: 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 run_settings option in browserstack.json to get an update after that build is done running. The payload POSTed to this callback URL is 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 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 run_settings.

Note: A feature-rich Slack integration is in works and will be released soon.

For example, to accept incoming webhooks in Slack, refer to this Slack guide. After you’ve setup the incoming webhook, you can use it in the run_settings as follows:

browserstack.json
{
  "run_settings": {
    ...
    "callback_url": "https://hooks.slack.com/TXXXXX/BXXXXX/XXXXXXXXXX",
    ...
  }
}

That’s it. After every build completion (success or failure), you will get a message in your selected channel with your build name, project name and a link to the test report for that build as follows:

Build Message in selected channel with test report link

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

Is this page helping you?

Yes
No

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!

Talk to an Expert
Talk to an Expert