UNPKG

@testwizard/core

Version:

92 lines (66 loc) 2.81 kB
'use strict'; const path = require('path'); const RestClient = require("./restClient"); class RobotClient { constructor(baseUri) { this.client = new RestClient(baseUri); this.disposed = false; } async createTestRun(metadata) { this.throwIfDisposed(); const requestObj = { scriptFilePath: process.argv[1], workingDirectory: process.cwd(), tester: metadata.tester, parameters: metadata.parameters, resources: metadata.resources, customProperties: metadata.customProperties, outputFolder: metadata.outputFolder || this.constructOutputFolder() }; const testRun = await this.client.postJson('/api/v2/testruns', requestObj, 'Error creating session'); return testRun.id; } constructOutputFolder() { let strDate = new Date().toISOString(); strDate = strDate.replace(/-/g, ''); strDate = strDate.replace(/:/g, ''); strDate = strDate.replace(/\./g, ''); const workingDir = process.cwd(); return `${workingDir}${path.sep}Runs${path.sep}${strDate}`; } async getSessionInfo(testRunId) { this.throwIfDisposed(); return await this.client.getJson(`/api/v2/testruns/${testRunId}/info`, 'Error getting session info'); } async getTestObject(resourceId) { this.throwIfDisposed(); return await this.client.getJson('/api/v2/testobjects/' + resourceId, 'Get TestObject by id failed'); } async executeCommand(testRunId, resourceName, commandName, requestObj, errorMessagePrefix) { this.throwIfDisposed(); return await this.client.postJson(`/api/v2/testruns/${testRunId}/testobjects/${resourceName}/commands/${commandName}`, requestObj, errorMessagePrefix); } async postTestResult(testRunId, requestObj) { this.throwIfDisposed(); await this.client.post(`/api/v2/testruns/${testRunId}/result`, requestObj, 'Error setting test result'); } async postTestStatus(testRunId, status) { this.throwIfDisposed(); const requestObj = { status: status }; await this.client.post(`/api/v2/testruns/${testRunId}/status`, requestObj, 'Error setting test status'); } async tearDown(testRunId) { this.throwIfDisposed(); await this.client.delete('/api/v2/testruns/' + testRunId, undefined, 'Error tearing down session'); } dispose() { this.disposed = true; this.client = null; } throwIfDisposed() { if (this.disposed) throw new Error('Cannot access a disposed object'); } } module.exports = RobotClient;