@zondax/zemu
Advanced tools
Comparing version 0.28.0 to 0.29.0
export declare const DEFAULT_EMU_IMG = "zondax/builder-zemu@sha256:9c97b921dbba8f0a221c9419b577371540a7afb29727527857ee4de62307cf2f"; | ||
export declare const TIMEOUT = 1000; | ||
export declare const DEFAULT_MODEL = "nanos"; | ||
export declare const DEFAULT_START_DELAY = 3000; | ||
export declare const DEFAULT_START_DELAY = 20000; | ||
export declare const DEFAULT_KEY_DELAY = 250; | ||
export declare const DEFAULT_HOST = "127.0.0.1"; | ||
export declare const DEFAULT_TRANSPORT_PORT = 9998; | ||
export declare const BASE_NAME = "zemu-656d75-"; | ||
export declare const KILL_TIMEOUT = 10000; | ||
export declare const DEFAULT_START_TIMEOUT = 20000; | ||
export declare const KILL_TIMEOUT = 5000; | ||
export declare const KEYS: { | ||
@@ -11,0 +10,0 @@ NOT_PRESSED: number; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.WINDOW_X = exports.WINDOW_S = exports.KEYS = exports.KILL_TIMEOUT = exports.BASE_NAME = exports.DEFAULT_TRANSPORT_PORT = exports.DEFAULT_HOST = exports.DEFAULT_KEY_DELAY = exports.DEFAULT_START_DELAY = exports.DEFAULT_MODEL = exports.TIMEOUT = exports.DEFAULT_EMU_IMG = void 0; | ||
exports.WINDOW_X = exports.WINDOW_S = exports.KEYS = exports.KILL_TIMEOUT = exports.DEFAULT_START_TIMEOUT = exports.BASE_NAME = exports.DEFAULT_HOST = exports.DEFAULT_KEY_DELAY = exports.DEFAULT_START_DELAY = exports.DEFAULT_MODEL = exports.DEFAULT_EMU_IMG = void 0; | ||
exports.DEFAULT_EMU_IMG = 'zondax/builder-zemu@sha256:9c97b921dbba8f0a221c9419b577371540a7afb29727527857ee4de62307cf2f'; | ||
exports.TIMEOUT = 1000; | ||
exports.DEFAULT_MODEL = 'nanos'; | ||
exports.DEFAULT_START_DELAY = 3000; | ||
exports.DEFAULT_START_DELAY = 20000; | ||
exports.DEFAULT_KEY_DELAY = 250; | ||
exports.DEFAULT_HOST = '127.0.0.1'; | ||
exports.DEFAULT_TRANSPORT_PORT = 9998; | ||
exports.BASE_NAME = 'zemu-656d75-'; | ||
exports.KILL_TIMEOUT = 10000; | ||
exports.DEFAULT_START_TIMEOUT = 20000; | ||
exports.KILL_TIMEOUT = 5000; | ||
exports.KEYS = { | ||
@@ -14,0 +13,0 @@ NOT_PRESSED: 0, |
@@ -205,3 +205,3 @@ "use strict"; | ||
]; | ||
this.log("[ZEMU] Creating Container"); | ||
this.log("[ZEMU] Creating Container ".concat(this.image, " - ").concat(this.name, " ")); | ||
_a = this; | ||
@@ -243,5 +243,5 @@ return [4 /*yield*/, docker.createContainer({ | ||
return __awaiter(this, void 0, void 0, function () { | ||
var container, e_1, e_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var container, e_1, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
@@ -252,29 +252,28 @@ if (!this.currentContainer) return [3 /*break*/, 9]; | ||
this.log("[ZEMU] Stopping container"); | ||
_a.label = 1; | ||
_b.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
_b.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, container.stop({ t: 0 })]; | ||
case 2: | ||
_a.sent(); | ||
_b.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
this.log("[ZEMU] ".concat(e_1)); | ||
e_1 = _b.sent(); | ||
this.log("[ZEMU] Stopping: ".concat(e_1)); | ||
return [3 /*break*/, 4]; | ||
case 4: | ||
this.log("[ZEMU] Stopped"); | ||
_a.label = 5; | ||
_b.label = 5; | ||
case 5: | ||
_a.trys.push([5, 7, , 8]); | ||
_b.trys.push([5, 7, , 8]); | ||
return [4 /*yield*/, container.remove()]; | ||
case 6: | ||
_a.sent(); | ||
_b.sent(); | ||
return [3 /*break*/, 8]; | ||
case 7: | ||
e_2 = _a.sent(); | ||
this.log("[ZEMU] ".concat(e_2)); | ||
_a = _b.sent(); | ||
return [3 /*break*/, 8]; | ||
case 8: | ||
this.log("[ZEMU] Removed"); | ||
_a.label = 9; | ||
_b.label = 9; | ||
case 9: return [2 /*return*/]; | ||
@@ -281,0 +280,0 @@ } |
@@ -62,15 +62,9 @@ /// <reference types="node" /> | ||
private mainMenuSnapshot; | ||
private vncSession; | ||
private libElfs; | ||
private emuContainer; | ||
private transport; | ||
private containerName; | ||
constructor(elfPath: string, libElfs?: { | ||
[key: string]: string; | ||
}, host?: string, desiredTransportPort?: number, desiredSpeculosApiPort?: number); | ||
getSession(): any; | ||
static saveRGBA2Png(rect: { | ||
width: number; | ||
height: number; | ||
data: Buffer; | ||
}, filename: string): void; | ||
static LoadPng2RGB(filename: string): PNG.PNGWithMetadata; | ||
@@ -110,2 +104,3 @@ static delay(v: number): void; | ||
deleteEvents(): Promise<void>; | ||
dumpEvents(): Promise<void>; | ||
waitScreenChange(timeout?: number): Promise<void>; | ||
@@ -117,3 +112,3 @@ waitForText(text: string, timeout?: number, caseSensitive?: boolean): Promise<void>; | ||
clickBoth(filename?: string, waitForScreenUpdate?: boolean): Promise<any>; | ||
private getPortsToListen; | ||
private assignPortsToListen; | ||
} |
@@ -95,3 +95,3 @@ "use strict"; | ||
caseSensitive: false, | ||
startTimeout: 1000 | ||
startTimeout: constants_1.DEFAULT_START_TIMEOUT | ||
}; | ||
@@ -130,3 +130,2 @@ var StartOptions = /** @class */ (function () { | ||
this.mainMenuSnapshot = null; | ||
this.vncSession = null; | ||
if (this.elfPath == null) { | ||
@@ -143,17 +142,5 @@ throw new Error('elfPath cannot be null!'); | ||
}); | ||
var containerName = constants_1.BASE_NAME + rndstr.generate(5); | ||
this.emuContainer = new emulator_1["default"](this.elfPath, this.libElfs, constants_1.DEFAULT_EMU_IMG, containerName); | ||
this.containerName = constants_1.BASE_NAME + rndstr.generate(); | ||
this.emuContainer = new emulator_1["default"](this.elfPath, this.libElfs, constants_1.DEFAULT_EMU_IMG, this.containerName); | ||
} | ||
Zemu.prototype.getSession = function () { | ||
return this.vncSession; | ||
}; | ||
Zemu.saveRGBA2Png = function (rect, filename) { | ||
var png = new pngjs_1["default"].PNG({ | ||
width: rect.width, | ||
height: rect.height | ||
}); | ||
png.data = rect.data.slice(); | ||
var buffer = pngjs_1["default"].PNG.sync.write(png, { colorType: 6 }); | ||
fs_extra_1["default"].writeFileSync(filename, buffer); | ||
}; | ||
Zemu.LoadPng2RGB = function (filename) { | ||
@@ -245,15 +232,13 @@ var tmpBuffer = fs_extra_1["default"].readFileSync(filename); | ||
case 1: | ||
_c.trys.push([1, 9, , 10]); | ||
_c.trys.push([1, 8, , 9]); | ||
return [4 /*yield*/, Zemu.stopAllEmuContainers()]; | ||
case 2: | ||
_c.sent(); | ||
if (!(!this.transportPort || !this.speculosApiPort)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, this.getPortsToListen()]; | ||
return [4 /*yield*/, this.assignPortsToListen()]; | ||
case 3: | ||
_c.sent(); | ||
_c.label = 4; | ||
case 4: | ||
if (!this.transportPort || !this.speculosApiPort) { | ||
e = new Error("The Speculos API port or/and transport port couldn't be reserved"); | ||
this.log("[ZEMU] ".concat(e)); | ||
// noinspection ExceptionCaughtLocallyJS | ||
throw e; | ||
@@ -263,5 +248,5 @@ } | ||
return [4 /*yield*/, this.emuContainer.runContainer(__assign(__assign({}, this.startOptions), { transportPort: this.transportPort.toString(), speculosApiPort: this.speculosApiPort.toString() }))]; | ||
case 5: | ||
case 4: | ||
_c.sent(); | ||
this.log("Started Container"); | ||
this.log("Connecting to container"); | ||
// eslint-disable-next-liwaine func-names | ||
@@ -272,22 +257,24 @@ return [4 /*yield*/, this.connect()["catch"](function (error) { | ||
throw error; | ||
})]; | ||
case 6: | ||
}) | ||
// Captures main screen | ||
]; | ||
case 5: | ||
// eslint-disable-next-liwaine func-names | ||
_c.sent(); | ||
this.log("Get initial snapshot"); | ||
// Captures main screen | ||
this.log("Wait for start text"); | ||
return [4 /*yield*/, this.waitForText(this.startOptions.startText, this.startOptions.startTimeout, this.startOptions.caseSensitive)]; | ||
case 7: | ||
// Captures main screen | ||
case 6: | ||
_c.sent(); | ||
this.log("Get initial snapshot"); | ||
_b = this; | ||
return [4 /*yield*/, this.snapshot()]; | ||
case 7: | ||
_b.mainMenuSnapshot = _c.sent(); | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
_b.mainMenuSnapshot = _c.sent(); | ||
return [3 /*break*/, 10]; | ||
case 9: | ||
e_1 = _c.sent(); | ||
this.log("[ZEMU] ".concat(e_1)); | ||
throw e_1; | ||
case 10: return [2 /*return*/]; | ||
case 9: return [2 /*return*/]; | ||
} | ||
@@ -300,19 +287,39 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var waitDelay, transport_url, _c; | ||
var transport_url, start, connected, maxWait, currentTime, elapsed, _c, e_2; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
waitDelay = (_b = (_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.startDelay) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_START_DELAY; | ||
this.log("Wait for ".concat(waitDelay, " ms")); | ||
Zemu.delay(waitDelay); | ||
transport_url = "".concat(this.transportProtocol, "://").concat(this.host, ":").concat(this.transportPort); | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belogs to StaticTransport | ||
start = new Date(); | ||
connected = false; | ||
maxWait = (_b = (_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.startDelay) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_START_DELAY; | ||
_d.label = 1; | ||
case 1: | ||
if (!!connected) return [3 /*break*/, 6]; | ||
currentTime = new Date(); | ||
elapsed = currentTime.getTime() - start.getTime(); | ||
if (elapsed > maxWait) { | ||
throw "Timeout waiting to connect"; | ||
} | ||
Zemu.delay(100); | ||
_d.label = 2; | ||
case 2: | ||
_d.trys.push([2, 4, , 5]); | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belongs to StaticTransport | ||
// https://github.com/LedgerHQ/ledgerjs/blob/0ec9a60fe57d75dff26a69c213fd824aa321231c/packages/hw-transport-http/src/withStaticURLs.ts#L89 | ||
_c = this; | ||
return [4 /*yield*/, (0, hw_transport_http_1["default"])(transport_url).open(transport_url)]; | ||
case 1: | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belogs to StaticTransport | ||
case 3: | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belongs to StaticTransport | ||
// https://github.com/LedgerHQ/ledgerjs/blob/0ec9a60fe57d75dff26a69c213fd824aa321231c/packages/hw-transport-http/src/withStaticURLs.ts#L89 | ||
_c.transport = _d.sent(); | ||
return [2 /*return*/]; | ||
connected = true; | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
e_2 = _d.sent(); | ||
this.log("WAIT ".concat(this.containerName, " ").concat(elapsed, " - ").concat(e_2, " ").concat(transport_url)); | ||
connected = false; | ||
return [3 /*break*/, 5]; | ||
case 5: return [3 /*break*/, 1]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -373,13 +380,9 @@ }); | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
// Exponential back-off retry delay between requests | ||
(0, axios_retry_1["default"])(axios_1["default"], { retryDelay: axios_retry_1["default"].exponentialDelay }); | ||
return [4 /*yield*/, (0, axios_1["default"])({ | ||
method: 'GET', | ||
url: url, | ||
responseType: 'arraybuffer' | ||
})]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
// Exponential back-off retry delay between requests | ||
(0, axios_retry_1["default"])(axios_1["default"], { retryDelay: axios_retry_1["default"].exponentialDelay }); | ||
return [2 /*return*/, (0, axios_1["default"])({ | ||
method: 'GET', | ||
url: url, | ||
responseType: 'arraybuffer' | ||
})]; | ||
}); | ||
@@ -475,4 +478,3 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var snapshotPrefixGolden, snapshotPrefixTmp, imageIndex, filename, i, value, j, j, events; | ||
var _this = this; | ||
var snapshotPrefixGolden, snapshotPrefixTmp, imageIndex, filename, i, value, j, j; | ||
return __generator(this, function (_a) { | ||
@@ -538,8 +540,5 @@ switch (_a.label) { | ||
return [3 /*break*/, 2]; | ||
case 14: return [4 /*yield*/, this.getEvents()]; | ||
case 14: return [4 /*yield*/, this.dumpEvents()]; | ||
case 15: | ||
events = _a.sent(); | ||
if (events) { | ||
events.forEach(function (x) { return _this.log("[ZEMU] ".concat(JSON.stringify(x))); }); | ||
} | ||
_a.sent(); | ||
return [2 /*return*/, this.compareSnapshots(path, testcaseName, imageIndex)]; | ||
@@ -707,2 +706,19 @@ } | ||
}; | ||
Zemu.prototype.dumpEvents = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var events; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.getEvents()]; | ||
case 1: | ||
events = _a.sent(); | ||
if (events) { | ||
events.forEach(function (x) { return _this.log("[ZEMU] ".concat(JSON.stringify(x))); }); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Zemu.prototype.waitScreenChange = function (timeout) { | ||
@@ -745,3 +761,3 @@ if (timeout === void 0) { timeout = 5000; } | ||
Zemu.prototype.waitForText = function (text, timeout, caseSensitive) { | ||
if (timeout === void 0) { timeout = 1000; } | ||
if (timeout === void 0) { timeout = 5000; } | ||
if (caseSensitive === void 0) { caseSensitive = false; } | ||
@@ -761,3 +777,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
if (elapsed > timeout) { | ||
throw "Timeout waiting for text (".concat(text, ")"); | ||
throw "Timeout (".concat(timeout, ") waiting for text (").concat(text, ")"); | ||
} | ||
@@ -768,14 +784,11 @@ return [4 /*yield*/, this.getEvents()]; | ||
events.forEach(function (element) { | ||
if (caseSensitive) { | ||
if (element['text'].includes(text)) { | ||
found = true; | ||
} | ||
var v = element['text']; | ||
var q = text; | ||
if (!caseSensitive) { | ||
v = v.toLowerCase(); | ||
q = q.toLowerCase(); | ||
} | ||
else { | ||
if (element['text'].toLowerCase().includes(text.toLowerCase())) { | ||
found = true; | ||
} | ||
} | ||
found || (found = v === q); | ||
}); | ||
return [4 /*yield*/, Zemu.delay(500)]; | ||
return [4 /*yield*/, Zemu.delay(100)]; | ||
case 3: | ||
@@ -856,3 +869,3 @@ _a.sent(); | ||
}; | ||
Zemu.prototype.getPortsToListen = function () { | ||
Zemu.prototype.assignPortsToListen = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -862,3 +875,5 @@ var transportPort, speculosApiPort; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, (0, get_port_1["default"])({ port: this.desiredTransportPort })]; | ||
case 0: | ||
if (!(!this.transportPort || !this.speculosApiPort)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, (0, get_port_1["default"])({ port: this.desiredTransportPort })]; | ||
case 1: | ||
@@ -871,3 +886,4 @@ transportPort = _a.sent(); | ||
this.speculosApiPort = speculosApiPort; | ||
return [2 /*return*/]; | ||
_a.label = 3; | ||
case 3: return [2 /*return*/]; | ||
} | ||
@@ -874,0 +890,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"license": "Apache-2.0", | ||
"version": "0.28.0", | ||
"version": "0.29.0", | ||
"description": "Zemu Testing Framework", | ||
@@ -26,3 +26,3 @@ "main": "./dist/index.js", | ||
"test": "yarn build && jest", | ||
"linter": "eslint --ext .ts,.tsx,.js,.jsx --ignore-path .eslintignore .", | ||
"linter": "eslint --ext .ts,.tsx,.js,.jsx --ignore-path .eslintignore . --max-warnings 0", | ||
"linter:fix": "yarn linter --fix", | ||
@@ -48,3 +48,2 @@ "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"" | ||
"randomstring": "^1.2.1", | ||
"rfb2": "^0.2.2", | ||
"sleep": "^6.3.0" | ||
@@ -51,0 +50,0 @@ }, |
export const DEFAULT_EMU_IMG = 'zondax/builder-zemu@sha256:9c97b921dbba8f0a221c9419b577371540a7afb29727527857ee4de62307cf2f' | ||
export const TIMEOUT = 1000 | ||
export const DEFAULT_MODEL = 'nanos' | ||
export const DEFAULT_START_DELAY = 3000 | ||
export const DEFAULT_START_DELAY = 20000 | ||
export const DEFAULT_KEY_DELAY = 250 | ||
export const DEFAULT_HOST = '127.0.0.1' | ||
export const DEFAULT_TRANSPORT_PORT = 9998 | ||
export const BASE_NAME = 'zemu-656d75-' | ||
export const KILL_TIMEOUT = 10000 | ||
export const DEFAULT_START_TIMEOUT = 20000 | ||
export const KILL_TIMEOUT = 5000 | ||
@@ -12,0 +11,0 @@ export const KEYS = { |
@@ -179,4 +179,3 @@ /** ****************************************************************************** | ||
this.log(`[ZEMU] Creating Container`) | ||
this.log(`[ZEMU] Creating Container ${this.image} - ${this.name} `) | ||
this.currentContainer = await docker.createContainer({ | ||
@@ -220,3 +219,3 @@ Image: this.image, | ||
} catch (e) { | ||
this.log(`[ZEMU] ${e}`) | ||
this.log(`[ZEMU] Stopping: ${e}`) | ||
} | ||
@@ -226,4 +225,4 @@ this.log(`[ZEMU] Stopped`) | ||
await container.remove() | ||
} catch (e) { | ||
this.log(`[ZEMU] ${e}`) | ||
} catch { | ||
// eslint-disable-next-line no-empty | ||
} | ||
@@ -230,0 +229,0 @@ this.log(`[ZEMU] Removed`) |
125
src/index.ts
@@ -18,3 +18,2 @@ /** ****************************************************************************** | ||
import fs from 'fs-extra' | ||
import {RfbClient} from 'rfb2' | ||
import sleep from 'sleep' | ||
@@ -38,2 +37,3 @@ import getPort from 'get-port' | ||
DEFAULT_START_DELAY, | ||
DEFAULT_START_TIMEOUT, | ||
KILL_TIMEOUT, | ||
@@ -59,3 +59,3 @@ WINDOW_S, | ||
caseSensitive: false, | ||
startTimeout: 1000, | ||
startTimeout: DEFAULT_START_TIMEOUT, | ||
} | ||
@@ -99,6 +99,6 @@ | ||
private mainMenuSnapshot: null | ||
private vncSession: RfbClient | null | ||
private libElfs: { [p: string]: string } | ||
private emuContainer: EmuContainer | ||
private transport: Transport | undefined | ||
private containerName: string | ||
@@ -118,3 +118,2 @@ constructor( | ||
this.mainMenuSnapshot = null | ||
this.vncSession = null | ||
@@ -135,20 +134,6 @@ if (this.elfPath == null) { | ||
const containerName = BASE_NAME + rndstr.generate(5) | ||
this.emuContainer = new EmuContainer(this.elfPath, this.libElfs, DEFAULT_EMU_IMG, containerName) | ||
this.containerName = BASE_NAME + rndstr.generate() | ||
this.emuContainer = new EmuContainer(this.elfPath, this.libElfs, DEFAULT_EMU_IMG, this.containerName) | ||
} | ||
getSession(): any { | ||
return this.vncSession | ||
} | ||
static saveRGBA2Png(rect: { width: number; height: number; data: Buffer }, filename: string) { | ||
const png = new PNG.PNG({ | ||
width: rect.width, | ||
height: rect.height, | ||
}) | ||
png.data = rect.data.slice() | ||
const buffer = PNG.PNG.sync.write(png, {colorType: 6}) | ||
fs.writeFileSync(filename, buffer) | ||
} | ||
static LoadPng2RGB(filename: string) { | ||
@@ -214,10 +199,8 @@ const tmpBuffer = fs.readFileSync(filename) | ||
await Zemu.stopAllEmuContainers() | ||
await this.assignPortsToListen() | ||
if (!this.transportPort || !this.speculosApiPort) { | ||
await this.getPortsToListen() | ||
} | ||
if (!this.transportPort || !this.speculosApiPort) { | ||
const e = new Error("The Speculos API port or/and transport port couldn't be reserved") | ||
this.log(`[ZEMU] ${e}`) | ||
// noinspection ExceptionCaughtLocallyJS | ||
throw e | ||
@@ -233,4 +216,3 @@ } | ||
this.log(`Started Container`) | ||
this.log(`Connecting to container`) | ||
// eslint-disable-next-liwaine func-names | ||
@@ -243,6 +225,7 @@ await this.connect().catch(error => { | ||
this.log(`Get initial snapshot`) | ||
// Captures main screen | ||
this.log(`Wait for start text`) | ||
await this.waitForText(this.startOptions.startText, this.startOptions.startTimeout, this.startOptions.caseSensitive) | ||
this.log(`Get initial snapshot`) | ||
this.mainMenuSnapshot = await this.snapshot() | ||
@@ -256,13 +239,25 @@ } catch (e) { | ||
async connect() { | ||
// FIXME: Can we detect open ports? | ||
const waitDelay = this.startOptions?.startDelay ?? DEFAULT_START_DELAY | ||
const transport_url = `${this.transportProtocol}://${this.host}:${this.transportPort}` | ||
const start = new Date() | ||
let connected = false | ||
const maxWait = this.startOptions?.startDelay ?? DEFAULT_START_DELAY | ||
this.log(`Wait for ${waitDelay} ms`) | ||
Zemu.delay(waitDelay) | ||
while (!connected) { | ||
const currentTime = new Date() | ||
const elapsed: any = currentTime.getTime() - start.getTime() | ||
if (elapsed > maxWait) { | ||
throw `Timeout waiting to connect` | ||
} | ||
Zemu.delay(100) | ||
const transport_url = `${this.transportProtocol}://${this.host}:${this.transportPort}` | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belogs to StaticTransport | ||
// https://github.com/LedgerHQ/ledgerjs/blob/0ec9a60fe57d75dff26a69c213fd824aa321231c/packages/hw-transport-http/src/withStaticURLs.ts#L89 | ||
this.transport = await (TransportHttp(transport_url) as any).open(transport_url) | ||
try { | ||
// Here it should be "StaticTransport" type, in order to be able to use the static method "open". That method belongs to StaticTransport | ||
// https://github.com/LedgerHQ/ledgerjs/blob/0ec9a60fe57d75dff26a69c213fd824aa321231c/packages/hw-transport-http/src/withStaticURLs.ts#L89 | ||
this.transport = await (TransportHttp(transport_url) as any).open(transport_url) | ||
connected = true | ||
} catch (e) { | ||
this.log(`WAIT ${this.containerName} ${elapsed} - ${e} ${transport_url}`) | ||
connected = false | ||
} | ||
} | ||
} | ||
@@ -314,5 +309,5 @@ | ||
// Exponential back-off retry delay between requests | ||
axiosRetry(axios, {retryDelay: axiosRetry.exponentialDelay}) | ||
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay }) | ||
return await axios({ | ||
return axios({ | ||
method: 'GET', | ||
@@ -423,6 +418,3 @@ url: url, | ||
const events = await this.getEvents() | ||
if (events) { | ||
events.forEach((x: any) => this.log(`[ZEMU] ${JSON.stringify(x)}`)) | ||
} | ||
await this.dumpEvents() | ||
@@ -525,6 +517,6 @@ return this.compareSnapshots(path, testcaseName, imageIndex) | ||
async getEvents() { | ||
axiosRetry(axios, {retryDelay: axiosRetry.exponentialDelay}) | ||
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay }) | ||
const eventsUrl = 'http://localhost:' + this.speculosApiPort?.toString() + '/events' | ||
try { | ||
const {data} = await axios.get(eventsUrl) | ||
const { data } = await axios.get(eventsUrl) | ||
return data['events'] | ||
@@ -543,2 +535,9 @@ } catch (error) { | ||
async dumpEvents() { | ||
const events = await this.getEvents() | ||
if (events) { | ||
events.forEach((x: any) => this.log(`[ZEMU] ${JSON.stringify(x)}`)) | ||
} | ||
} | ||
async waitScreenChange(timeout = 5000) { | ||
@@ -563,3 +562,3 @@ const start = new Date() | ||
async waitForText(text: string, timeout = 1000, caseSensitive = false) { | ||
async waitForText(text: string, timeout = 5000, caseSensitive = false) { | ||
const start = new Date() | ||
@@ -572,3 +571,3 @@ let found = false | ||
if (elapsed > timeout) { | ||
throw `Timeout waiting for text (${text})` | ||
throw `Timeout (${timeout}) waiting for text (${text})` | ||
} | ||
@@ -578,13 +577,13 @@ | ||
events.forEach((element: any) => { | ||
if (caseSensitive) { | ||
if (element['text'].includes(text)) { | ||
found = true | ||
} | ||
} else { | ||
if (element['text'].toLowerCase().includes(text.toLowerCase())) { | ||
found = true | ||
} | ||
let v = element['text'] | ||
let q = text | ||
if (!caseSensitive) { | ||
v = v.toLowerCase() | ||
q = q.toLowerCase() | ||
} | ||
found ||= v === q | ||
}) | ||
await Zemu.delay(500) | ||
await Zemu.delay(100) | ||
} | ||
@@ -599,3 +598,3 @@ } | ||
const bothClickUrl = 'http://localhost:' + this.speculosApiPort?.toString() + endpoint | ||
const payload = {action: 'press-and-release'} | ||
const payload = { action: 'press-and-release' } | ||
await axios.post(bothClickUrl, payload) | ||
@@ -631,9 +630,11 @@ this.log(`Click ${endpoint} -> ${filename}`) | ||
private async getPortsToListen(): Promise<void> { | ||
const transportPort = await getPort({port: this.desiredTransportPort}) | ||
const speculosApiPort = await getPort({port: this.desiredSpeculosApiPort}) | ||
private async assignPortsToListen(): Promise<void> { | ||
if (!this.transportPort || !this.speculosApiPort) { | ||
const transportPort = await getPort({ port: this.desiredTransportPort }) | ||
const speculosApiPort = await getPort({ port: this.desiredSpeculosApiPort }) | ||
this.transportPort = transportPort | ||
this.speculosApiPort = speculosApiPort | ||
this.transportPort = transportPort | ||
this.speculosApiPort = speculosApiPort | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
// noinspection SpellCheckingInspection | ||
/** ****************************************************************************** | ||
@@ -90,4 +92,4 @@ * (c) 2020 Zondax GmbH | ||
await sim.start(ZEMU_OPTIONS_S) | ||
const result = sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot(), 2000) | ||
await expect(result).rejects.toEqual('Timeout waiting for screen to change (2000 ms)') | ||
const result = sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot(), 5000) | ||
await expect(result).rejects.toEqual('Timeout waiting for screen to change (5000 ms)') | ||
} finally { | ||
@@ -94,0 +96,0 @@ await sim.close() |
@@ -0,1 +1,3 @@ | ||
// noinspection SpellCheckingInspection | ||
/** ****************************************************************************** | ||
@@ -2,0 +4,0 @@ * (c) 2020 Zondax GmbH |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2966917
14
2642
- Removedrfb2@^0.2.2
- Removedrfb2@0.2.2(transitive)