test: enable addScriptTag tests (#10583)

This commit is contained in:
Alex Rudenko 2023-07-18 21:55:30 +02:00 committed by GitHub
parent d71836e7ed
commit c7f3fb21be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 91 deletions

View File

@ -764,9 +764,64 @@ export class Frame {
*/
async addScriptTag(
options: FrameAddScriptTagOptions
): Promise<ElementHandle<HTMLScriptElement>>;
async addScriptTag(): Promise<ElementHandle<HTMLScriptElement>> {
throw new Error('Not implemented');
): Promise<ElementHandle<HTMLScriptElement>> {
let {content = '', type} = options;
const {path} = options;
if (+!!options.url + +!!path + +!!content !== 1) {
throw new Error(
'Exactly one of `url`, `path`, or `content` must be specified.'
);
}
if (path) {
const fs = await importFSPromises();
content = await fs.readFile(path, 'utf8');
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
}
type = type ?? 'text/javascript';
return this.mainRealm().transferHandle(
await this.isolatedRealm().evaluateHandle(
async ({Deferred}, {url, id, type, content}) => {
const deferred = Deferred.create<void>();
const script = document.createElement('script');
script.type = type;
script.text = content;
if (url) {
script.src = url;
script.addEventListener(
'load',
() => {
return deferred.resolve();
},
{once: true}
);
script.addEventListener(
'error',
event => {
deferred.reject(
new Error(event.message ?? 'Could not load script')
);
},
{once: true}
);
} else {
deferred.resolve();
}
if (id) {
script.id = id;
}
document.head.appendChild(script);
await deferred.valueOrThrow();
return script;
},
LazyArg.create(context => {
return context.puppeteerUtil;
}),
{...options, type, content}
)
);
}
/**

View File

@ -1183,9 +1183,8 @@ export class Page extends EventEmitter {
*/
async addScriptTag(
options: FrameAddScriptTagOptions
): Promise<ElementHandle<HTMLScriptElement>>;
async addScriptTag(): Promise<ElementHandle<HTMLScriptElement>> {
throw new Error('Not implemented');
): Promise<ElementHandle<HTMLScriptElement>> {
return this.mainFrame().addScriptTag(options);
}
/**

View File

@ -17,7 +17,7 @@
import {Protocol} from 'devtools-protocol';
import {ElementHandle} from '../api/ElementHandle.js';
import {Frame as BaseFrame, FrameAddScriptTagOptions} from '../api/Frame.js';
import {Frame as BaseFrame} from '../api/Frame.js';
import {HTTPResponse} from '../api/HTTPResponse.js';
import {Page, WaitTimeoutOptions} from '../api/Page.js';
import {assert} from '../util/assert.js';
@ -33,10 +33,9 @@ import {ExecutionContext} from './ExecutionContext.js';
import {FrameManager} from './FrameManager.js';
import {IsolatedWorld} from './IsolatedWorld.js';
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
import {LazyArg} from './LazyArg.js';
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js';
import {EvaluateFunc, EvaluateFuncWith, HandleFor, NodeFor} from './types.js';
import {importFSPromises, withSourcePuppeteerURLIfNone} from './util.js';
import {withSourcePuppeteerURLIfNone} from './util.js';
/**
* @internal
@ -333,68 +332,6 @@ export class Frame extends BaseFrame {
return this.#detached;
}
override async addScriptTag(
options: FrameAddScriptTagOptions
): Promise<ElementHandle<HTMLScriptElement>> {
let {content = '', type} = options;
const {path} = options;
if (+!!options.url + +!!path + +!!content !== 1) {
throw new Error(
'Exactly one of `url`, `path`, or `content` must be specified.'
);
}
if (path) {
const fs = await importFSPromises();
content = await fs.readFile(path, 'utf8');
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
}
type = type ?? 'text/javascript';
return this.mainRealm().transferHandle(
await this.isolatedRealm().evaluateHandle(
async ({Deferred}, {url, id, type, content}) => {
const deferred = Deferred.create<void>();
const script = document.createElement('script');
script.type = type;
script.text = content;
if (url) {
script.src = url;
script.addEventListener(
'load',
() => {
return deferred.resolve();
},
{once: true}
);
script.addEventListener(
'error',
event => {
deferred.reject(
new Error(event.message ?? 'Could not load script')
);
},
{once: true}
);
} else {
deferred.resolve();
}
if (id) {
script.id = id;
}
document.head.appendChild(script);
await deferred.valueOrThrow();
return script;
},
LazyArg.create(context => {
return context.puppeteerUtil;
}),
{...options, type, content}
)
);
}
override async title(): Promise<string> {
return this.isolatedRealm().title();
}

View File

@ -21,7 +21,7 @@ import {Protocol} from 'devtools-protocol';
import type {Browser} from '../api/Browser.js';
import type {BrowserContext} from '../api/BrowserContext.js';
import {ElementHandle} from '../api/ElementHandle.js';
import {Frame, FrameAddScriptTagOptions} from '../api/Frame.js';
import {Frame} from '../api/Frame.js';
import {HTTPRequest} from '../api/HTTPRequest.js';
import {HTTPResponse} from '../api/HTTPResponse.js';
import {JSHandle} from '../api/JSHandle.js';
@ -578,12 +578,6 @@ export class CDPPage extends Page {
}
}
override async addScriptTag(
options: FrameAddScriptTagOptions
): Promise<ElementHandle<HTMLScriptElement>> {
return this.mainFrame().addScriptTag(options);
}
override async exposeFunction(
name: string,
pptrFunction: Function | {default: Function}

View File

@ -155,6 +155,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["FAIL", "TIMEOUT"]
},
{
"testIdPattern": "[page.spec] Page Page.addScriptTag *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[page.spec] Page Page.addStyleTag *",
"platforms": ["darwin", "linux", "win32"],
@ -311,6 +317,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[CDPSession.spec] Target.createCDPSession *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[CDPSession.spec] Target.createCDPSession should not report created targets for custom CDP sessions",
"platforms": ["darwin", "linux", "win32"],
@ -1107,13 +1119,7 @@
"testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with URL to the CSP page",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
"expectations": ["FAIL"]
},
{
"testIdPattern": "[page.spec] Page Page.addStyleTag should throw when added with content to the CSP page",
@ -3053,12 +3059,6 @@
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[page.spec] Page Page.addScriptTag should throw an error if loading from url fail",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page",
"platforms": ["darwin", "linux", "win32"],