chore: add evaluateOnNewDocument for BiDi (#10712)

This commit is contained in:
Nikolay Vitkov 2023-08-09 10:00:33 +02:00 committed by GitHub
parent f70048c84f
commit 47dfc3578d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -47,6 +47,10 @@ interface Commands {
params: Bidi.Script.AddPreloadScriptParameters;
returnType: Bidi.Script.AddPreloadScriptResult;
};
'script.removePreloadScript': {
params: Bidi.Script.RemovePreloadScriptParameters;
returnType: Bidi.EmptyResult;
};
'browsingContext.activate': {
params: Bidi.BrowsingContext.ActivateParameters;

View File

@ -22,6 +22,7 @@ import Protocol from 'devtools-protocol';
import {
GeolocationOptions,
MediaFeature,
NewDocumentScriptEvaluation,
Page as PageBase,
PageEmittedEvents,
ScreenshotOptions,
@ -45,6 +46,7 @@ import {Tracing} from '../Tracing.js';
import {EvaluateFunc, HandleFor} from '../types.js';
import {
debugError,
evaluationString,
isString,
validateDialogType,
waitForEvent,
@ -726,6 +728,30 @@ export class Page extends PageBase {
context: this.mainFrame()._id,
});
}
override async evaluateOnNewDocument<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown,
>(
pageFunction: Func | string,
...args: Params
): Promise<NewDocumentScriptEvaluation> {
const expression = evaluationExpression(pageFunction, ...args);
const {result} = await this.#connection.send('script.addPreloadScript', {
functionDeclaration: expression,
// TODO: should change spec to accept browsingContext
});
return {identifier: result.script};
}
override async removeScriptToEvaluateOnNewDocument(
id: string
): Promise<void> {
await this.#connection.send('script.removePreloadScript', {
script: id,
});
}
}
function isConsoleLogEntry(
@ -755,3 +781,7 @@ function getStackTraceLocations(
}
return stackTraceLocations;
}
function evaluationExpression(fun: Function | string, ...args: unknown[]) {
return `() => {${evaluationString(fun, ...args)}}`;
}

View File

@ -4150,5 +4150,17 @@
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "chrome", "headless"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should evaluate before anything else on the page",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should work with CSP",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
}
]