Skip to content

Commit

Permalink
test: add speculos screenshots for failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrahman-ledger committed Feb 12, 2025
1 parent 527e63a commit 852377a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 51 deletions.
14 changes: 13 additions & 1 deletion apps/ledger-live-mobile/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { by, element, waitFor, device, web } from "detox";
import { Direction } from "detox/detox";
import { findFreePort, close as closeBridge, init as initBridge } from "./bridge/server";

import { startSpeculos, stopSpeculos, specs } from "@ledgerhq/live-common/e2e/speculos";
import {
startSpeculos,
stopSpeculos,
specs,
takeScreenshot,
} from "@ledgerhq/live-common/e2e/speculos";
import { SpeculosDevice } from "@ledgerhq/speculos-transport";
import invariant from "invariant";
import { getEnv, setEnv } from "@ledgerhq/live-env";
Expand Down Expand Up @@ -269,3 +274,10 @@ export async function deleteSpeculos(proxyPort?: number) {
}
setEnv("SPECULOS_API_PORT", 0);
}

export async function takeSpeculosScreenshot() {
for (const [, device] of speculosDevices) {
const speculosScreenshot = await takeScreenshot(device.ports.apiPort);
speculosScreenshot && allure.attachment("Speculos Screenshot", speculosScreenshot, "image/png");
}
}
36 changes: 9 additions & 27 deletions apps/ledger-live-mobile/e2e/setup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { device } from "detox";
import * as serverBridge from "./bridge/server";
import fs from "fs";
import { getState } from "expect";
import { MatcherState } from "expect";
import { format } from "date-fns";
import { launchApp, deleteSpeculos, setupEnvironment } from "./helpers";
import { launchApp, deleteSpeculos, setupEnvironment, takeSpeculosScreenshot } from "./helpers";
import { closeProxy } from "./bridge/proxy";
import { getEnv, setEnv } from "@ledgerhq/live-env";

const currentDate = new Date();
const date = format(currentDate, "MM-dd");
const directoryPath = `artifacts/${date}_LLM`;
let testFailed = false;
const broadcastOriginalValue = getEnv("DISABLE_TRANSACTION_BROADCAST");

setupEnvironment();
Expand All @@ -25,29 +20,16 @@ beforeAll(
process.env.CI ? 150000 : 300000,
);

afterEach(async () => {
if (process.env.CI) writeFile(getState(), "json", await serverBridge.getLogs());
});

afterAll(async () => {
const state = getState();
if (state.assertionCalls === 0 || state.suppressedErrors.length > 0) testFailed = true;
if (testFailed) {
await takeSpeculosScreenshot();
if (process.env.CI)
allure.attachment("App logs", await serverBridge.getLogs(), "application/json");
}
setEnv("DISABLE_TRANSACTION_BROADCAST", broadcastOriginalValue);
serverBridge.close();
closeProxy();
await deleteSpeculos();
});

const writeFile = (state: MatcherState, extension: string, data: string) => {
const time = format(currentDate, "HH-mm-ss");

const testFile = (state.testPath?.split("/").pop() || "logs").split(".")[0];
const testName = (state.currentTestName || "").replace(/[^a-z0-9]/gi, "_").toLowerCase();

const fileName = `${date}_${time}-${testFile}_${testName}.${extension}`;
const filePath = `${directoryPath}/${fileName}`;

if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
fs.writeFileSync(filePath, data, "utf-8");
return filePath;
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ export async function runAddAccountTest(currency: Currency, tmsLink: string) {
await app.account.expectOperationHistoryVisible(accountId);
await app.account.expectAddressIndex(0);
});

afterAll(async () => {
await app.common.removeSpeculos();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,5 @@ export async function runDeleteAccountTest(account: Account, tmsLink: string) {
await app.accounts.openViaDeeplink();
await app.accounts.expectAccountsNumber(0);
});

afterAll(async () => {
await app.common.removeSpeculos();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,4 @@ describe(`Ledger Sync Accounts`, () => {
await app.ledgerSync.closeDeletionSuccessPage();
await device.enableSynchronization();
});

afterAll(async () => {
await app?.common.removeSpeculos();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,5 @@ export async function runVerifyAddressTest(account: Account, tmsLink: string) {
await app.receive.expectReceivePageIsDisplayed(account.currency.ticker, account.accountName);
await app.receive.expectAddressIsCorrect(displayedAddress);
});

afterAll(async () => {
await app?.common.removeSpeculos();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@ describe(`Verify Address warnings - ${account.currency.name}`, () => {
await app.receive.expectReceivePageIsDisplayed(account.currency.ticker, account.accountName);
await app.receive.expectTronNewAddressWarning();
});

afterAll(async () => {
await app?.common.removeSpeculos();
});
});
5 changes: 2 additions & 3 deletions libs/ledger-live-common/src/e2e/speculos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,15 @@ export function containsSubstringInEvent(targetString: string, events: string[])
return result;
}

export async function takeScreenshot() {
const speculosApiPort = getEnv("SPECULOS_API_PORT");
export async function takeScreenshot(port?: number): Promise<Buffer | undefined> {
const speculosApiPort = port ?? getEnv("SPECULOS_API_PORT");
try {
const response = await axios.get(`http://127.0.0.1:${speculosApiPort}/screenshot`, {
responseType: "arraybuffer",
});
return response.data;
} catch (error) {
console.error("Error downloading speculos screenshot:", error);
throw error;
}
}

Expand Down

0 comments on commit 852377a

Please sign in to comment.