askui
Version:
Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on
139 lines (138 loc) • 6.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.UiControllerClient = void 0;
const ws_1 = __importDefault(require("ws"));
const runner_protocol_1 = require("../core/runner-protocol");
const logger_1 = require("../lib/logger");
const ui_controller_client_connection_state_1 = require("./ui-controller-client-connection-state");
const read_recording_response_stream_handler_1 = require("./read-recording-response-stream-handler");
const ui_controller_client_error_1 = require("./ui-controller-client-error");
const ui_controller_not_connected_error_1 = require("./ui-controller-not-connected-error");
class UiControllerClient {
constructor(url) {
this.url = url;
this.connectionState = ui_controller_client_connection_state_1.UiControllerClientConnectionState.NOT_CONNECTED;
this.currentReject = UiControllerClient.EMPTY_REJECT;
this.currentResolve = UiControllerClient.EMPTY_RESOLVE;
}
clearResponse() {
this.currentReject = UiControllerClient.EMPTY_REJECT;
this.currentResolve = UiControllerClient.EMPTY_RESOLVE;
}
onMessage(data) {
logger_1.logger.debug('onMessage');
clearTimeout(this.timeout);
this.timeout = undefined;
const response = JSON.parse(data.toString());
if (response.data.error) {
logger_1.logger.error(response.data.error);
this.currentReject(new ui_controller_client_error_1.UiControllerClientError(response.data.error));
this.clearResponse();
return;
}
this.currentResolve(response);
if (response.msgName === 'READ_RECORDING_PART_RESPONSE') {
return;
}
this.clearResponse();
}
connect() {
this.connectionState = ui_controller_client_connection_state_1.UiControllerClientConnectionState.CONNECTING;
return new Promise((resolve, reject) => {
try {
this.ws = new ws_1.default(this.url);
this.ws.on('message', (data) => {
this.onMessage(data);
});
this.ws.on('open', () => {
this.connectionState = ui_controller_client_connection_state_1.UiControllerClientConnectionState.CONNECTED;
resolve(this.connectionState);
});
this.ws.on('error', (error) => {
this.connectionState = ui_controller_client_connection_state_1.UiControllerClientConnectionState.ERROR;
reject(new ui_controller_client_error_1.UiControllerClientError(`Connection to UI Controller cannot be established,
Probably it was not started. Make sure you started UI Controller with this
Url ${this.url}. Cause: ${error}`));
});
}
catch (error) {
reject(new ui_controller_client_error_1.UiControllerClientError(`Connection to UI Controller cannot be established. Cause: ${error}`));
}
});
}
disconnect() {
var _a;
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
}
checkConnection() {
if (this.connectionState !== ui_controller_client_connection_state_1.UiControllerClientConnectionState.CONNECTED) {
throw new ui_controller_not_connected_error_1.UiControllerNotConnectedError();
}
}
sendAndReceive(msg, requestTimeout = UiControllerClient.REQUEST_TIMEOUT_IN_MS) {
this.checkConnection();
return new Promise((resolve, reject) => {
logger_1.logger.debug(`sendAndReceive - ${JSON.stringify(msg)}`);
this.currentResolve = resolve;
this.currentReject = reject;
try {
this.send(msg, requestTimeout);
if (this.timeout) {
throw new ui_controller_client_error_1.UiControllerClientError(`Clear the current request before setting a new one. Check for missing await. Error: ${JSON.stringify(msg)}`);
}
this.timeout = setTimeout(() => this.currentReject(new ui_controller_client_error_1.UiControllerClientError('Request to UI Controller timed out. It seems that the UI Controller is not running. Please, make sure that it is running when executing tests.')), UiControllerClient.REQUEST_TIMEOUT_IN_MS);
}
catch (error) {
this.currentReject(new ui_controller_client_error_1.UiControllerClientError(`The communication to the UI Controller is broken. Cause: ${error}`));
}
});
}
send(msg, _requestTimeout = UiControllerClient.REQUEST_TIMEOUT_IN_MS) {
this.checkConnection();
if (!this.currentReject || !this.currentResolve) {
throw new ui_controller_client_error_1.UiControllerClientError('Request is not finished! It is not possible to have multiple requests at the same time.');
}
logger_1.logger.debug(`Send: ${JSON.stringify(msg.msgName)}`);
this.ws.send(JSON.stringify(msg));
}
requestScreenshot() {
return this.sendAndReceive(new runner_protocol_1.CaptureScreenshotRequest());
}
getStartingArguments() {
return this.sendAndReceive(new runner_protocol_1.GetStartingArgumentsRequest());
}
getServerPid() {
return this.sendAndReceive(new runner_protocol_1.GetProcessPidRequest());
}
startVideoRecording() {
return this.sendAndReceive(new runner_protocol_1.StartRecordingRequest());
}
stopVideoRecording() {
return this.sendAndReceive(new runner_protocol_1.StopRecordingRequest());
}
readVideoRecording() {
return new Promise((resolve, reject) => {
const readRecordingStreamHandler = new read_recording_response_stream_handler_1.ReadRecordingResponseStreamHandler(resolve, reject);
this.currentResolve = readRecordingStreamHandler.onMessage.bind(readRecordingStreamHandler);
this.currentReject = readRecordingStreamHandler.onError.bind(readRecordingStreamHandler);
this.send(new runner_protocol_1.ReadRecordingRequest(), 60 * 15 * 1000);
});
}
annotateInteractively(boundingBoxes, imageString) {
return new Promise((resolve, reject) => {
this.currentResolve = resolve;
this.currentReject = reject;
this.send(new runner_protocol_1.InteractiveAnnotationRequest(boundingBoxes, imageString), 60 * 60 * 1000);
});
}
requestControl(controlCommand) {
return this.sendAndReceive(new runner_protocol_1.ControlRequest(controlCommand));
}
}
exports.UiControllerClient = UiControllerClient;
UiControllerClient.EMPTY_REJECT = (_reason) => { };
UiControllerClient.EMPTY_RESOLVE = (_value) => { };
UiControllerClient.REQUEST_TIMEOUT_IN_MS = 30000;