angles-javascript-client
Version:
This is the javascript client for the Angles Dashboard. It allows you to store your test results.
193 lines • 7.99 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnglesReporterClass = void 0;
const axios_1 = __importDefault(require("axios"));
const TeamRequests_1 = require("./requests/TeamRequests");
const EnvironmentRequests_1 = require("./requests/EnvironmentRequests");
const BuildRequests_1 = require("./requests/BuildRequests");
const ExecutionRequests_1 = require("./requests/ExecutionRequests");
const ScreenshotRequests_1 = require("./requests/ScreenshotRequests");
const Build_1 = require("./models/Build");
const CreateExecution_1 = require("./models/requests/CreateExecution");
const Action_1 = require("./models/Action");
const CreateBuild_1 = require("./models/requests/CreateBuild");
const StoreScreenshot_1 = require("./models/requests/StoreScreenshot");
const StepStates_1 = require("./models/enum/StepStates");
const Step_1 = require("./models/Step");
class AnglesReporterClass {
constructor() {
this.apiConfig = {
returnRejectedPromiseOnError: true,
timeout: 10000,
baseURL: 'http://127.0.0.1:3000/rest/api/v1.0/',
headers: {
common: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
},
};
if (AnglesReporterClass._instance) {
throw new Error('Error: Instantiation failed: Use AnglesReporterClass.getInstance() instead of new.');
}
AnglesReporterClass._instance = this;
this.instantiateAxios();
}
setBaseUrl(baseUrl) {
this.apiConfig.baseURL = baseUrl;
this.instantiateAxios();
}
/**
* If the current build at a seperate point, then you can set it again by calling this function.
* @param buildId
*/
setCurrentBuild(buildId) {
if (!this.currentBuild) {
this.currentBuild = new Build_1.Build();
}
this.currentBuild._id = buildId;
}
instantiateAxios() {
this.axiosInstance = axios_1.default.create(this.apiConfig);
this.teams = new TeamRequests_1.TeamRequests(this.axiosInstance);
this.environments = new EnvironmentRequests_1.EnvironmentRequests(this.axiosInstance);
this.builds = new BuildRequests_1.BuildRequests(this.axiosInstance);
this.executions = new ExecutionRequests_1.ExecutionRequests(this.axiosInstance);
this.screenshots = new ScreenshotRequests_1.ScreenshotRequests(this.axiosInstance);
}
static getInstance() {
return AnglesReporterClass._instance;
}
/**
* Returns the single instance of the angles reporter.
*
* @param {string} [baseUrl] - Set a new base url whilst grabbing the latest instance. e.g. http://127.0.0.1:3000/rest/api/v1.0/
*/
static getInstanceWithBaseUrl(baseUrl) {
if (baseUrl !== undefined) {
AnglesReporterClass._instance.setBaseUrl(baseUrl);
}
return AnglesReporterClass._instance;
}
async startBuild(name, team, environment, component, phase) {
const createBuildRequest = new CreateBuild_1.CreateBuild();
createBuildRequest.name = name;
createBuildRequest.team = team;
createBuildRequest.environment = environment;
createBuildRequest.component = component;
if (phase)
createBuildRequest.phase = phase;
createBuildRequest.start = new Date();
return new Promise((resolve, reject) => {
this.builds.createBuild(createBuildRequest)
.then((createdBuild) => {
this.currentBuild = createdBuild;
resolve(createdBuild);
})
.catch((error) => {
reject(error);
});
});
}
addArtifacts(artifacts) {
return this.builds.addArtifacts(this.currentBuild._id, artifacts);
}
startTest(title, suite) {
this.currentExecution = new CreateExecution_1.CreateExecution();
this.currentExecution.title = title;
this.currentExecution.suite = suite;
this.currentExecution.build = this.currentBuild._id;
this.currentExecution.actions = [];
this.currentExecution.platforms = [];
this.currentAction = undefined;
}
updateTestName(title, suite) {
if (this.currentExecution) {
this.currentExecution.title = title;
if (suite)
this.currentExecution.suite = suite;
}
}
storePlatformDetails(platform) {
if (this.currentExecution) {
this.currentExecution.platforms.push(platform);
}
}
saveTest() {
return this.executions.saveExecution(this.currentExecution);
}
saveScreenshot(filePath, view, tags) {
return this.saveScreenshotWithPlatform(filePath, view, tags, undefined);
}
saveScreenshotWithPlatform(filePath, view, tags, platform) {
const path = require('path');
const storeScreenshot = new StoreScreenshot_1.StoreScreenshot();
storeScreenshot.buildId = this.currentBuild._id;
storeScreenshot.filePath = path.resolve(filePath);
storeScreenshot.view = view;
storeScreenshot.timestamp = new Date();
storeScreenshot.tags = tags;
storeScreenshot.platform = platform;
return this.screenshots.saveScreenshot(storeScreenshot);
}
compareScreenshotAgainstBaseline(screenshotId) {
return this.screenshots.getBaselineCompare(screenshotId);
}
addAction(name) {
this.currentAction = new Action_1.Action();
this.currentAction.name = name;
this.currentAction.start = new Date();
this.currentAction.steps = [];
if (this.currentExecution === undefined) {
this.startTest("Set-up", "Set-up");
}
this.currentExecution.actions.push(this.currentAction);
}
info(info) {
this.addStep('INFO', undefined, undefined, info, StepStates_1.StepStates.INFO, undefined);
}
debug(info) {
this.addStep('DEBUG', undefined, undefined, info, StepStates_1.StepStates.DEBUG, undefined);
}
infoWithScreenshot(info, screenshotId) {
this.addStep('INFO', undefined, undefined, info, StepStates_1.StepStates.INFO, screenshotId);
}
error(error) {
this.addStep('ERROR', undefined, undefined, error, StepStates_1.StepStates.ERROR, undefined);
}
errorWithScreenshot(error, screenshotId) {
this.addStep('ERROR', undefined, undefined, error, StepStates_1.StepStates.ERROR, screenshotId);
}
pass(name, expected, actual, info) {
this.addStep(name, expected, actual, info, StepStates_1.StepStates.PASS, undefined);
}
passWithScreenshot(name, expected, actual, info, screenshot) {
this.addStep(name, expected, actual, info, StepStates_1.StepStates.PASS, screenshot);
}
fail(name, expected, actual, info) {
this.addStep(name, expected, actual, info, StepStates_1.StepStates.FAIL, undefined);
}
failWithScreenshot(name, expected, actual, info, screenshotId) {
this.addStep(name, expected, actual, info, StepStates_1.StepStates.FAIL, screenshotId);
}
addStep(name, expected, actual, info, status, screenshot) {
if (this.currentAction === undefined) {
this.addAction('test-details');
}
const step = new Step_1.Step();
step.name = name;
step.actual = actual;
step.expected = expected;
step.info = info;
step.status = status;
step.timestamp = new Date();
step.screenshot = screenshot;
this.currentAction.steps.push(step);
}
}
exports.AnglesReporterClass = AnglesReporterClass;
AnglesReporterClass._instance = new AnglesReporterClass();
//# sourceMappingURL=AnglesReporter.js.map