cypress-testbench-reporter
Version:
A TestBench reporter for cypress
107 lines • 4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const axios = require('axios');
class TestBenchAutomation {
constructor(options) {
this.options = options;
this.session = undefined;
this.base = `${options.domain}/api`;
}
login() {
console.log("TestBenchAutomation.login()");
axios({
method: 'post',
url: `${this.base}/tenants/login/session`,
headers: { 'Content-Type': 'application/json' },
data: {
tenantName: this.options.tenantName,
force: true,
login: this.options.username,
password: this.options.password
}
})
.then(response => {
this.session = {
accessToken: response.data.sessionToken,
tenantId: response.data.tenantId,
productId: this.options.productId
};
})
.catch(error => this.logError(error));
}
logout() {
console.log(`TestBenchAutomation.logout()`);
axios({
method: 'delete',
url: `${this.base}/tenants/${this.session.tenantId}/login/session`,
headers: this.automationHeaders()
})
.then(response => this.session = undefined)
.catch(error => this.logError(error));
}
logError(error) {
if (error.response === undefined) {
console.error("Unexpected error occurred:");
console.error(error);
}
else if (error.response.data === undefined) {
console.error("Unexpected response error occurred:");
console.error(error.response);
}
else {
console.error("Error occurred:");
console.error(error.response.data);
}
}
automationUrl(suffix) {
return `${this.base}/tenants/${this.session.tenantId}/products/${this.session.productId}/automation/testCase${suffix}`;
}
automationHeaders() {
return { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.session.accessToken}` };
}
runAutomatedTest(testCase, status) {
console.log(`TestBenchAutomation.runAutomatedTest(${JSON.stringify(testCase.externalId)}}`);
axios({
method: 'post',
url: this.automationUrl(""),
headers: this.automationHeaders(),
data: testCase
})
.then(response => this.terminateAutomation(status))
.catch(error => {
if (error.response && error.response.status === 409 && this.options.closeAlreadyRunningAutomation) {
console.warn(`Warning: Terminating old automation of ${error.response.data.details.externalId} and starting automation of ${testCase.externalId}.`);
this.terminateAutomation(undefined);
this.runAutomatedTest(testCase);
}
else {
this.logError(error);
}
});
}
terminateAutomation(status) {
console.log(`TestBenchAutomation.terminateAutomation(${status}}`);
axios({
method: 'patch',
url: this.automationUrl(""),
headers: this.automationHeaders(),
data: {
executionStatus: status
},
}).catch(error => this.logError(error));
}
publishTestStepResults(testSteps) {
console.log(`TestBenchAutomation.publishTestStepResults(${JSON.stringify(testSteps.length)}}`);
for (let testStep of testSteps) {
axios({
method: 'put',
url: this.automationUrl(`/testStep/${testStep.testStepId}`),
headers: this.automationHeaders(),
data: testStep.result
})
.catch(error => this.logError(error));
}
}
}
exports.TestBenchAutomation = TestBenchAutomation;
//# sourceMappingURL=testbench.js.map