@qytera/xray-client v4.0.0
Node.js Xray Client
This project provides TypeScript clients to users that want to interact with the API of Xray. Supports Xray server and Xray cloud.
!WARNING The client is at a very early stage of development and may not cover all Xray interactions. Please refer to the roadmaps for full lists of supported endpoints.
Based on: https://docs.getxray.app/display/XRAY/REST+API
- v1
- Tests
GET /rest/raven/1.0/api/testGET /rest/raven/1.0/api/settings/teststatusesGET /rest/raven/1.0/api/test/{key}/testrunsGET /rest/raven/1.0/api/test/{key}/preconditionsGET /rest/raven/1.0/api/test/{key}/testsetsGET /rest/raven/1.0/api/test/{key}/testexecutionsGET /rest/raven/1.0/api/test/{key}/testplans- Test Steps
GET /rest/raven/1.0/api/settings/teststepstatusesGET /rest/raven/1.0/api/test/{testKey}/stepGET /rest/raven/1.0/api/test/{testKey}/step/{id}PUT /rest/raven/1.0/api/test/{testKey}/stepPOST /rest/raven/1.0/api/test/{testKey}/step/{id}DELETE /rest/raven/1.0/api/test/{testKey}/step/{id}GET /rest/raven/1.0/api/test/{testKey}/step/{id}/attachmentDELETE /rest/raven/1.0/api/test/{testKey}/step/{id}/attachment/{attachmentID}
- Pre-Conditions
GET /rest/raven/1.0/api/precondition/{preConditionKey}/testPOST /rest/raven/1.0/api/precondition/{preConditionKey}/testDELETE /rest/raven/1.0/api/precondition/{preConditionKey}/test/{testKey}
- Test Sets
GET /rest/raven/1.0/api/testset/{testSetKey}/testPOST /rest/raven/1.0/api/testset/{testSetKey}/testDELETE /rest/raven/1.0/api/testset/{testSetKey}/test/{testKey}
- Test Plans
GET /rest/raven/1.0/api/testplan/{testPlanKey}/testPOST /rest/raven/1.0/api/testplan/{testPlanKey}/testDELETE /rest/raven/1.0/api/testplan/{testPlanKey}/test/{testKey}GET /rest/raven/1.0/api/testplan/{testPlanKey}/testexecutionPOST /rest/raven/1.0/api/testplan/{testPlanKey}/testexecutionDELETE /rest/raven/1.0/api/testplan/{testPlanKey}/testexecution/{testExecKey}
- Test Executions
GET /rest/raven/1.0/api/testexec/{testExecKey}/testPOST /rest/raven/1.0/api/testexec/{testExecKey}/testDELETE /rest/raven/1.0/api/testexec/{testExecKey}/test/{testKey}
- Test Runs
GET /rest/raven/1.0/api/testrun/{id}/GET /rest/raven/1.0/api/testrun/PUT /rest/raven/1.0/api/testrun/{id}/GET /rest/raven/1.0/api/testrun/{id}/statusPUT /rest/raven/1.0/api/testrun/{id}/statusGET /rest/raven/1.0/api/testrun/{id}/defectPOST /rest/raven/1.0/api/testrun/{id}/defectDELETE /rest/raven/1.0/api/testrun/{id}/defect/{issueIdOrKey}GET /rest/raven/1.0/api/testrun/{id}/attachmentPOST /rest/raven/1.0/api/testrun/{id}/attachmentDELETE /rest/raven/1.0/api/testrun/{id}/attachmentDELETE /rest/raven/1.0/api/testrun/{id}/attachment/{attachmentid}GET /rest/raven/1.0/api/testrun/{id}/commentPUT /rest/raven/1.0/api/testrun/{id}/commentGET /rest/raven/1.0/api/testrun/{id}/exampleGET /rest/raven/1.0/api/testrun/{id}/stepGET /rest/raven/1.0/api/testrun/{id}/assigneePUT /rest/raven/1.0/api/testrun/{id}/assignee- Test Examples
GET /rest/raven/1.0/api/testrun/{id}/example/{exampleIndex}PUT /rest/raven/1.0/api/testrun/{id}/example/{exampleIndex}
- Test Step Results
GET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/PUT /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/GET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/statusPUT /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/statusGET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachmentPOST /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachmentDELETE /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachmentDELETE /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachment/{attachmentid}
- Test Repository
GET /rest/raven/1.0/api/testrepository/{projectKey}/foldersGET /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}/testsPUT /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}/testsGET /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}POST /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}PUT /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}DELETE /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}
- Import Execution Results
POST /rest/raven/1.0/api/import/executionPOST /rest/raven/1.0/api/import/execution/multipartPOST /rest/raven/1.0/api/import/execution/cucumberPOST /rest/raven/1.0/api/import/execution/cucumber/multipartPOST /rest/raven/1.0/api/import/execution/behavePOST /rest/raven/1.0/api/import/execution/behave/multipartPOST /rest/raven/1.0/api/import/execution/junitPOST /rest/raven/1.0/api/import/execution/junit/multipartPOST /rest/raven/1.0/api/import/execution/testngPOST /rest/raven/1.0/api/import/execution/testng/multipartPOST /rest/raven/1.0/api/import/execution/nunitPOST /rest/raven/1.0/api/import/execution/nunit/multipartPOST /rest/raven/1.0/api/import/execution/xunitPOST /rest/raven/1.0/api/import/execution/xunit/multipartPOST /rest/raven/1.0/api/import/execution/robotPOST /rest/raven/1.0/api/import/execution/robot/multipartPOST /rest/raven/1.0/api/import/execution/bundle
- Export Execution Results
GET /rest/raven/1.0/api/testrunsGET /rest/raven/1.0/api/execution/result (deprecated)
- Importing Cucumber Tests
POST /rest/raven/1.0/api/import/feature
- Exporting Cucumber Tests
GET /rest/raven/1.0/api/export/test
- Settings
GET /rest/raven/1.0/api/settings/teststatusesGET /rest/raven/1.0/api/settings/teststepstatuses
- Tests
- v2
- Test Step
GET /rest/raven/2.0/api/test/{testKey}/stepsPOST /rest/raven/2.0/api/test/{testKey}/stepsGET /rest/raven/2.0/api/test/{testKey}/steps/{stepId}PUT /rest/raven/2.0/api/test/{testKey}/steps/{stepId}DELETE /rest/raven/2.0/api/test/{testKey}/steps/{stepId}GET /rest/raven/2.0/api/test/{testKey}/steps/{stepId}/attachmentsDELETE /rest/raven/2.0/api/test/{testKey}/steps/{stepId}/attachment/{attachmentId}
- Test Run
GET /rest/raven/2.0/api/testrunGET /rest/raven/2.0/api/testrun/{id}PUT /rest/raven/2.0/api/testrun/{id}GET /rest/raven/2.0/api/testrun/{id}/customfield/{customFieldId}PUT /rest/raven/2.0/api/testrun/{id}/customfield/{customFieldId}GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}PUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/stepGET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}PUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/statusPUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/statusGET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachmentPOST /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachmentDELETE /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachmentDELETE /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachment/{attachmentId}GET /testruns
- Test Run Status
PUT /rest/raven/2.0/api/testrunstatus/reset
- Requirement Status
PUT /rest/raven/2.0/api/requirementstatus/reset
- Project
GET /rest/raven/2.0/api/project/{id}/settings/customfields/testrunsGET /rest/raven/2.0/api/project/{id}/settings/customfields/teststeps
- External Apps
GET /rest/raven/2.0/api/xraylicense
- Import
POST /rest/raven/2.0/api/import/executionPOST /rest/raven/2.0/api/import/execution/multipart
- Dataset
GET /rest/raven/2.0/api/dataset/exportPOST /rest/raven/2.0/api/dataset/import
- Test Repository
GET /rest/raven/2.0/api/testrepository/{projectKey}/folders/{folderId}
- Test Step
Based on: https://docs.getxray.app/display/XRAYCLOUD/REST+API
- v1
- Exporting Cucumber Tests
GET /api/v1/export/cucumber
- Import Execution Results
POST /api/v1/import/executionPOST /api/v1/import/execution/multipartPOST /api/v1/import/execution/cucumberPOST /api/v1/import/execution/cucumber/multipartPOST /api/v1/import/execution/junitPOST /api/v1/import/execution/junit/multipartPOST /api/v1/import/execution/nunitPOST /api/v1/import/execution/nunit/multipartPOST /api/v1/import/execution/xunitPOST /api/v1/import/execution/xunit/multipartPOST /api/v1/import/execution/testngPOST /api/v1/import/execution/testng/multipartPOST /api/v1/import/execution/robotPOST /api/v1/import/execution/robot/multipartPOST /api/v1/import/execution/behavePOST /api/v1/import/execution/behave/multipart
- Importing Tests
POST /api/v1/import/test/bulkGET /api/v1/import/test/bulk/{jobId}/status
- Importing Cucumber Tests
POST /api/v1/import/feature
- Backup
POST /api/v1/backupGET /api/v1/backup/{jobId}/statusGET /api/v1/backup/fileGET /api/v1/backup/file/attachment
- Attachments
GET /api/v1/attachments/{attachmentId}POST /api/v1/attachments
- Exporting Cucumber Tests
- v2
- Exporting Cucumber Tests
GET /api/v2/export/cucumber
- Exporting Datasets
GET /api/v2/dataset/export
- Import Execution Results
POST /api/v2/import/executionPOST /api/v2/import/execution/multipartPOST /api/v2/import/execution/cucumberPOST /api/v2/import/execution/cucumber/multipartPOST /api/v2/import/execution/junitPOST /api/v2/import/execution/junit/multipartPOST /api/v2/import/execution/nunitPOST /api/v2/import/execution/nunit/multipartPOST /api/v2/import/execution/xunitPOST /api/v2/import/execution/xunit/multipartPOST /api/v2/import/execution/testngPOST /api/v2/import/execution/testng/multipartPOST /api/v2/import/execution/robotPOST /api/v2/import/execution/robot/multipartPOST /api/v2/import/execution/behavePOST /api/v2/import/execution/behave/multipart
- Importing Tests
POST /api/v2/import/test/bulkGET /api/v2/import/test/bulk/{jobId}/status
- Importing Cucumber Tests
POST /api/v2/import/feature
- Backup
POST /api/v2/backupGET /api/v2/backup/{jobId}/statusGET /api/v2/backup/fileGET /api/v2/backup/file/attachment
- Attachments
GET /api/v2/attachments/{attachmentId}POST /api/v2/attachments
- Exporting Cucumber Tests
- GraphQL
- query
- getFolder
- getTest
- getTests
- getExpandedTest
- getExpandedTests
- getCoverableIssue
- getCoverableIssues
- getPrecondition
- getPreconditions
- getTestSet
- getTestSets
- getTestPlan
- getTestPlans
- getTestExecution
- getTestExecutions
- getTestRun
- getTestRunById
- getTestRuns
- getTestRunsById
- getStatus
- getStatuses
- getStepStatus
- getStepStatuses
- getProjectSettings
- getIssueLinkTypes
- mutation
- createFolder
- deleteFolder
- renameFolder
- moveFolder
- addTestsToFolder
- addIssuesToFolder
- removeTestsFromFolder
- removeIssuesFromFolder
- createTest
- updateTestType
- updateUnstructuredTestDefinition
- updateGherkinTestDefinition
- deleteTest
- addTestStep
- updateTestStep
- removeTestStep
- removeAllTestSteps
- addPreconditionToTest
- removePreconditionFromTest
- updateTestFolder
- updatePreconditionFolder
- addTestSetsToTest
- removeTestSetsFromTest
- addTestPlansToTest
- removeTestPlansFromTest
- addTestExecutionsToTest
- removeTestExecutionsFromTest
- createPrecondition
- updatePrecondition
- deletePrecondition
- addTestsToPrecondition
- removeTestsFromPrecondition
- createTestSet
- deleteTestSet
- addTestsToTestSet
- removeTestsFromTestSet
- createTestPlan
- deleteTestPlan
- addTestsToTestPlan
- removeTestsFromTestPlan
- addTestExecutionsToTestPlan
- removeTestExecutionsFromTestPlan
- createTestExecution
- deleteTestExecution
- addTestsToTestExecution
- removeTestsFromTestExecution
- addTestEnvironmentsToTestExecution
- removeTestEnvironmentsFromTestExecution
- resetTestRun
- updateTestRunStatus
- updateTestRunComment
- updateTestRun
- addDefectsToTestRun
- removeDefectsFromTestRun
- addEvidenceToTestRun
- removeEvidenceFromTestRun
- updateTestRunStep
- addEvidenceToTestRunStep
- removeEvidenceFromTestRunStep
- addDefectsToTestRunStep
- removeDefectsFromTestRunStep
- updateTestRunStepComment
- updateTestRunStepStatus
- updateTestRunExampleStatus
- updateIterationStatus
- setTestRunTimer
- query
Installation
Xray Server
npm install @qytera/xray-clientXray Cloud
If you don't intend to use the GraphQL endpoints:
npm install @qytera/xray-clientWith GraphQL support:
npm install @qytera/xray-client graphql graphql-tagUsage
Simply create a client instance for your Xray setup and start using the fully typed endpoints available.
Xray Server
import { XrayClientServer } from "@qytera/xray-client";
// PAT authentication.
export const serverClient = new XrayClientServer({
credentials: {
token: "jira-token",
},
url: "https://jira.company.com",
});
// Basic authentication.
export const serverClient = new XrayClientServer({
credentials: {
username: "jira-username",
password: "jira-password",
},
url: "https://jira.company.com",
});
const csvData = await serverClient.dataset.export({ testIssueKey: "PRJ-123" });
const testRun = await serverClient.testRun.getTestRun({
testExecIssueKey: "PRJ-456",
testIssueKey: "PRJ-123",
});
// ... other endpointsXray Cloud
import { XrayClientCloud } from "@qytera/xray-client";
// Xray cloud authentication.
export const cloudClient = new XrayClientCloud({
credentials: {
clientId: "xray-client-id",
clientSecret: "xray-client-secret",
path: "/api/v2/authenticate",
},
url: "https://xray.cloud.getxray.app",
});
const csvData = await cloudClient.dataset.export({ testIssueKey: "PRJ-123" });
// ... other endpointsThe GraphQL query and mutation endpoints are also available. For example, consider the following getTestRuns query:
{
getTestRuns(limit: 100, testExecIssueIds: ["XCN-2"]) {
total
limit
start
results {
test {
jira(fields: ["key"])
}
}
}
}It can easily be mapped to the following code snippet using the library:
const testRuns = await cloudClient.graphql.getTestRuns(
{ limit: 100, testExecIssueIds: ["XCN-2"] },
(testRunResults) => [
testRunResults.total,
testRunResults.limit,
testRunResults.start,
testRunResults.results((testRun) => [
testRun.test((test) => [
test.jira({ fields: ["key"] })
])
]),
]
);Version Specification
Xray server and Xray cloud both have versioned API endpoints with slightly different behaviour. By default, the clients access the latest documented version available for each endpoint.
You can switch between versions on a per-request basis:
// Xray server:
const evidence = await serverClient.testRun.evidence.getEvidence("12345"); // 2.0 endpoint
const evidence = await serverClient.testRun.evidence.v1.getEvidence("12345"); // 1.0 endpoint
// Xray cloud:
const attachment = await cloudClient.attachment.addAttachment("my-file.txt"); // v2 endpoint
const attachment = await cloudClient.attachment.v1.addAttachment("my-file.txt"); // v1 endpointCustom HTTP Agents
The clients can use custom HTTP agents in the form of custom fetch implementations. The recommended approach is to use undici, which is already included in Node.js but is not exposed.
!NOTE Make sure to install the version of
undicithat comes with the installed version of Node.js to avoid version conflicts and typing issues:npm install undici@$(node -e "console.log(process.versions.undici)")
import { ProxyAgent, fetch } from "undici";
const proxyAgent = new ProxyAgent({
token: Buffer.from("username:password").toString("base64"),
uri: "http://1.2.3.4:8765",
});
const configuration: ClientConfiguration = {
fetch: (url, init) => {
return fetch(url, { ...init, dispatcher: proxyAgent });
},
credentials: { /* ... */ },
url: ...
};Error Handling
Requests may result in HTTP error responses (e.g. 400 Bad Request, 403 Forbidden, 500 Internal Server Error) depending on the request configuration. The clients always throw errors when the actual response status does not match the expected status defined internally for each endpoint.
!NOTE Unfortunately, Xray's official documentation is not always precise about error response formats. The error responses also vary between Xray server/cloud and based on the Jira/Xray versions present. It is therefore impossible for the clients to directly model all error responses.
To handle response errors gracefully, use isResponseError to adapt possible outcomes to your Xray environment:
import { isResponseError } from "@qytera/xray-client";
try {
const data = await client.import.execution.xray(/* ... */);
// ...
} catch (error: unknown) {
if (isResponseError(error)) {
// access request/response data
console.error("request failed:", error.request.url);
console.error("request failed:", error.response.json.details);
} else {
// ...
}
}You can also easily define your own expected response error details and distinguish between them:
import { isResponseError } from "@qytera/xray-client";
try {
const data = await client.import.execution.xray(/* ... */);
// ...
} catch (error: unknown) {
if (isResponseError(error, 400)) {
console.error("Bad Request:", error.response.text);
} else if (isResponseError<{ missingPermission: string }>(error, 403)) {
console.error("Forbidden:", error.response.json.missingPermission);
} else {
console.error("Unexpected error:", error);
}
}Credits
This project was heavily inspired by jira.js.