Docs/API tests
#

API tests

Your coding agent writes an API test: requests in order, values passed between them, assertions on each response, run on a sandbox and added to your suite.

Write an API test with your agent

An API test sends a few requests to your app's API in order, passes values from one response into the next request, and checks what comes back. It needs no browser. Your agent writes it as a short JSON file and runs it on a sandbox, a test copy of your app.

In the CLI, an API test is an API-only workflow with one API sequence. The commands below use those names.

Write and run an API test

  1. Find the requests to call. Your agent searches the API catalog, the list of requests Cofactor knows about for an application.

    bash
    cofactor api-catalog endpoints list --search projects --application <application id> --json
    

    Each result has an id, a method and an exampleUrl. If the request you need is missing, add it in one of three ways:

    • Run a browser test on the application. Calls the browser makes during the run are added to the catalog.

    • Import an OpenAPI description from a URL. Save this as source.json, then run the command below it.

      json
      {
        "url": "https://api.example.com/openapi.json",
        "propertyId": "<application id>",
        "formatId": "openapi",
        "formatLabel": "OpenAPI 3.1.0",
        "baseUrl": "https://api.example.com"
      }
      
      bash
      cofactor api-catalog sources create --from-file source.json
      
    • Add one request. Save {"method": "GET", "url": "https://api.example.com/projects", "propertyId": "<application id>", "name": "List projects"} as endpoint.json, then run cofactor api-catalog endpoints create --from-file endpoint.json.

    cofactor application list prints each application's id. For a long catalog, add --page and --page-size to the search.

  2. Write the sequence. Save this as seq.json. Each step names a catalog request by apiRequestId. A later step reads an earlier response with {{stepId.response.path}}, and validations assert on the response.

    json
    {
      "description": "Create a project, read it back, delete it",
      "steps": [
        {
          "stepId": "create",
          "apiRequestId": "<id of POST /projects>",
          "credentialId": null,
          "credentialTemplateName": "admin",
          "urlTemplate": "$var.ENV.api_url/projects",
          "bodyTemplate": "{\"name\": \"Project $RANDOM_STRING(6)\"}",
          "expectedStatusCodes": [201],
          "validations": [{ "left": "create.response.id", "operator": "is_set" }]
        },
        {
          "stepId": "read",
          "apiRequestId": "<id of GET /projects/:id>",
          "credentialId": null,
          "credentialTemplateName": "admin",
          "urlTemplate": "$var.ENV.api_url/projects/{{create.response.id}}",
          "expectedStatusCodes": [200],
          "validations": [{ "left": "read.response.status", "operator": "equals", "right": "active" }]
        },
        {
          "stepId": "remove",
          "apiRequestId": "<id of DELETE /projects/:id>",
          "credentialId": null,
          "credentialTemplateName": "admin",
          "urlTemplate": "$var.ENV.api_url/projects/{{create.response.id}}",
          "expectedStatusCodes": [204]
        }
      ]
    }
    

    Three details keep the test reliable:

    • Start each URL with $var.ENV.api_url. The test then calls whichever environment it runs on. A step without urlTemplate calls the address stored in the catalog.
    • credentialTemplateName is the persona that makes the call. Use a persona name from cofactor sandbox show.
    • Set expectedStatusCodes on every step. A step without it passes on any 2xx response.
  3. Save the test.

    bash
    cofactor workflow create --api-only --name "Projects API: create, read, delete" --sequence-from-file seq.json
    

    The command prints the workflow id. Use it as <workflow id> below. If the file has a mistake, the command lists each one as path [CODE] :: message. Fix them and run it again. To change a saved test, run cofactor workflow api-sequence update <workflow id> --from-file seq.json.

    An API test with three steps. Step 2 is open and lists two assertions: one checks a field equals a value, the other that a field is set.
  4. Run it on a sandbox. Claim a sandbox, then run the test on it.

    bash
    cofactor sandbox claim --application "Acme App" --wait
    cofactor workflow run <workflow id> --sandbox <sandbox id> --stream
    

    The command prints Run finished: success or Run finished: failed. A failed run prints an error that names the first failed step, such as API sequence failed at step read, and the command exits with code 1. To read what a step sent and got back, run cofactor workflow runs get <workflow id> <run id> --json to find the run step id in steps[].id. Then add --step <run step id> --api-call <stepId>, where <stepId> is the step's name in seq.json, such as read.

  5. Keep it. Add the passing test to Regression, your default suite. This changes what your default suite runs, so confirm it first. Then destroy the sandbox.

    bash
    cofactor workflow add-to-regression <workflow id>
    cofactor sandbox destroy <sandbox id>
    

Check it worked

bash
cofactor workflow runs list <workflow id> --side api
cofactor workflow list --in-regression true

The latest run reads success, and the test is in the Regression list. In the app, open E2E tests, choose the test and open its Runs tab to see each run.

The Runs tab of an API test. Two runs read Success with 3 of 3 calls.

Select a run to see each call with its method, address, status and time.

The results of one run: three GET calls with status 200, 200 and 404, each with its duration.

Go deeper