@testwizard/core
Version:
254 lines (211 loc) • 7.5 kB
JavaScript
'use strict';
const RobotClient = require("./robotClient");
const ResultCodes = require("./resultCodes");
const BeginStepCommand = require("./commands/BeginStepCommand");
const EndStepCommand = require("./commands/EndStepCommand");
const AddCustomDataCommand = require("./commands/AddCustomDataCommand");
const AddPerformanceDataCommand = require("./commands/AddPerformanceDataCommand");
const StringCompare = require("./commands/StringCompareCommand");
const CancelCommandsCommand = require("./commands/CancelCommandsCommand");
const WaitForCommandCompletionCommand = require("./commands/WaitForCommandCompletionCommand");
class Session {
constructor(metadata, testRunId) {
this.metadata = metadata;
this.parameters = {};
if (metadata.parameters !== undefined) {
metadata.parameters.forEach(element => {
this.parameters[element.name] = element.value;
});
}
this.customProperties = {};
if (metadata.customProperties !== undefined) {
this.customProperties = metadata.customProperties;
}
if (testRunId === undefined) {
this.isSelfManaged = true;
}
else {
this.testRunId = testRunId;
this.isSelfManaged = false;
}
this.robot = new RobotClient("http://localhost:8500");
this.disposed = false;
}
async initialize() {
if (this.isSelfManaged)
this.testRunId = await this.robot.createTestRun(this.metadata);
this.isInitialized = true;
this.info = await this.robot.getSessionInfo(this.testRunId);
this.failCount = 0;
this.errorCount = 0;
this.results = new Array();
}
/**
* Pauses execution for a certain time
* @param {number} seconds The duration of the pause
*/
async sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
/**
* Adds a pass result but does not send it to the server
* @param {string} message The message for the passed result
*/
addPass(message) {
this.throwIfDisposed();
this.throwIfNotInitialized();
this.results.push({
result: ResultCodes.PASS,
comment: message
});
}
/**
* Adds a fail result but does not send it to the server
* @param {string} message The message for the failed result
*/
addFail(message) {
this.throwIfDisposed();
this.throwIfNotInitialized();
this.failCount++;
this.results.push({
result: ResultCodes.FAIL,
comment: message
});
}
/**
* Adds an error result but does not send it to the server
* @param {string} message The message for the error result
*/
addError(message) {
this.throwIfDisposed();
this.throwIfNotInitialized();
this.errorCount++;
this.results.push({
result: ResultCodes.SCRIPTERROR,
comment: message
});
}
/**
* Sends a result to the server
* @param {string} result The kind of result (see ResultCodes)
* @param {string} message The message for the result
*/
async setResult(result, message) {
this.throwIfDisposed();
this.throwIfNotInitialized();
if (result === ResultCodes.FAIL)
this.failCount++;
else if (result === ResultCodes.SCRIPTERROR)
this.errorCount++;
else if (result === ResultCodes.SYSTEMERROR)
this.errorCount++;
const requestObj = [{
result: result,
comment: message
}];
await this.robot.postTestResult(this.testRunId, requestObj);
}
/**
* Indicates if the session has a failed result
* @returns {boolean} True if the session has a failed result
*/
get hasFails() {
return this.failCount > 0;
}
/**
* Indicates if the session has an error result
* @returns {boolean} True if the session has an error result
*/
get hasErrors() {
return this.errorCount > 0;
}
async beginStep(name){
this.throwIfDisposed();
this.throwIfNotInitialized();
var command = new BeginStepCommand(this);
return await command.execute(name);
}
async endStep(result, message = null){
this.throwIfDisposed();
this.throwIfNotInitialized();
var command = new EndStepCommand(this);
return await command.execute(result, message);
}
async addCustomData(key, value){
this.throwIfDisposed();
this.throwIfNotInitialized();
var command = new AddCustomDataCommand(this);
return await command.execute(key, value);
}
async addPerformanceData(dataSetName, key, value, description){
this.throwIfDisposed();
this.throwIfNotInitialized();
var command = new AddPerformanceDataCommand(this);
return await command.execute(dataSetName, key, value, description);
}
async stringCompare(string1, string2){
this.throwIfDisposed();
this.throwIfNotInitialized();
var command = new StringCompare(this);
return await command.execute(string1, string2);
}
/**
* Cancel all running commands
*/
async cancelCommands() {
this.throwIfDisposed();
const command = new CancelCommandsCommand(this);
return await command.execute();
}
/**
* Wait for all running commands to complete (within the optional timeout or forever)
* @param {double} timeout (optional) timeout in seconds
*/
async waitForCommandCompletion(timeout) {
this.throwIfDisposed();
const command = new WaitForCommandCompletionCommand(this);
return await command.execute(timeout);
}
/**
* Closes the session after posting any results
*/
async close(status) {
if (!this.isInitialized)
return;
this.isInitialized = false;
if (this.results.length > 0)
await this.robot.postTestResult(this.testRunId, this.results);
if (status !== undefined)
await this.robot.postTestStatus(this.testRunId, status);
if (this.isSelfManaged)
await this.robot.tearDown(this.testRunId);
this.results = null;
this.testRunId = null;
this.isSelfManaged = true;
}
/**
* Checks if the session has been initialized
* @private
* @throws {Error} The session was not initialized
*/
throwIfNotInitialized() {
if (!this.isInitialized)
throw new Error('The session was not initialized');
}
/**
* Marks the session as disposed, sends results to the server that have not been send yet and ends the test run
*/
dispose() {
this.robot.dispose();
this.robot = null;
}
/**
* Checks if the object has been disposed
* @throws {Error} Cannot access a disposed object
*/
throwIfDisposed() {
if (this.disposed)
throw new Error('Cannot access a disposed session');
}
}
module.exports = Session;