TestProject Forum
Powered by leading experts in the test automation community

How to run jobs with testproject api?

I want to run my tests from the testproject api for my CI build. How can I do it?

Hi anouptrip,

This is actually a pretty good question :slight_smile:
You can utilize our API to create scripts that will trigger your jobs as part of your CI build.

First, you need to assign your tests into jobs on TestProject (a job is basically a container that is capable of running multiple tests at any time that you want). Then, you can follow this script to trigger multiple jobs from your account:

require('es6-promise').polyfill();
require('isomorphic-fetch');

const projectId = process.argv[2];
const apiKey = process.argv[3];
const agentId = process.argv[4]
const jobCount = process.argv[5];
const apiUrl = "https://api.testproject.io/";
let executionId = {};

async function triggerJobs() {
    for (i = 0; i < jobCount; i++) {
        let res = await runJob(process.argv[i + 6]);
    }
    console.log("Done");
}

async function runJob(jobId) {
    try {
        let res = await fetch (apiUrl + `v2/projects/${projectId}/jobs/${jobId}/run`, {
            headers: {
                "accept": "application/json",
                "Authorization": apiKey,
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                "agentId": agentId,
                "browsers": [
                  "Chrome"
                ],
                "queue": true
            }),
            method: "POST"
            })
    
        let data = await res.json();
        executionId[jobId] = data.id;
        await new Promise((resolve, reject) => {
            checkAndWaitFunc(jobId, resolve, reject);
          });

    } catch (err) {
        console.error(err);
        return null;
    }  
}

function checkAndWaitFunc(jobId, resolve, reject) {
    setTimeout(async () => {
      try {
        let state = await checkJobState(jobId, executionId[jobId]);

        if (state === "Passed" || state === "Aborted" || state === "Failed") {
            resolve();
            return;
        }
        checkAndWaitFunc(jobId, resolve, reject);

        } catch (err) {
            reject();
            return;
        }
    }, 5000);
}

async function checkJobState(jobId, executionId) {
    try {
        let res = await fetch(apiUrl + `v2/projects/${projectId}/jobs/${jobId}/executions/${executionId}/state`, {
            headers: {
                "accept": "application/json",
                "Authorization": apiKey
            },
            method: "GET"
        })

        let json = await res.json();
        console.log(json.state + " => jobID: " + jobId);
        return json.state;

    } catch (err) {
        console.error(err);
        return null;
    }
}

triggerJobs();

To execute this script, you need to type this command:
node triggerJobs.js {projectID} {apiKey} {agentID} {jobCount} {jobID_1} {jobID_2} {jobID_3}

** Replace each parameter with its actual value (delete the curly braces)**
** You can use more than three jobs, just add their IDs at the end **

Let me know if it helped you!

Kind regards,
Amit