@@ -15,6 +15,13 @@ export interface WrapWithStepsOptions {
15
15
videoManager ?: VideoManager ;
16
16
}
17
17
18
+ interface WrapWithScreenshotTakingOptions {
19
+ // eslint-disable-next-line @typescript-eslint/ban-types
20
+ method : Function ;
21
+ screenshots ?: ScreenshotHelper ;
22
+ allure : AllureRuntime ;
23
+ }
24
+
18
25
export function wrapWithSteps ( options : WrapWithStepsOptions ) {
19
26
const { detox, worker, allure, logs, screenshots, videoManager } = options ;
20
27
const { device } = detox ;
@@ -114,7 +121,7 @@ function wrapDeviceMethod(
114
121
} ;
115
122
}
116
123
117
- function wrapPilotMethod ( { detox, allure } : WrapWithStepsOptions ) {
124
+ function wrapPilotMethod ( { detox, allure, screenshots } : WrapWithStepsOptions ) {
118
125
const pilot = ( detox as any ) . pilot ;
119
126
const originalInit = pilot ?. init ;
120
127
if ( typeof originalInit !== 'function' ) return ;
@@ -127,12 +134,46 @@ function wrapPilotMethod({ detox, allure }: WrapWithStepsOptions) {
127
134
instance . performStep = allure . createStep (
128
135
'{{0}}' ,
129
136
[ null ] ,
130
- instance . performStep . bind ( instance ) ,
137
+ wrapWithScreenshotTaking ( {
138
+ method : instance . performStep . bind ( instance ) ,
139
+ screenshots,
140
+ allure,
141
+ } ) ,
131
142
) ;
132
143
}
133
144
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
+ ) ;
135
154
}
136
155
return result ;
137
156
} ;
138
157
}
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