"WebDriver:TakeScreenshot" doens't throw "unable to capture screen" error if height or width of captured image is 0

NEW
Unassigned

Status

defect
P3
normal
8 months ago
7 months ago

People

(Reporter: mozilla, Unassigned)

Tracking

(Blocks 1 bug)

62 Branch
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

Reporter

Description

8 months ago
User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36

Steps to reproduce:

Launch geckodriver:
```bash
./geckodriver --port=7000
```

Start a web server:
```bash
python3 -m http.server 9080
```

Create an empty test page empty.html with 0 margin and a background color:
```
<html><head><title></title></head><body style="background: #1289af; margin:0px;"></body></html>
```

Run the following script:
```bash
#!/bin/bash

SESSIONID=$(curl -X POST -d '{"capabilities":{"alwaysMatch":{"moz:firefoxOptions":{"args":["--headless"]}}}}' http://localhost:7000/session | jq -r ".value.sessionId")
echo "SESSIONID: ${SESSIONID}"

curl -v -X POST -d '{"url": "http://localhost:9080/empty.html"}' http://localhost:7000/session/${SESSIONID}/url
json=$(curl -v http://localhost:7000/session/${SESSIONID}/screenshot)
echo "${json}"
echo "${json}" | jq -r ".value" | base64 -d > empty.png
```



Actual results:

The resulting image file empty.png is not a valid PNG file (0 bytes). Specifically the base64 string returned by the screenshot command is empty.


Expected results:

The returned screenshot should be a valid PNG file. I suspect what is happening is that because the screenshot misbehavior of taking a screenshot of the body content rather than the viewport (https://bugzilla.mozilla.org/show_bug.cgi?id=1385706), the screenshot code is short-circuiting when it sees 0x0 data and returns nothing instead of a valid 0x0 PNG file.
I can see this too with the recent Nightly build of Firefox. When quickly debugging it the problem seems to be located here:

> canvas.toDataURL("image/png");

This only returns "data:," without any MIME type, and no data.

A check on MDN shows the following:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL

> If the height or width of the canvas is 0, the string "data:," is returned.

I had a look at the WebDriver specification and it says the following:

https://w3c.github.io/webdriver/#dfn-encoding-as-base64

> If the canvas element’s bitmap has no pixels (i.e. either its horizontal dimension or vertical dimension is zero) then return error with error code unable to capture screen. 

It means we miss to throw this defined error.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Priority: -- → P3
Summary: Screenshot of page with only background image results in invalid image file (0 bytes) → "WebDriver:TakeScreenshot" doens't throw "unable to capture screen" error if height or width of captured image is 0
You need to log in before you can comment on or make changes to this bug.