Gitlab
The following is an example of how to automate tests in GitLab CI/CD using Niteco Performance Insights REST API. We'll focus on the /v1/tests/runtest API endpoint. This example includes a gitlab-ci.yml configuration file that will run tests whenever code is pushed to the repository, or a merge request is created.
Step 1: Create Your Test Script
First, create a test script that will call the API endpoint. You can use a tool like curl or a testing library like Axios in a Node.js script.
#!/bin/bash
# Variables
API_URL="https://perf.niteco.com/api/v1/tests/runtest"
PAGE_ID="your_page_id"
DEVICE="your_device"
LOCATION="your_location"
FORM_FACTOR="DESKTOP"
# Make the API request
response=$(curl -s -G "$API_URL" \
--data-urlencode "page_id=$PAGE_ID" \
--data-urlencode "device=$DEVICE" \
--data-urlencode "location=$LOCATION" \
--data-urlencode "formFactor=$FORM_FACTOR")
# Check the response
if [[ $(echo "$response" | jq -r '.status') == "200" ]]; then
echo "Test passed successfully!"
else
echo "Test failed!"
exit 1
fi
Make sure you configure your test variables above:
PAGE_ID: Set this to the ID of the page you want to test.
DEVICE, LOCATION, FORM_FACTOR: Modify these values according to your testing requirements.
Make sure to give this script executable permissions:
chmod +x test-script.sh
Step 2: Create a GitLab CI/CD Configuration
Create a .gitlab-ci.yml:
Create a .gitlab-ci.yml file in the root of your repository. This file will define the CI/CD pipeline.:
#!/bin/sh
# Variables for API request
API_URL="https://perf.niteco.com/api/v1/tests/runtest"
PAGE_ID="your_page_id" # Replace with your actual page ID
DEVICE="DESKTOP" # Adjust as necessary
LOCATION="your_location" # Replace with your actual location
FORM_FACTOR="DESKTOP" # Can be DESKTOP, MOBILE, etc.
# Make the API request
RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null \
-G "${API_URL}" \
--data-urlencode "page_id=${PAGE_ID}" \
--data-urlencode "device=${DEVICE}" \
--data-urlencode "location=${LOCATION}" \
--data-urlencode "formFactor=${FORM_FACTOR}")
# Check the response status
if [ "$RESPONSE" -eq 200 ]; then
echo "Tests ran successfully!"
else
echo "Tests failed with status code: $RESPONSE"
exit 1 # Prevent push if tests fail
fi
Make sure you configure your test variables above:
PAGE_ID: Set this to the ID of the page you want to test.
DEVICE, LOCATION, FORM_FACTOR: Modify these values according to your testing requirements.
Step 3: Push Your Changes
Commit and push your changes to your GitLab repository. The pipeline will automatically trigger based on your configuration.
Make a Commit:
Create a new commit in your repository and push it.
git add .
git commit -m "Add API test script and CI configuration"
git push origin your-branch
Push the Changes:
git push origin main
Step4: Monitor Pipeline Results
After pushing, navigate to your GitLab repository and check the "CI/CD" section. You should see the pipeline running. If the test passes, you’ll see a success message; if it fails, you’ll receive an error.
Updated about 1 month ago