-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith-proxied-executor-bidi-capture-screenshot.js
46 lines (36 loc) · 1.59 KB
/
with-proxied-executor-bidi-capture-screenshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const webdriver = require("selenium-webdriver");
const getBrowsingContextInstance = require("selenium-webdriver/bidi/browsingContext");
const { timestampMicroseconds } = require("../utils/timestamp");
const { plainCommandSequence } = require("./common/plain-command-sequence");
exports.withProxiedExecutorBidiCaptureScreenshot = async (driver) => {
let screenshotData = {};
const executeProxy = (originalExecute, browsingContext) => {
return new Proxy(originalExecute, {
apply: async (target, thisArg, args) => {
const commandResult = await target.apply(thisArg, args);
/** @type {import('selenium-webdriver/lib/command').Command} */
const command = args[0];
const commandName = command.getName();
if (commandName !== "screenshot" && commandName !== "quit") {
screenshotData[`${timestampMicroseconds()}.png`] =
await browsingContext.captureScreenshot();
}
return commandResult;
},
});
};
const windowHandle = await driver.getWindowHandle();
const browsingContext = await getBrowsingContextInstance(driver, {
browsingContextId: windowHandle,
});
// Proxy WebDriver command executor to take screenshot on every command
const originalExecute = webdriver.WebDriver.prototype.execute;
webdriver.WebDriver.prototype.execute = executeProxy(
webdriver.WebDriver.prototype.execute,
browsingContext
);
await plainCommandSequence(driver);
// Restore execute to not interfere with creating a new driver instance
webdriver.WebDriver.prototype.execute = originalExecute;
return screenshotData;
};