Skip to content

Commit

Permalink
fix: move worker video download to reporter (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunattam authored Nov 4, 2024
1 parent f7ca94a commit 01bb396
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 104 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-ligers-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"appwright": patch
---

fix: move worker video download to reporter
14 changes: 5 additions & 9 deletions src/fixture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import {
AppwrightConfig,
} from "../types";
import { Device } from "../device";
import { createDeviceProvider, getProviderClass } from "../providers";
import { logger } from "../logger";
import { createDeviceProvider } from "../providers";
import { WorkerInfoStore } from "./workerInfo";
import { basePath } from "../utils";

type TestLevelFixtures = {
/**
Expand Down Expand Up @@ -66,22 +64,20 @@ export const test = base.extend<TestLevelFixtures, WorkerLevelFixtures>({
if (!sessionId) {
throw new Error("Worker must have a sessionId.");
}
const providerName = (project as FullProject<AppwrightConfig>).use.device
?.provider;
const afterSession = new Date();
const workerInfoStore = new WorkerInfoStore();
await workerInfoStore.saveWorkerStartTime(
workerIndex,
sessionId,
providerName!,
beforeSession,
afterSession,
);
await use(device);
await workerInfoStore.saveWorkerEndTime(workerIndex, new Date());
await device.close();
logger.log(`Teardown for worker ${workerIndex}, will download video`);
const providerName = (project as FullProject<AppwrightConfig>).use.device
?.provider;
const providerClass = getProviderClass(providerName!);
const fileName = `worker-${workerIndex}-video`;
await providerClass.downloadVideo(sessionId, basePath(), fileName);
},
{ scope: "worker" },
],
Expand Down
34 changes: 25 additions & 9 deletions src/fixture/workerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ type TestInWorkerInfo = {
startTime: string;
};

type WorkerInfo = {
export type WorkerInfo = {
idx: number;
sessionId?: string | undefined;
providerName?: string | undefined;
startTime?:
| {
// Dates stored as ISO datetime strings
beforeAppiumSession: string;
afterAppiumSession: string;
}
| undefined;
endTime?: string | undefined;
tests: TestInWorkerInfo[];
};

Expand Down Expand Up @@ -58,29 +60,43 @@ export class WorkerInfoStore {
async saveWorkerStartTime(
idx: number,
sessionId: string,
providerName: string,
beforeAppiumSession: Date,
afterAppiumSession: Date,
) {
let info = await this.getWorkerFromDisk(idx);
const delta = {
providerName,
sessionId,
startTime: {
beforeAppiumSession: beforeAppiumSession.toISOString(),
afterAppiumSession: afterAppiumSession.toISOString(),
},
};
if (!info) {
info = {
...delta,
idx,
sessionId,
startTime: {
beforeAppiumSession: beforeAppiumSession.toISOString(),
afterAppiumSession: afterAppiumSession.toISOString(),
},
tests: [],
};
} else {
info.startTime = {
beforeAppiumSession: beforeAppiumSession.toISOString(),
afterAppiumSession: afterAppiumSession.toISOString(),
info = {
...info,
...delta,
};
}
return this.saveWorkerToDisk(idx, info);
}

async saveWorkerEndTime(idx: number, endTime: Date) {
let info = await this.getWorkerFromDisk(idx);
if (!info) {
throw new Error(`Worker info not found for idx: ${idx}`);
}
info.endTime = endTime.toISOString();
return this.saveWorkerToDisk(idx, info);
}

async saveTestStartTime(idx: number, testTitle: string, startTime: Date) {
let info = await this.getWorkerFromDisk(idx);
if (!info) {
Expand Down
Loading

0 comments on commit 01bb396

Please sign in to comment.