@opentap/runner-client
Version:
This is the web client for the OpenTAP Runner.
758 lines (757 loc) • 30.3 kB
JavaScript
import { BreakPoints, CommonContext, CommonSettings, DataGridControl, Image, Interaction, ListItemType, LogList, MetricsConfiguration, OnTestPlanRun, OnTestStepRun, Parameter, Result, RunStatus, SessionEvent, Setting, TestPlan, TestRun, TestStepType, TestStepValidationError, WatchDog, } from './DTOs';
import { JSONCodec } from 'nats.ws';
import { BaseClient } from './BaseClient';
export class SessionClient extends BaseClient {
constructor(baseSubject, options) {
super(baseSubject, options);
this.subscriptions = [];
}
/**
* @param sessionLogsHandler Function to be called when log list or error is received
* @param options (optional) Subscription options
* @returns Subscription object
*/
connectSessionLogs(sessionLogsHandler, options) {
return this.subscribe('SessionLogs', Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
sessionLogsHandler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const logListJson = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const logList = LogList.fromJS(logListJson);
sessionLogsHandler(logList, error);
}
catch (error) {
sessionLogsHandler(undefined, error);
}
} }));
}
/**
* @param eventHandler Function to be called when session event or error is received
* @param options (optional) Subscription options
* @returns Subscription object
*/
connectSessionEvents(eventHandler, options) {
const callback = (error, encodedMessage) => {
if (error) {
eventHandler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const sessionEventJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const sessionEvent = SessionEvent.fromJS(sessionEventJs);
eventHandler(sessionEvent, error);
}
catch (error) {
eventHandler(undefined, error);
}
};
this.subscriptions.push(this.subscribe('Events', Object.assign(Object.assign({}, options), { callback })));
this.subscriptions.push(this.subscribe('Events.*', Object.assign(Object.assign({}, options), { callback })));
}
/**
* @param resultHandler Function to be called when result or error is received
* @param testRunHandler Function to be called when test run or error is received
* @param options (optional) Subscription options
* @returns Subscription array: Result and Test Run subscriptions
*/
connectSessionResults(resultHandler, testRunHandler, options) {
return [
this.subscribe('OnResult', Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
resultHandler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const resultJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const result = Result.fromJS(resultJs);
resultHandler(result, error);
}
catch (error) {
resultHandler(undefined, error);
}
} })),
this.subscribe('OnTestRun', Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
testRunHandler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const testRunJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const testRun = TestRun.fromJS(testRunJs);
testRunHandler(testRun, error);
}
catch (error) {
testRunHandler(undefined, error);
}
} })),
];
}
/**
* @param testRunHandler Function to be called when test run or error is received
* @param options (optional) Subscription options
* @returns Subscription array: Result and Test Run subscriptions
*/
connectTestRunEvents(testRunHandler, options) {
return this.subscribe('OnTestRun', Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
testRunHandler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const testRunJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const testRun = TestRun.fromJS(testRunJs);
testRunHandler(testRun, error);
}
catch (error) {
testRunHandler(undefined, error);
}
} }));
}
/**
* Connect to listen to the TestPlanRun events for the given test plan run ID.
* @param {string} testPlanRunId
* @param {(event:OnTestPlanRun|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestPlanRunEvents(testPlanRunId, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
handler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const onTestPlanRunJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const onTestPlanRun = OnTestPlanRun.fromJS(onTestPlanRunJs);
handler(onTestPlanRun, error);
}
catch (error) {
handler(undefined, error);
}
} }));
}
/**
* Connect to listen to the test plan run logs for the given test plan run ID
* @param {string} testPlanRunId
* @param {(logList:LogList|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestPlanRunLogs(testPlanRunId, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}.Logs`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
handler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const logListJson = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const logList = LogList.fromJS(logListJson);
handler(logList, error);
}
catch (error) {
handler(undefined, error);
}
} }));
}
/**
* Connect to listen to the test plan run parameter events for the given test plan run ID
* @param {string} testPlanRunId
* @param {(event:Parameter[]|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestPlanRunParameterEvents(testPlanRunId, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}.Parameters`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
handler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const parameterListJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const parameterList = parameterListJs.map(parameterJs => Parameter.fromJS(parameterJs));
handler(parameterList, error);
}
catch (error) {
handler(undefined, error);
}
} }));
}
/**
* Connect to listen to the test step run events for the given test step and test plan run ID
* @param {string} testPlanRunId
* @param {string} testStepRunId
* @param {(testRun:OnTestStepRun|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestStepRunEvents(testPlanRunId, testStepRunId, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}.StepRun.${testStepRunId}`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
var _a;
if (error) {
handler(undefined, undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const onTestStepRunJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const onTestStepRun = OnTestStepRun.fromJS(onTestStepRunJs);
const stepRunId = (_a = encodedMessage.subject.match(/\.StepRun\.(.+)$/)) === null || _a === void 0 ? void 0 : _a[1];
handler(stepRunId, onTestStepRun, error);
}
catch (error) {
handler(undefined, undefined, error);
}
} }));
}
/**
* Connect to listen to the test step run parameters for the given test step and test plan run ID
* @param {string} testPlanRunId
* @param {string} testStepRunId
* @param {(event:Parameter[]|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestStepRunParameterEvents(testPlanRunId, testStepRunId, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}.StepRun.${testStepRunId}.Parameters`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
handler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const parameterListJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const parameterList = parameterListJs.map(parameterJs => Parameter.fromJS(parameterJs));
handler(parameterList, error);
}
catch (error) {
handler(undefined, error);
}
} }));
}
/**
* Connect to listen to the test step run results for the given test step and test plan run ID
* @param {string} testPlanRunId
* @param {string} testStepRunId
* @param {string} resultName
* @param {(result:Result|undefined,err:NatsError|Error|null)=>void} handler
* @param {SubscriptionOptions} options?
* @returns Subscription
*/
connectTestStepRunResults(testPlanRunId, testStepRunId, resultName, handler, options) {
return this.subscribe(`PlanRun.${testPlanRunId}.StepRun.${testStepRunId}.Result.${resultName}`, Object.assign(Object.assign({}, options), { callback(error, encodedMessage) {
if (error) {
handler(undefined, error);
return;
}
try {
const jsonCodec = JSONCodec();
const resultJs = jsonCodec.decode(encodedMessage === null || encodedMessage === void 0 ? void 0 : encodedMessage.data);
const result = Result.fromJS(resultJs);
handler(result, error);
}
catch (error) {
handler(undefined, error);
}
} }));
}
/**
* Unsubscibe from session events
*/
unsubscribeSessionEvents() {
var _a;
(_a = this.subscriptions) === null || _a === void 0 ? void 0 : _a.forEach(subscription => subscription.unsubscribe());
}
/**
* Retrieve session logs
* @param id (optional)
* @param level (optional)
* @param excludedSource (optional)
* @param filterText (optional)
* @param offset (optional)
* @param limit (optional)
* @return Successfully retrieved OpenTAP log messages.
*/
getSessionLogs(id, levels, excludedSources, filterText, offset, limit) {
const getSessionLogsRequest = { id, levels, excludedSources, filterText, offset, limit };
return this.request('GetSessionLogs', getSessionLogsRequest)
.then(logListJs => LogList.fromJS(logListJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve session log indexes
* @param id (optional)
* @param level (optional)
* @param excludedSource (optional)
* @param filterText (optional)
* @param searchText (optional)
* @return Logs indexes retrieved
*/
sessionLogSearch(id, levels, excludedSources, filterText, searchText) {
const getSessionSearchRequest = { id, levels, excludedSources, filterText, searchText };
return this.request('SessionLogSearch', getSessionSearchRequest).then(this.success()).catch(this.error());
}
/**
* Retrieve log sources
* @param id (optional)
* @return Logs indexes retrieved
*/
sessionLogSources(id) {
return this.request('SessionLogSources', id).then(this.success()).catch(this.error());
}
/**
* Retrieve session log counts
* @param id (optional)
* @return Logs indexes retrieved
*/
sessionLogCounts(id) {
return this.request('SessionLogCounts', id).then(this.success()).catch(this.error());
}
/**
* Retrieve log severities
* @return Log levels retrieved
*/
logLevels() {
return this.request('LogLevels').then(this.success()).catch(this.error());
}
/**
* Get status, with an optional testplan execution completion timeout
* @param {number | null | undefined} timeout (optional)
* @return RunStatus retrieved
*/
getStatus(timeout) {
return this.request('GetStatus', timeout)
.then(runStatusJs => RunStatus.fromJS(runStatusJs))
.then(this.success())
.catch(this.error());
}
/**
* Upload test plan XML
* @param xml Test Plan XML
* @return Test plan loaded. List of load errors is returned.
*/
setTestPlanXML(xml) {
return this.request('SetTestPlanXML', xml).then(this.success()).catch(this.error());
}
/**
* Retrieve loaded test plan XML
* @return Test plan retrieved
*/
getTestPlanXML() {
// Generate a unique subject where the Runner will publish the chunks
return this.request('GetTestPlanXML').then(this.success()).catch(this.error());
}
/**
* Downloads the resource from the runner and returns it as raw bytes
* @return Raw bytes representing the resource
*/
downloadResource(resource) {
var _a;
const runnerResourcePrefix = 'subject://';
if (!((_a = resource.source) === null || _a === void 0 ? void 0 : _a.startsWith(runnerResourcePrefix))) {
return Promise.reject('The source of the provided resource is not a nats subject.');
}
const subject = resource.source.slice(runnerResourcePrefix.length);
return this.request(subject, undefined, { rawResponse: true, fullSubject: true })
.then(this.success())
.catch(this.error());
}
/**
* Load test plan using a test plan TapPackage from a repository
* @param {RepositoryPackageDefinition} packageReference
* @return Test plan loaded. List of load errors is returned.
*/
loadTestPlanFromRepository(packageReference) {
return this.request('LoadTestPlanFromRepository', packageReference).then(this.success()).catch(this.error());
}
/**
* Save currently loaded test plan to a repository
* @param {RepositoryPackageDefinition} packageReference
* @return Test plan uploaded.
*/
saveTestPlanToRepository(packageReference) {
return this.request('SaveTestPlanToRepository', packageReference).then(this.success()).catch(this.error());
}
/**
* Test plan resources opened
* @return Test plan resources opened.
*/
resourcesOpen() {
return this.request('ResourcesOpen')
.then(testPlanJs => TestPlan.fromJS(testPlanJs))
.then(this.success())
.catch(this.error());
}
/**
* Test plan resources closed
* @return Test plan resources closed.
*/
resourcesClose() {
return this.request('ResourcesClose')
.then(testPlanJs => TestPlan.fromJS(testPlanJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve test plan or test step settings
* @param {string} contextId
* @return List of settings retrieved
*/
getSettings(contextId) {
return this.request('GetSettings', contextId)
.then(settingArrayJs => settingArrayJs.map(settingJs => Setting.fromJS(settingJs)))
.then(this.success())
.catch(this.error());
}
/**
* Change test plan or test step settings
* @param {string} contextId
* @param {Setting[]} settings
* @return Settings changed
*/
setSettings(contextId, settings) {
const setSettingsRequest = { contextId, settings };
return this.request('SetSettings', setSettingsRequest)
.then(settingArrayJs => settingArrayJs.map(settingJs => Setting.fromJS(settingJs)))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve test plan structure
* @param {string[] | null | undefined} properties
* @return Test plan retrieved
*/
getTestPlan(properties) {
return this.request('GetTestPlan', properties)
.then(testPlanJs => TestPlan.fromJS(testPlanJs))
.then(this.success())
.catch(this.error());
}
/**
* Change test plan structure
* @param {TestPlan} plan
* @return Test plan changed
*/
setTestPlan(plan) {
return this.request('SetTestPlan', plan)
.then(testPlanJs => TestPlan.fromJS(testPlanJs))
.then(this.success())
.catch(this.error());
}
/**
* Set the name of the test plan
* @param {string} testPlanName
* @return {Promise<void>}
*/
setTestPlanName(testPlanName) {
return this.request('SetTestPlanName', testPlanName).then(this.success()).catch(this.error());
}
/**
* Retrieve all validation errors present in the test plan
* @return Retrieved validation errors for loaded TestPlan
*/
getValidationErrors() {
return this.request('GetValidationErrors')
.then(testStepValidationErrorArrayJs => testStepValidationErrorArrayJs.map(testStepValidationErrorJs => TestStepValidationError.fromJS(testStepValidationErrorJs)))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve or change common test step settings
* @param {CommonSettings} commonSettings
* @return Common test step settings retrieved or changed
*/
commonStepSettings(commonSettings) {
return this.request('CommonStepSettings', commonSettings)
.then(commonSettingsJs => CommonSettings.fromJS(commonSettingsJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve or invoke common test step settings context menu items
* @param {string} propertyName
* @param {TestPlan} commonContext
* @return Get context menu (right-click menu) for a property in the testplan or teststep
*/
commonStepSettingsContextMenu(propertyName, commonContext) {
const commonStepSettingsContextRequest = { propertyName, commonContext };
return this.request('CommonStepSettingsContextMenu', commonStepSettingsContextRequest)
.then(commonContextJs => CommonContext.fromJS(commonContextJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve all pending user input IDs
* @return Retrieved pending user inputs
*/
getUserInputs() {
return this.request('GetUserInputs').then(this.success()).catch(this.error());
}
/**
* Retrieve pending user input based on ID
* @param {string} id
* @return Retrieved pending user input
*/
getUserInput(id) {
return this.request('GetUserInput', id)
.then(interactionJs => Interaction.fromJS(interactionJs))
.then(this.success())
.catch(this.error());
}
/**
* Modify or answer pending user input based on ID
* @param interaction
* @returns {Promise<Interaction>}
*/
setUserInput(interaction) {
return this.request('SetUserInput', interaction)
.then(interactionJs => Interaction.fromJS(interactionJs))
.then(this.success())
.catch(this.error());
}
/**
* Property context menu
* @param {string} contextId
* @param {string | null} propertyName
* @return Get context menu (right-click menu) for a property in the testplan or teststep
*/
getContextMenu(contextId, propertyName) {
const propertyReferenceRequest = {
contextId,
propertyName,
};
return this.request('GetContextMenu', propertyReferenceRequest)
.then(settingArrayJs => settingArrayJs.map(settingJs => Setting.fromJS(settingJs)))
.then(this.success())
.catch(this.error());
}
/**
* Property context menu
* @param {string} contextId
* @param {string | null} propertyName
* @param {Setting[]} contextMenu
* @return Set context menu (right-click menu) for a property in the testplan or teststep
*/
setContextMenu(contextId, propertyName, contextMenu) {
const setContextMenuRequest = {
contextId,
propertyName,
contextMenu,
};
return this.request('SetContextMenu', setContextMenuRequest)
.then(settingArrayJs => settingArrayJs.map(settingJs => Setting.fromJS(settingJs)))
.then(this.success())
.catch(this.error());
}
/**
* Get data grid
* @param {string} contextId
* @param {string | null} propertyName
* @return DataGridControl retrieved
*/
getDataGrid(contextId, propertyName) {
const propertyReferenceRequest = {
contextId,
propertyName,
};
return this.request('GetDataGrid', propertyReferenceRequest)
.then(dataGridControlJs => DataGridControl.fromJS(dataGridControlJs))
.then(this.success())
.catch(this.error());
}
/**
* Set data grid
* @return Set data grid
*/
setDataGrid(contextId, propertyName, dataGridControl) {
const setDataGridRequest = { contextId, propertyName, dataGridControl };
return this.request('SetDataGrid', setDataGridRequest)
.then(dataGridControlJs => DataGridControl.fromJS(dataGridControlJs))
.then(this.success())
.catch(this.error());
}
/**
* Add item type to data grid
* @param {string} contextId
* @param {string | null} propertyName
* @param {string | null} typeName
* @return DataGridControl retrieved
*/
addDataGridItemType(contextId, propertyName, typeName) {
const addDataGridItemTypeRequest = {
contextId,
propertyName,
typeName,
};
return this.request('AddDataGridItemType', addDataGridItemTypeRequest)
.then(dataGridControlJs => DataGridControl.fromJS(dataGridControlJs))
.then(this.success())
.catch(this.error());
}
/**
* Add item to data grid
* @param {string} contextId
* @param {string | null} propertyName
* @return DataGridControl retrieved
*/
addDataGridItem(contextId, propertyName) {
const propertyReferenceRequest = {
contextId,
propertyName,
};
return this.request('AddDataGridItem', propertyReferenceRequest)
.then(dataGridControlJs => DataGridControl.fromJS(dataGridControlJs))
.then(this.success())
.catch(this.error());
}
/**
* Get item types available in the data grid
* @param {string} contextId
* @param {string | null} propertyName
* @return List of ListItemType retrieved
*/
getDataGridTypes(contextId, propertyName) {
const propertyReferenceRequest = {
contextId,
propertyName,
};
return this.request('GetDataGridTypes', propertyReferenceRequest)
.then(listItemTypeArrayJs => listItemTypeArrayJs.map(listItemTypeJs => ListItemType.fromJS(listItemTypeJs)))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve static available step type information.
* @return StepTypes retrieved
*/
getStepTypes() {
return this.request('GetStepTypes')
.then(testStepTypeArrayJs => testStepTypeArrayJs.map(testStepTypeJs => TestStepType.fromJS(testStepTypeJs)))
.then(this.success())
.catch(this.error());
}
/**
* Pause test plan execution at next possible test step
*/
setPauseNext() {
return this.request('SetPauseNext').then(this.success()).catch(this.error());
}
/**
* Retrieve breakpoints set in test plan
*/
getBreakpoints() {
return this.request('GetBreakpoints')
.then(breakPointsJs => BreakPoints.fromJS(breakPointsJs))
.then(this.success())
.catch(this.error());
}
/**
* Set breakpoints in test plan
* @param {BreakPoints} breakPointsDto
* @return BreakPoints retrieved
*/
setBreakpoints(breakPointsDto) {
return this.request('SetBreakpoints', breakPointsDto)
.then(breakPointsJs => BreakPoints.fromJS(breakPointsJs))
.then(this.success())
.catch(this.error());
}
/**
* Jump test plan execution to step. Execution state must be 'breaking'.
* @param {string} stepId
*/
setJumpToStep(stepId) {
return this.request('SetJumpToStep', stepId).then(this.success()).catch(this.error());
}
/**
* Execute test plan with attaching metadata
* @param {Parameter[]} parameters
* @return RunStatus retrieved
*/
runTestPlan(parameters) {
return this.request('RunTestPlan', parameters)
.then(runStatusJs => RunStatus.fromJS(runStatusJs))
.then(this.success())
.catch(this.error());
}
/**
* Abort test plan execution
*/
abortTestPlan() {
return this.request('AbortTestPlan').then(this.success()).catch(this.error());
}
/**
* Shuts down session
*/
shutdown() {
return this.request('Shutdown').then(this.success()).catch(this.error());
}
/**
* Retrieves installed packages in this session
*/
getImage() {
return this.request('GetImage')
.then(imageJs => Image.fromJS(imageJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieves session readiness
*/
getReadiness() {
return this.request('GetReadiness').then(this.success()).catch(this.error());
}
/**
* Retrieves watchdog
*/
getWatchDog() {
return this.request('GetWatchDog')
.then(watchDogJs => WatchDog.fromJS(watchDogJs))
.then(this.success())
.catch(this.error());
}
/**
* Sets a new watchdog
* @param {WatchDog} watchDog
*/
setWatchDog(watchDog) {
return this.request('SetWatchDog', watchDog)
.then(watchDogJs => WatchDog.fromJS(watchDogJs))
.then(this.success())
.catch(this.error());
}
/**
* Retrieve settings types used in creating a Settings TapPackage
*/
getSettingsTypes() {
return this.request('GetSettingsTypes').then(this.success()).catch(this.error());
}
/**
* Get the metric information
* @returns Promise
*/
getMetricsConfiguration() {
return this.request('GetMetricsConfiguration')
.then(metricsRequestJs => MetricsConfiguration.fromJS(metricsRequestJs))
.then(this.success())
.catch(this.error());
}
/**
* Set the metric information. Used for setting up which metrics should be published for polling.
* @param {IMetricsConfiguration} metricsRequest
* @returns Promise
*/
setMetricsConfiguration(metricsRequest) {
return this.request('SetMetricsConfiguration', metricsRequest).then(this.success()).catch(this.error());
}
}