@@ -150,7 +150,6 @@ func (s *screenshotter) restoreViewport(p *Page, originalViewport *Size) error {
150
150
return p .resetViewport ()
151
151
}
152
152
153
- //nolint:funlen
154
153
func (s * screenshotter ) screenshot (
155
154
sess session , doc , viewport * Rect , format ImageFormat , omitBackground bool , quality int64 , path string ,
156
155
) ([]byte , error ) {
@@ -178,30 +177,9 @@ func (s *screenshotter) screenshot(
178
177
capture .WithFormat (cdppage .CaptureScreenshotFormatPng )
179
178
}
180
179
181
- // Add clip region
182
- //nolint:dogsled
183
- _ , _ , _ , _ , visualViewport , _ , err := cdppage .GetLayoutMetrics ().Do (cdp .WithExecutor (s .ctx , sess ))
180
+ visualViewportScale , visualViewportPageX , visualViewportPageY , err := getViewPortDimensions (s .ctx , sess , s .logger )
184
181
if err != nil {
185
- return nil , fmt .Errorf ("getting layout metrics for screenshot: %w" , err )
186
- }
187
-
188
- visualViewportScale := 1.0
189
- visualViewportPageX , visualViewportPageY := 0.0 , 0.0
190
- // we had a null pointer panic cases, when visualViewport is nil
191
- // instead of the erroring out, we fallback to defaults and still try to do a screenshot
192
- if visualViewport != nil {
193
- visualViewportScale = visualViewport .Scale
194
- visualViewportPageX = visualViewport .PageX
195
- visualViewportPageY = visualViewport .PageY
196
- } else {
197
- s .logger .Warnf (
198
- "Screenshotter::screenshot" ,
199
- "chrome browser returned nil on page.getLayoutMetrics, falling back to defaults for visualViewport " +
200
- "(scale: %v, pageX: %v, pageY: %v)." +
201
- "This is non-standard behavior, if possible please report this issue (with reproducible script) " +
202
- "to the https://github.com/grafana/xk6-browser/issues/1502." ,
203
- visualViewportScale , visualViewportPageX , visualViewportPageY ,
204
- )
182
+ return nil , err
205
183
}
206
184
207
185
if doc == nil {
@@ -255,6 +233,42 @@ func (s *screenshotter) screenshot(
255
233
return buf , nil
256
234
}
257
235
236
+ func getViewPortDimensions (ctx context.Context , sess session , logger * log.Logger ) (float64 , float64 , float64 , error ) {
237
+ visualViewportScale := 1.0
238
+ visualViewportPageX , visualViewportPageY := 0.0 , 0.0
239
+
240
+ // Add clip region
241
+ //nolint:dogsled
242
+ _ , visualViewport , _ , _ , cssVisualViewport , _ , err := cdppage .GetLayoutMetrics ().Do (cdp .WithExecutor (ctx , sess ))
243
+ if err != nil {
244
+ return 0 , 0 , 0 , fmt .Errorf ("getting layout metrics for screenshot: %w" , err )
245
+ }
246
+
247
+ // we had a null pointer panic cases, when visualViewport is nil
248
+ // instead of the erroring out, we fallback to defaults and still try to do a screenshot
249
+ switch {
250
+ case cssVisualViewport != nil :
251
+ visualViewportScale = cssVisualViewport .Scale
252
+ visualViewportPageX = cssVisualViewport .PageX
253
+ visualViewportPageY = cssVisualViewport .PageY
254
+ case visualViewport != nil :
255
+ visualViewportScale = visualViewport .Scale
256
+ visualViewportPageX = visualViewport .PageX
257
+ visualViewportPageY = visualViewport .PageY
258
+ default :
259
+ logger .Warnf (
260
+ "Screenshotter::screenshot" ,
261
+ "chrome browser returned nil on page.getLayoutMetrics, falling back to defaults for visualViewport " +
262
+ "(scale: %v, pageX: %v, pageY: %v)." +
263
+ "This is non-standard behavior, if possible please report this issue (with reproducible script) " +
264
+ "to the https://github.com/grafana/xk6-browser/issues/1502." ,
265
+ visualViewportScale , visualViewportPageX , visualViewportPageY ,
266
+ )
267
+ }
268
+
269
+ return visualViewportScale , visualViewportPageX , visualViewportPageY , nil
270
+ }
271
+
258
272
//nolint:funlen
259
273
func (s * screenshotter ) screenshotElement (h * ElementHandle , opts * ElementHandleScreenshotOptions ) ([]byte , error ) {
260
274
format := opts .Format
0 commit comments