Using Ghost Inspector And CodeShip For UI And Browser Testing

3 minute read

About Ghost Inspector

Ghost Inspector lets you write and run UI and browser tests as part of your builds without configuring local servers and managing your own browsers.

By using Ghost Inspector you can easily test your UI without complex browser testing overhead.

Their documentation does a great job of providing more information, in addition to the setup instructions below.

CloudBees CodeShip Pro

Setting Your API Keys

You will need to add your Ghost Inspector API key and suite ID to your encrypted environment variables that you encrypt and include in your codeship-services.yml file.

Triggering The Suite

Next, you will need to add the following commands to a script, placed in your repository, that you will call from your codeship-steps.yml file:

- name: Ghost Inspector service: app command: ghost-inspector.sh

Inside the script, you will need the following Ghost Inspector commands:

# Execute Ghost Inspector suite via API and store results in JSON file curl "https://api.ghostinspector.com/v1/suites/$GHOST_SUITE_ID/execute/?apiKey=$GHOST_API_KEY" > ghostinspector.json # Check JSON results for failing tests if [ $(grep -c '"passing":false' ghostinspector.json) -ne 0 ]; then exit 1; else echo "Tests Passed"; fi

Advanced Testing

For a more complex setup with more granular control, Ghost Inspector recommends setting up and using an ngrok tunnel to triggering the Ghost Inspector test suite.

To do this, you will want to change the script you run from your codeship-steps.yml file to the following:

# Start our application (Command needs to be customized) node server.js & # Download ngrok and unzip wget 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 https://stedolan.github.io/jq/download/linux64/jq chmod +x jq # Intialize ngrok and open tunnel to our application (Port 3000 needs to be customized) ./ngrok authtoken $NGROK_TOKEN ./ngrok http 3000 > /dev/null & # Execute Ghost Inspector suite via API using the ngrok tunnel and store results in JSON file curl "https://api.ghostinspector.com/v1/suites/$GHOST_SUITE_ID/execute/?apiKey=$GHOST_API_KEY&startUrl=$(curl 'http://localhost:4040/api/tunnels' | ./jq -r '.tunnels[1].public_url')" > ghostinspector.json # Check JSON results for failing tests if [ $(grep -c '"passing":false' ghostinspector.json) -ne 0 ]; then exit 1; else echo "Tests Passed"; fi

CloudBees CodeShip Basic

Setting Your API Keys

You will need to add your Ghost Inspector API key and suite ID to your to your project’s environment variables.

You can do this by navigating to Project Settings and then clicking on the Environment tab.

Triggering The Suite

Once your Ghost Inspector credentials are loaded via your environment variables, you will want to run the following commands, directly or via a script, in your project’s setup commands:

# Execute Ghost Inspector suite via API and store results in JSON file curl "https://api.ghostinspector.com/v1/suites/$GHOST_SUITE_ID/execute/?apiKey=$GHOST_API_KEY" > ghostinspector.json # Check JSON results for failing tests if [ $(grep -c '"passing":false' ghostinspector.json) -ne 0 ]; then exit 1; else echo "Tests Passed"; fi

Advanced Testing

For a more complex setup with more granular control, Ghost Inspector recommends setting up and using an ngrok tunnel to triggering the Ghost Inspector test suite.

To do this, you will want to add the following code to a script in your repository that you run in your setup commands:

# Start our application (Command needs to be customized) node server.js & # Download ngrok and unzip wget 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 https://stedolan.github.io/jq/download/linux64/jq chmod +x jq # Intialize ngrok and open tunnel to our application (Port 3000 needs to be customized) ./ngrok authtoken $NGROK_TOKEN ./ngrok http 3000 > /dev/null & # Execute Ghost Inspector suite via API using the ngrok tunnel and store results in JSON file curl "https://api.ghostinspector.com/v1/suites/$GHOST_SUITE_ID/execute/?apiKey=$GHOST_API_KEY&startUrl=$(curl 'http://localhost:4040/api/tunnels' | ./jq -r '.tunnels[1].public_url')" > ghostinspector.json # Check JSON results for failing tests if [ $(grep -c '"passing":false' ghostinspector.json) -ne 0 ]; then exit 1; else echo "Tests Passed"; fi