Open thiagowfx opened 1 year ago
WPT headful test is failing. I temporary disabled it in https://github.com/GoogleChromeLabs/chromium-bidi/pull/548
DevTools has functionality to capture node screenshot. What it does is:
Call callFunctionOn(node)
with script:
() => {
const e = this.getBoundingClientRect(),
t = this.ownerDocument.documentElement.getBoundingClientRect();
return JSON.stringify({
x: e.left - t.left,
y: e.top - t.top,
width: e.width,
height: e.height,
scale: 1.0
});
}
And call Page.captureScreenshot
with the following params:
{
"format": "png",
"quality": 100,
"fromSurface": true,
"captureBeyondViewport": true,
"clip": {
RECEIVED_VALUES
}
}
The comment above could help implementing the nested frame screenshot.
References:
@sadym-chromium:
const e = this.getBoundingClientRect()
seems to be
const metrics = await this.#cdpTarget.cdpClient.sendCommand(
'Page.getLayoutMetrics'
).cssContentSize;
Do you know how to retrieve "t"?
Debugging:
async captureScreenshot(): Promise<BrowsingContext.CaptureScreenshotResult> {
// XXX: Either make this a proposal in the BiDi spec, or focus the
// original tab right after the screenshot is taken.
// The screenshot command gets blocked until we focus the active tab.
await this.#cdpTarget.cdpClient.sendCommand('Page.bringToFront'); // window.focus() also works
const docRect = await this.#cdpTarget.cdpClient.sendCommand(
'Runtime.callFunctionOn',
{
functionDeclaration: `() => {
const docRect = window.documentElement.getBoundingClientRect();
return JSON.stringify({
x: docRect.left,
y: docRect.top,
});
}`,
executionContextId: this.#defaultRealm.executionContextId,
}
);
const {result: docRectResult} = docRect;
console.log(docRectResult);
const metrics = await this.#cdpTarget.cdpClient.sendCommand(
'Page.getLayoutMetrics'
);
// or maybe cssLayoutViewport
const {cssContentSize: viewport} = metrics;
debugger;
const [result] = await Promise.all([
this.#cdpTarget.cdpClient.sendCommand('Page.captureScreenshot', {
format: 'png', // XXX: add more formats: jpeg,webp, then add quality for jpeg
captureBeyondViewport: true,
clip: {...viewport, scale: 1.0},
}),
]);
return {
result: {
data: result.data,
},
};
}
More debugging:
async captureScreenshot(): Promise<BrowsingContext.CaptureScreenshotResult> {
// XXX: Either make this a proposal in the BiDi spec, or focus the
// original tab right after the screenshot is taken.
// The screenshot command gets blocked until we focus the active tab.
await this.#cdpTarget.cdpClient.sendCommand('Page.bringToFront'); // window.focus() also works
const docRect = await this.#cdpTarget.cdpClient.sendCommand(
'Runtime.callFunctionOn',
{
functionDeclaration: `() => { return JSON.stringify(globalThis.document.documentElement.getBoundingClientRect()); }`,
executionContextId: this.#defaultRealm.executionContextId,
}
);
console.log(docRect);
const metrics = await this.#cdpTarget.cdpClient.sendCommand(
'Page.getLayoutMetrics'
);
// or maybe cssLayoutViewport
const {cssContentSize: viewport} = metrics;
debugger;
const [result] = await Promise.all([
this.#cdpTarget.cdpClient.sendCommand('Page.captureScreenshot', {
format: 'png', // XXX: add more formats: jpeg,webp, then add quality for jpeg
captureBeyondViewport: true,
clip: {...viewport, scale: 1.0},
}),
]);
return {
result: {
data: result.data,
},
};
}
Implement scrollIntoView
This one can be closed, as it was removed from the BiDi spec.
Tracking
/webdriver/tests/bidi/browsing_context/capture_screenshot/*
Page.captureScreenshot
-> need viewport/session/{id}/screenshot
(via WebDriver) --> this is merely an implementation detailcaptureScreenshot
scrollIntoView
iframes: setcaptureBeyondViewport
to trueTab focus: https://github.com/GoogleChromeLabs/chromium-bidi/pull/796https://github.com/GoogleChromeLabs/chromium-bidi/pull/797/ https://github.com/w3c/webdriver-bidi/issues/441--enable-automation