Skip to content

Commit b700d9a

Browse files
committed
fix: take screenshot after pilot errors
1 parent f1f5b32 commit b700d9a

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

src/steps/wrapWithSteps.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export interface WrapWithStepsOptions {
1515
videoManager?: VideoManager;
1616
}
1717

18+
interface WrapWithScreenshotTakingOptions {
19+
// eslint-disable-next-line @typescript-eslint/ban-types
20+
method: Function;
21+
screenshots?: ScreenshotHelper;
22+
allure: AllureRuntime;
23+
}
24+
1825
export function wrapWithSteps(options: WrapWithStepsOptions) {
1926
const { detox, worker, allure, logs, screenshots, videoManager } = options;
2027
const { device } = detox;
@@ -114,7 +121,7 @@ function wrapDeviceMethod(
114121
};
115122
}
116123

117-
function wrapPilotMethod({ detox, allure }: WrapWithStepsOptions) {
124+
function wrapPilotMethod({ detox, allure, screenshots }: WrapWithStepsOptions) {
118125
const pilot = (detox as any).pilot;
119126
const originalInit = pilot?.init;
120127
if (typeof originalInit !== 'function') return;
@@ -127,12 +134,46 @@ function wrapPilotMethod({ detox, allure }: WrapWithStepsOptions) {
127134
instance.performStep = allure.createStep(
128135
'{{0}}',
129136
[null],
130-
instance.performStep.bind(instance),
137+
wrapWithScreenshotTaking({
138+
method: instance.performStep.bind(instance),
139+
screenshots,
140+
allure,
141+
}),
131142
);
132143
}
133144
if (typeof instance?.autopilot === 'function') {
134-
instance.autopilot = allure.createStep('{{0}}', [null], instance.autopilot.bind(instance));
145+
instance.autopilot = allure.createStep(
146+
'{{0}}',
147+
[null],
148+
wrapWithScreenshotTaking({
149+
method: instance.autopilot.bind(instance),
150+
screenshots,
151+
allure,
152+
}),
153+
);
135154
}
136155
return result;
137156
};
138157
}
158+
159+
function wrapWithScreenshotTaking({
160+
method,
161+
screenshots,
162+
allure,
163+
}: WrapWithScreenshotTakingOptions) {
164+
if (!screenshots || !allure) {
165+
return method;
166+
}
167+
168+
return async (...args: any[]) => {
169+
try {
170+
return await method(...args);
171+
} catch (error) {
172+
if (`${error}`.startsWith('Error:')) {
173+
await screenshots?.attachFailure(allure);
174+
}
175+
176+
throw error;
177+
}
178+
};
179+
}

0 commit comments

Comments
 (0)