Trigger test suite details and get build status using CI CD REST API
You can now integrate with your existing CI tools with low code automation to achieve the following objectives:
Automatically trigger low code tests upon deployment to the staging environment
Trigger critical tests on each commit to a pull request, or based on specific conditions
Incorporate low code tests into your existing sanity/regression pipeline jobs
API’s to trigger Test suite and get build status
Use REST API to trigger test suite’s and to get the build status.
Prerequisites
BrowserStack Username and Access key. You can find this in your account profile section. If you do not have an account yet, sign up for a Free Trial.
Test suite ID. You can find this on your test suite details page.
REST API details
Use the curl commands to trigger test suites and get build statuses. In the curl requests, replace the YOUR_USERNAME and YOUR_ACCESS_KEY variables with your account’s username and access-key.
Trigger test suite
Use the following curl command to trigger Low Code Automation test suites based on the TEST_SUITE_ID specified:
```bash
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" -X POST "https://lcnc-api.browserstack.com/api/v1/test-suite/TEST_SUITE_ID/run"
```
Replace TEST_SUITE_ID with the id of the test suite you want to trigger.
The response returns a JSON object, which contains the build_id that you can use to fetch the test suite execution status.
Fetch build status
Use the following curl command to fetch the status of build execution for the BUILD_ID specified:
```bash
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" -X GET "https://lcnc-api.browserstack.com/api/v1/builds/BUILD_ID/status"
```
Replace BUILD_ID with the build_id value you receive from the Trigger test suite API.
Integration with CI/CD pipelines
You can integrate with any of your existing CI/CD pipelines. Following are a some of the examples of popular CI tools which can be integrated using the REST APIs mentioned above
To integrate the provided Groovy script into your existing Jenkins pipeline, insert it within a stage. Make sure to replace placeholders with actual BrowserStack credentials.
Insert the provided script into your Bitbucket Pipelines YAML file within a step, ensuring to replace placeholders with actual BrowserStack credentials.
```bash
pipelines:
default:
- step:
name: Trigger Test Suite
script:
- apt-get update && apt-get install -y jq
- |
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -X POST https://lcnc-api.browserstack.com/api/v1/test-suite/${TEST_SUITE_ID}/run)
BUILD_ID=$(echo $response | jq -r '.build_id')
echo "Build ID: $BUILD_ID"
echo $BUILD_ID > build_id.txt
artifacts:
- build_id.txt
- step:
name: Check Build Status
script:
- apt-get update && apt-get install -y jq
- BUILD_ID=$(cat build_id.txt)
- |
buildStatus=""
retryInterval=30
while [[ "$buildStatus" != "passed" && "$buildStatus" != "failed" ]]; do
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -s https://lcnc-api.browserstack.com/api/v1/builds/$BUILD_ID/status)
echo "Response: $response"
buildStatus=$(echo $response | jq -r '.build_status')
echo "Build Status: $buildStatus"
sleep $retryInterval
done
if [[ "$buildStatus" == "passed" ]]; then
echo "Build passed"
# Add further steps for actions to be taken if build passed
elif [[ "$buildStatus" == "failed" ]]; then
echo "Build failed"
exit 1
else
echo "Build status unknown"
exit 1
```
Integrate the provided script into your Circle CI configuration file within a job, make sure to replace placeholders with actual BrowserStack credentials.
```bash
version: 2.1
jobs:
build:
docker:
- image: circleci/python:3.8
steps:
- run:
name: Trigger Test Suite
command: |
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -X POST https://lcnc-api.browserstack.com/api/v1/test-suite/${TEST_SUITE_ID}/run)
BUILD_ID=$(echo $response | jq -r '.build_id')
echo "Build ID: $BUILD_ID"
- run:
name: Check Build Status
command: |
buildStatus=""
retryInterval=30
while [[ ("$buildStatus" != "passed" && "$buildStatus" != "failed") ]]; do
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -s https://lcnc-api.browserstack.com/api/v1/builds/${BUILD_ID}/status)
buildStatus=$(echo $response | jq -r '.build_status')
sleep $retryInterval
done
if [[ "$buildStatus" == "passed" ]]; then
echo "Build passed"
# Add further steps for actions to be taken if build passed
elif [[ "$buildStatus" == "failed" ]]; then
echo "Build failed"
exit 1
else
echo "Build status unstable"
exit 1
fi
```
Integrate the provided script into your GitHub Actions workflow file within a step, ensure to replace placeholders with actual BrowserStack credentials.
```bash
name: Test and Deploy
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger Test Suite
id: trigger_test_suite
run: |
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -X POST https://lcnc-api.browserstack.com/api/v1/test-suite/${TEST_SUITE_ID}/run)
BUILD_ID=$(echo $response | jq -r '.build_id')
echo "::set-output name=build_id::$BUILD_ID"
echo "Build ID: $BUILD_ID"
- name: Check Build Status
run: |
buildStatus=""
retryInterval=30
BUILD_ID=$
while [[ ("$buildStatus" != "passed" && "$buildStatus" != "failed") ]]; do
response=$(curl -u YOUR_USERNAME:YOUR_ACCESS_KEY -s https://lcnc-api.browserstack.com/api/v1/builds/$BUILD_ID/status)
buildStatus=$(echo $response | jq -r '.build_status')
sleep $retryInterval
done
if [[ "$buildStatus" == "passed" ]]; then
echo "Build passed"
# Add further steps for actions to be taken if build passed
elif [[ "$buildStatus" == "failed" ]]; then
echo "Build failed"
exit 1
else
echo "Build status unstable"
exit 1
fi
```