Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 689f4a9

Browse files
committed
Fallback to the deprecated viewPort definitions
1 parent 1f50726 commit 689f4a9

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

common/screenshotter.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func (s *screenshotter) restoreViewport(p *Page, originalViewport *Size) error {
150150
return p.resetViewport()
151151
}
152152

153-
//nolint:funlen
154153
func (s *screenshotter) screenshot(
155154
sess session, doc, viewport *Rect, format ImageFormat, omitBackground bool, quality int64, path string,
156155
) ([]byte, error) {
@@ -178,30 +177,9 @@ func (s *screenshotter) screenshot(
178177
capture.WithFormat(cdppage.CaptureScreenshotFormatPng)
179178
}
180179

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)
184181
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
205183
}
206184

207185
if doc == nil {
@@ -255,6 +233,42 @@ func (s *screenshotter) screenshot(
255233
return buf, nil
256234
}
257235

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+
258272
//nolint:funlen
259273
func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleScreenshotOptions) ([]byte, error) {
260274
format := opts.Format

0 commit comments

Comments
 (0)