How to Add Test Execution to a Test Plan with Xray Rest Api?

I'm trying to add a Test Execution to a Test Plan using Xray Rest API & Axios. I have already created an API key and authenticated successfully like so:

const axios = require('axios');
const { argv } = require('yargs');

const { clientId, clientSecret } = argv;

const headers = {
  Accept: 'application/json',
  'Content-Type': 'application/json',
};

const authenticateXray = async () => {
  await axios({
    method: 'post',
    url: `
    headers,
    data: {
      client_id: clientId, // my created client id goes here
      client_secret: clientSecret, // my created client secret goes here
    },
  })
    .then(res => console.log(`Xray authentication response status was: ${res.status}`)) // 200!
    .catch(e => {
      throw new Error(e.response.data.error);
    });
};

Then, I made a call & passed some param values gathered before when creating a Test Plan & Test Execution like so:

await axios({
    method: 'posts',
    url: `
    data: {
      0: createdTestExecIssue.data.id,
    },
});

However, I get this error: (node:46352) UnhandledPromiseRejectionWarning: Error: Request failed with status code 400. I was able to link a Test Execution to the Test Plan using another endpoint from Jira Rest API, but I'm looking to add it instead, check the screenshot below for reference.

2 Answers

I was able to solve this issue by passing the data differently like so:

await axios({
    ...
    ...
    data: [`${testExecutionId}`],
});

& passing the X-acpt key/value pair in the Request Header like so:

'X-acpt': `encodedCharaterGoesHere-YouNeedToretrievUsingNetworkTabInChrome`,

My request ended up like so:

await axios({
    method: 'post',
    url: `
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'X-acpt': `encodedCharaterGoesHere-YouNeedToretrievUsingNetworkTabInChrome`,
      'X-Powered-By': 'Express',
    },
    data: [`${testExecutionId}`],
  });

You can do this with the graphQL api, just replace the YourTestPlanID and yourTestExecutionId.

const response =  await axios({
    method: 'post',
    url:    '
    data:   { 
      query: 
        `mutation {
          addTestExecutionsToTestPlan(
            issueId: "YourTestPlanID",
            testExecIssueIds: ["yourTestExecutionId"]) {
                addedTestExecutions
                warning
            }
        }`
    },
    headers: { 
        Authorization:  `Bearer TOKEN obtained using the authentication api`,
        'Content-Type': 'application/json' }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest