fix(firefox): throw error when added script blocked by CSP (#1841)

This commit is contained in:
Yury Semikhatsky 2020-04-17 08:51:54 -07:00 committed by GitHub
parent e8bf5fd928
commit 2b96b85e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 3 deletions

View File

@ -35,6 +35,7 @@ import { selectors } from '../selectors';
const UTILITY_WORLD_NAME = '__playwright_utility_world__'; const UTILITY_WORLD_NAME = '__playwright_utility_world__';
export class FFPage implements PageDelegate { export class FFPage implements PageDelegate {
readonly cspErrorsAsynchronousForInlineScipts = true;
readonly rawMouse: RawMouseImpl; readonly rawMouse: RawMouseImpl;
readonly rawKeyboard: RawKeyboardImpl; readonly rawKeyboard: RawKeyboardImpl;
readonly _session: FFSession; readonly _session: FFSession;

View File

@ -571,12 +571,19 @@ export class Frame {
return this._raceWithCSPError(async () => { return this._raceWithCSPError(async () => {
if (url !== null) if (url !== null)
return (await context.evaluateHandleInternal(addScriptUrl, { url, type })).asElement()!; return (await context.evaluateHandleInternal(addScriptUrl, { url, type })).asElement()!;
let result;
if (path !== null) { if (path !== null) {
let contents = await util.promisify(fs.readFile)(path, 'utf8'); let contents = await util.promisify(fs.readFile)(path, 'utf8');
contents += '//# sourceURL=' + path.replace(/\n/g, ''); contents += '//# sourceURL=' + path.replace(/\n/g, '');
return (await context.evaluateHandleInternal(addScriptContent, { content: contents, type })).asElement()!; result = (await context.evaluateHandleInternal(addScriptContent, { content: contents, type })).asElement()!;
} else {
result = (await context.evaluateHandleInternal(addScriptContent, { content: content!, type })).asElement()!;
} }
return (await context.evaluateHandleInternal(addScriptContent, { content: content!, type })).asElement()!; // Another round trip to the browser to ensure that we receive CSP error messages
// (if any) logged asynchronously in a separate task on the content main thread.
if (this._page._delegate.cspErrorsAsynchronousForInlineScipts)
await context.evaluateInternal(() => true);
return result;
}); });
async function addScriptUrl(options: { url: string, type: string }): Promise<HTMLElement> { async function addScriptUrl(options: { url: string, type: string }): Promise<HTMLElement> {

View File

@ -75,6 +75,8 @@ export interface PageDelegate {
// Work around Chrome's non-associated input and protocol. // Work around Chrome's non-associated input and protocol.
inputActionEpilogue(): Promise<void>; inputActionEpilogue(): Promise<void>;
// Work around for asynchronously dispatched CSP errors in Firefox.
readonly cspErrorsAsynchronousForInlineScipts?: boolean;
} }
type PageState = { type PageState = {

View File

@ -650,7 +650,7 @@ describe('Page.addScriptTag', function() {
}); });
// Firefox fires onload for blocked script before it issues the CSP console error. // Firefox fires onload for blocked script before it issues the CSP console error.
it.fail(FFOX)('should throw when added with content to the CSP page', async({page, server}) => { it('should throw when added with content to the CSP page', async({page, server}) => {
await page.goto(server.PREFIX + '/csp.html'); await page.goto(server.PREFIX + '/csp.html');
let error = null; let error = null;
await page.addScriptTag({ content: 'window.__injected = 35;' }).catch(e => error = e); await page.addScriptTag({ content: 'window.__injected = 35;' }).catch(e => error = e);