Integration: JetBrains TeamCity

Integrate Ghost Inspector with JetBrains TeamCity

Search our documentation:

Toggle Documentation Menu

Integration: JetBrains TeamCity

Ghost Inspector test suites can be executed inside your TeamCity builds by leveraging the Ghost Inspector API. This tutorial will show you how you can run Ghost Inspector tests against an application running on your TeamCity build agent.

Note: This example assumes your build agent is running in a Linux-like environment.

Getting started

To get started, we are going to need the following:

Setting up the build

To execute a Ghost Inspector test suite within our project, we're going to add a new Command Line Build Step. Go to your project's build and select Edit Configuration Settings > Build Steps > Add Build Step. From the dropdown menu, select Command Line. From the Run: dropdown select "Custom Script".

Add Ghost Inspector step in TeamCity

Add a Step Name of "Run Ghost Inspector Test Suite" and then add the script below to the Custom Script field.

Custom script

The following scripts will handle the actual work of executing the test suite in Ghost Inspector. They will launch your application locally, set up an ngrok tunnel to that application, execute our test suite, and then poll the Ghost Inspector API for the suite results. Here we've provided examples for both Linux (bash) and Windows (PowerShell)

Note that you will need to modify the values for API_KEY, APP_PORT, GI_SUITE, and NGROK_TOKEN at the top of the script.

Linux (bash) script

################################
##### CUSTOMIZE BELOW HERE #####

API_KEY='<your-api-key>'
APP_PORT='<your-application-port>'
GI_SUITE='<your-suite-id>'
NGROK_TOKEN='<your-ngrok-auth-token>'

# Tailor this command to suit your application, *must* have the trailing '&'
node server.js &

##### CUSTOMIZE ABOVE HERE #####
################################

# Save the application process ID so we can clean up later
APP_PID=$!
echo "Local application started with PID: $APP_PID"

# Download ngrok and unzip
wget -qN https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
chmod +x ngrok

# Download JSON parser for determining ngrok tunnel
wget -qN -Ojq https://stedolan.github.io/jq/download/linux64/jq
chmod +x jq

# Intialize ngrok and open tunnel to our application
./ngrok authtoken $NGROK_TOKEN
./ngrok http $APP_PORT > /dev/null &
NGROK_PID=$!

# give ngrok a second to register URLs
sleep 5

# Grab the ngrok url to send to the API
START_URL=$(curl -s 'http://localhost:4040/api/tunnels' | ./jq -r '.tunnels[1].public_url')
echo "Using start URL: $START_URL"

# Set up a couple variables to monitor result state
STATUS='null'
SUITE_RESULT=
PASSING=

# Execute Ghost Inspector suite via API and grab the result ID
EXECUTE_URL="https://api.ghostinspector.com/v1/suites/$GI_SUITE/execute/?apiKey=$API_KEY&startUrl=$START_URL&immediate=1"
echo "Executing suite: $EXECUTE_URL"
RESULT_ID=$(curl -s $EXECUTE_URL | ./jq -r '.data._id')

# for the suite result, sleep for a few seconds if it hasn't changed
echo "Polling for suite results (ID: $RESULT_ID)"
while [ "$STATUS" = 'null' ]; do
  sleep 5
  SUITE_RESULT=$(curl -s "https://api.ghostinspector.com/v1/suite-results/$RESULT_ID/?apiKey=$API_KEY")
  STATUS=$(echo $SUITE_RESULT | ./jq -r '.data.passing')
  echo " - status: $STATUS"
done

# status has been updated, check results for "passing"
if [ "$(echo $SUITE_RESULT | ./jq -r '.data.passing')" != 'true' ]; then
  echo "Suite failed! ¯\_(ツ)_/¯"
  PASSING=1
else 
  echo "Suite passed! \o/"
  PASSING=0
fi

# clean up our processes
kill -9 $APP_PID
kill -9 $NGROK_PID

# return our passing status
exit $PASSING

 

Windows (PowerShell) script

Note: To run a PowerShell script you need to change Runner Type to PowerShell.
################################
##### CUSTOMIZE BELOW HERE #####
$API_KEY = '<your-api-key>'
$APP_PORT = '<your-application-port>'
$GI_SUITE = '<your-suite-id>'
$NGROK_TOKEN = '<your-ngrok-token>'

# Tailor this path to suit your application, currently
# looking for 'index.js' in the local directory
$app_entrypoint = "$((Get-Location).Path)\index.js"

##### CUSTOMIZE ABOVE HERE #####
################################

$app_job = Start-Job `
  -ScriptBlock {Invoke-Expression -command $args[0] } `
  -ArgumentList "node.exe $app_entrypoint"

# Download and unzip ngrok
$url = "https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-windows-386.zip"
(New-Object System.Net.WebClient).DownloadFile($url, "$(Get-Location)\ngrok.zip")

# Unzip the file to specified location
$shell_app = New-Object -com shell.application 
$zip_file = $shell_app.namespace("$(Get-Location)\ngrok.zip")
$destination = $shell_app.namespace("$(Get-Location)") 
$destination.Copyhere($zip_file.items())

# Intialize ngrok and open tunnel to our application
$ngrok_job = Start-Job `
  -ScriptBlock {Invoke-Expression -command $args[0] } `
  -ArgumentList "$((Get-Location).Path)\ngrok.exe http $APP_PORT"

# give ngrok a second to register URLs
Start-Sleep -Seconds 5

# Grab the ngrok HTTPS url to send to Ghost Inspector API
$START_URL = (Invoke-RestMethod -Uri "http://localhost:4040/api/tunnels").tunnels[1].public_url
Write-Output "Using start URL: $START_URL"

# Set up initial state
$STATUS = $null

# Execute Ghost Inspector suite via API and grab the result ID
$EXECUTE_URL = "https://api.ghostinspector.com/v1/suites/$GI_SUITE/execute/?apiKey=$API_KEY&startUrl=$START_URL&immediate=1"
Write-Output "Executing suite: $EXECUTE_URL"
$RESULT_ID = (Invoke-RestMethod -Uri "$EXECUTE_URL").data._id

# for the suite result, sleep for a few seconds if it hasn't changed
while ($STATUS -eq $null) {
  Start-Sleep -Seconds 5
  $STATUS = (Invoke-RestMethod -Uri "https://api.ghostinspector.com/v1/suite-results/$RESULT_ID/?apiKey=$API_KEY").data.passing
  Write-Output " - status: $STATUS"
}

# status has been updated, check results for "passing"
if ($STATUS) {
  Write-Output "Suite passed! \o/"
  $PASSING = 0
} else {
  Write-Output "Suite failed! :("
  $PASSING = 1
}

# clean up our jobs
Stop-Job $app_job
Remove-Job $app_job
Stop-Job $ngrok_job
Remove-Job $ngrok_job

# exit with passing status
exit $PASSING  

 

Wrapping up

From here, execute your first build by clicking Run and wait for your build status to update or check out your build log to follow status updates.