Comprehensive Guide to Ghost Inspector API Automations

Introduction to Ghost Inspector

Ghost Inspector is a powerful tool that allows you to automate website and web application testing through its versatile API. With Ghost Inspector, you can create, execute, and manage tests with ease, ensuring your web projects run smoothly without manual intervention. In this blog, we will introduce you to some of the key APIs provided by Ghost Inspector and show you how to use them with code snippets and a detailed app example.

1. Create a New Test Suite

The first step to automate your testing with Ghost Inspector is to create a new test suite. You can do this by making a POST request to the /suites/ endpoint.

  
    POST https://api.ghostinspector.com/v1/suites/
    {
      "name": "My New Test Suite",
      "organization": "your-organization-id"
    }
  

2. Add a Test to a Suite

Once you have a test suite, you can add individual tests to it by making a POST request to the /suites/:suiteId/tests/ endpoint.

  
    POST https://api.ghostinspector.com/v1/suites/:suiteId/tests/
    {
      "name": "My First Test",
      "startUrl": "https://example.com"
    }
  

3. Execute a Test

To execute a test, you can use the /tests/:testId/execute/ endpoint. This initiates the test run and provides you with a test result id that you can use to track the test status.

  
    POST https://api.ghostinspector.com/v1/tests/:testId/execute/
    {
      "immediate": true
    }
  

4. Get Test Results

You can retrieve the results of a specific test execution by querying the /results/:resultId/ endpoint. This provides detailed information about the test run, including passed and failed steps.

  
    GET https://api.ghostinspector.com/v1/results/:resultId/
  

5. Example App with Ghost Inspector API

Let’s build a simple application that utilizes the Ghost Inspector API to automate the process of creating a test suite, adding tests, executing them, and retrieving the results.

  
    const axios = require('axios');

    async function createSuite(apiKey, suiteName, orgId) {
      const response = await axios.post(
        'https://api.ghostinspector.com/v1/suites/', 
        {
          name: suiteName,
          organization: orgId
        }, 
        {
          headers: { 'Authorization': `Bearer ${apiKey}` }
        }
      );
      return response.data.data._id;
    }

    async function addTest(apiKey, suiteId, testName, startUrl) {
      const response = await axios.post(
        `https://api.ghostinspector.com/v1/suites/${suiteId}/tests/`, 
        {
          name: testName,
          startUrl: startUrl
        }, 
        {
          headers: { 'Authorization': `Bearer ${apiKey}` }
        }
      );
      return response.data.data._id;
    }

    async function executeTest(apiKey, testId) {
      const response = await axios.post(
        `https://api.ghostinspector.com/v1/tests/${testId}/execute/`, 
        {
          immediate: true
        }, 
        {
          headers: { 'Authorization': `Bearer ${apiKey}` }
        }
      );
      return response.data.data;
    }

    async function getResult(apiKey, resultId) {
      const response = await axios.get(
        `https://api.ghostinspector.com/v1/results/${resultId}/`, 
        {
          headers: { 'Authorization': `Bearer ${apiKey}` }
        }
      );
      return response.data.data;
    }

    // Example Usage
    const apiKey = 'your-api-key';
    const orgId = 'your-organization-id';
    const suiteName = 'Demo Suite';
    const testName = 'Homepage Test';
    const startUrl = 'https://example.com';

    (async () => {
      const suiteId = await createSuite(apiKey, suiteName, orgId);
      const testId = await addTest(apiKey, suiteId, testName, startUrl);
      const testExecution = await executeTest(apiKey, testId);
      console.log('Test Execution ID:', testExecution._id);

      // Wait for test to complete before getting results (example only)
      setTimeout(async () => {
        const result = await getResult(apiKey, testExecution._id);
        console.log('Test Result:', result);
      }, 30000); // 30 seconds wait
    })();
  

With this example, you should be able to see how easily you can integrate Ghost Inspector into your CI/CD pipeline to automate your testing process.

Hash: 1a7683eac0f9a6ae8e8959e78e12512aceedc325743707956f50405766885c48

Leave a Reply

Your email address will not be published. Required fields are marked *