mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: move more unsupported apis to interception (#91)
This commit is contained in:
parent
0494c4c04e
commit
c7a07ea2a8
31
docs/api.md
31
docs/api.md
@ -88,7 +88,6 @@
|
|||||||
* [page.accessibility](#pageaccessibility)
|
* [page.accessibility](#pageaccessibility)
|
||||||
* [page.addScriptTag(options)](#pageaddscripttagoptions)
|
* [page.addScriptTag(options)](#pageaddscripttagoptions)
|
||||||
* [page.addStyleTag(options)](#pageaddstyletagoptions)
|
* [page.addStyleTag(options)](#pageaddstyletagoptions)
|
||||||
* [page.authenticate(credentials)](#pageauthenticatecredentials)
|
|
||||||
* [page.browser()](#pagebrowser)
|
* [page.browser()](#pagebrowser)
|
||||||
* [page.browserContext()](#pagebrowsercontext)
|
* [page.browserContext()](#pagebrowsercontext)
|
||||||
* [page.click(selector[, options])](#pageclickselector-options)
|
* [page.click(selector[, options])](#pageclickselector-options)
|
||||||
@ -127,7 +126,6 @@
|
|||||||
* [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout)
|
* [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout)
|
||||||
* [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
|
* [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
|
||||||
* [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
|
* [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
|
||||||
* [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
|
|
||||||
* [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
|
* [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
|
||||||
* [page.setViewport(viewport)](#pagesetviewportviewport)
|
* [page.setViewport(viewport)](#pagesetviewportviewport)
|
||||||
* [page.target()](#pagetarget)
|
* [page.target()](#pagetarget)
|
||||||
@ -232,10 +230,12 @@
|
|||||||
* [executionContext.frame()](#executioncontextframe)
|
* [executionContext.frame()](#executioncontextframe)
|
||||||
- [class: Interception](#class-interception)
|
- [class: Interception](#class-interception)
|
||||||
* [interception.abort(request, [errorCode])](#interceptionabortrequest-errorcode)
|
* [interception.abort(request, [errorCode])](#interceptionabortrequest-errorcode)
|
||||||
|
* [interception.authenticate(credentials)](#interceptionauthenticatecredentials)
|
||||||
* [interception.continue(request, [overrides])](#interceptioncontinuerequest-overrides)
|
* [interception.continue(request, [overrides])](#interceptioncontinuerequest-overrides)
|
||||||
* [interception.disable()](#interceptiondisable)
|
* [interception.disable()](#interceptiondisable)
|
||||||
* [interception.enable()](#interceptionenable)
|
* [interception.enable()](#interceptionenable)
|
||||||
* [interception.fulfill(request, response)](#interceptionfulfillrequest-response)
|
* [interception.fulfill(request, response)](#interceptionfulfillrequest-response)
|
||||||
|
* [interception.setOfflineMode(enabled)](#interceptionsetofflinemodeenabled)
|
||||||
- [class: JSHandle](#class-jshandle)
|
- [class: JSHandle](#class-jshandle)
|
||||||
* [jsHandle.asElement()](#jshandleaselement)
|
* [jsHandle.asElement()](#jshandleaselement)
|
||||||
* [jsHandle.dispose()](#jshandledispose)
|
* [jsHandle.dispose()](#jshandledispose)
|
||||||
@ -1119,16 +1119,6 @@ Adds a `<link rel="stylesheet">` tag into the page with the desired url or a `<s
|
|||||||
|
|
||||||
Shortcut for [page.mainFrame().addStyleTag(options)](#frameaddstyletagoptions).
|
Shortcut for [page.mainFrame().addStyleTag(options)](#frameaddstyletagoptions).
|
||||||
|
|
||||||
#### page.authenticate(credentials)
|
|
||||||
- `credentials` <?[Object]>
|
|
||||||
- `username` <[string]>
|
|
||||||
- `password` <[string]>
|
|
||||||
- returns: <[Promise]>
|
|
||||||
|
|
||||||
Provide credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
|
|
||||||
|
|
||||||
To disable authentication, pass `null`.
|
|
||||||
|
|
||||||
#### page.browser()
|
#### page.browser()
|
||||||
|
|
||||||
- returns: <[Browser]>
|
- returns: <[Browser]>
|
||||||
@ -1676,10 +1666,6 @@ The extra HTTP headers will be sent with every request the page initiates.
|
|||||||
|
|
||||||
> **NOTE** changing this value won't affect scripts that have already been run. It will take full effect on the next [navigation](#pagegotourl-options).
|
> **NOTE** changing this value won't affect scripts that have already been run. It will take full effect on the next [navigation](#pagegotourl-options).
|
||||||
|
|
||||||
#### page.setOfflineMode(enabled)
|
|
||||||
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
|
|
||||||
- returns: <[Promise]>
|
|
||||||
|
|
||||||
#### page.setUserAgent(userAgent)
|
#### page.setUserAgent(userAgent)
|
||||||
- `userAgent` <[string]> Specific user agent to use in this page
|
- `userAgent` <[string]> Specific user agent to use in this page
|
||||||
- returns: <[Promise]> Promise which resolves when the user agent is set.
|
- returns: <[Promise]> Promise which resolves when the user agent is set.
|
||||||
@ -3111,6 +3097,16 @@ await resultHandle.dispose();
|
|||||||
Aborts request. To use this, request interception should be enabled with `page.interception.enable()`.
|
Aborts request. To use this, request interception should be enabled with `page.interception.enable()`.
|
||||||
Exception is immediately thrown if the request interception is not enabled.
|
Exception is immediately thrown if the request interception is not enabled.
|
||||||
|
|
||||||
|
#### interception.authenticate(credentials)
|
||||||
|
- `credentials` <?[Object]>
|
||||||
|
- `username` <[string]>
|
||||||
|
- `password` <[string]>
|
||||||
|
- returns: <[Promise]>
|
||||||
|
|
||||||
|
Provide credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
|
||||||
|
|
||||||
|
To disable authentication, pass `null`.
|
||||||
|
|
||||||
#### interception.continue(request, [overrides])
|
#### interception.continue(request, [overrides])
|
||||||
- `request` <[Request]>
|
- `request` <[Request]>
|
||||||
- `overrides` <[Object]> Optional request overwrites, which can be one of the following:
|
- `overrides` <[Object]> Optional request overwrites, which can be one of the following:
|
||||||
@ -3195,6 +3191,9 @@ page.on('request', request => {
|
|||||||
> **NOTE** Mocking responses for dataURL requests is not supported.
|
> **NOTE** Mocking responses for dataURL requests is not supported.
|
||||||
> Calling `request.respond` for a dataURL request is a noop.
|
> Calling `request.respond` for a dataURL request is a noop.
|
||||||
|
|
||||||
|
#### interception.setOfflineMode(enabled)
|
||||||
|
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
|
||||||
|
- returns: <[Promise]>
|
||||||
|
|
||||||
### class: JSHandle
|
### class: JSHandle
|
||||||
|
|
||||||
|
|||||||
@ -217,10 +217,6 @@ export class Page extends EventEmitter {
|
|||||||
return this._frameManager.frames();
|
return this._frameManager.frames();
|
||||||
}
|
}
|
||||||
|
|
||||||
setOfflineMode(enabled: boolean) {
|
|
||||||
return this._frameManager.networkManager().setOfflineMode(enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
setDefaultNavigationTimeout(timeout: number) {
|
setDefaultNavigationTimeout(timeout: number) {
|
||||||
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
||||||
}
|
}
|
||||||
@ -290,10 +286,6 @@ export class Page extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async authenticate(credentials: { username: string; password: string; } | null) {
|
|
||||||
return this._frameManager.networkManager().authenticate(credentials);
|
|
||||||
}
|
|
||||||
|
|
||||||
async setExtraHTTPHeaders(headers: { [s: string]: string; }) {
|
async setExtraHTTPHeaders(headers: { [s: string]: string; }) {
|
||||||
return this._frameManager.networkManager().setExtraHTTPHeaders(headers);
|
return this._frameManager.networkManager().setExtraHTTPHeaders(headers);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -109,8 +109,7 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
expect(coverage.length).toBe(0);
|
expect(coverage.length).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// @see https://crbug.com/990945
|
it('should not hang when there is a debugger statement', async function({page, server}) {
|
||||||
xit('should not hang when there is a debugger statement', async function({page, server}) {
|
|
||||||
await page.coverage.startJSCoverage();
|
await page.coverage.startJSCoverage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
|
|||||||
@ -92,6 +92,7 @@ class JSCoverage {
|
|||||||
helper.addEventListener(this._client, 'Debugger.scriptParsed', this._onScriptParsed.bind(this)),
|
helper.addEventListener(this._client, 'Debugger.scriptParsed', this._onScriptParsed.bind(this)),
|
||||||
helper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)),
|
helper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)),
|
||||||
];
|
];
|
||||||
|
this._client.on('Debugger.paused', () => this._client.send('Debugger.resume'));
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this._client.send('Profiler.enable'),
|
this._client.send('Profiler.enable'),
|
||||||
this._client.send('Profiler.startPreciseCoverage', {callCount: false, detailed: true}),
|
this._client.send('Profiler.startPreciseCoverage', {callCount: false, detailed: true}),
|
||||||
|
|||||||
@ -29,4 +29,12 @@ export class Interception {
|
|||||||
async abort(request: Request, errorCode: string = 'failed') {
|
async abort(request: Request, errorCode: string = 'failed') {
|
||||||
return request._abort(errorCode);
|
return request._abort(errorCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setOfflineMode(enabled: boolean) {
|
||||||
|
return this._networkManager.setOfflineMode(enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
async authenticate(credentials: { username: string; password: string; } | null) {
|
||||||
|
return this._networkManager.authenticate(credentials);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,7 +94,7 @@ module.exports.addTests = function({testRunner, expect, playwright, defaultBrows
|
|||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
await page.interception.enable();
|
await page.interception.enable();
|
||||||
page.on('request', r => r.respond({body: 'YO, GOOGLE.COM'}));
|
page.on('request', r => page.interception.fulfill(r, {body: 'YO, GOOGLE.COM'}));
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
const frame = document.createElement('iframe');
|
const frame = document.createElement('iframe');
|
||||||
frame.setAttribute('src', 'https://google.com/');
|
frame.setAttribute('src', 'https://google.com/');
|
||||||
|
|||||||
@ -358,12 +358,12 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// FIXME: WebKit doesn't support network interception.
|
// FIXME: WebKit doesn't support network interception.
|
||||||
describe.skip(FFOX || WEBKIT)('Page.authenticate', function() {
|
describe.skip(FFOX || WEBKIT)('Interception.authenticate', function() {
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
server.setAuth('/empty.html', 'user', 'pass');
|
server.setAuth('/empty.html', 'user', 'pass');
|
||||||
let response = await page.goto(server.EMPTY_PAGE);
|
let response = await page.goto(server.EMPTY_PAGE);
|
||||||
expect(response.status()).toBe(401);
|
expect(response.status()).toBe(401);
|
||||||
await page.authenticate({
|
await page.interception.authenticate({
|
||||||
username: 'user',
|
username: 'user',
|
||||||
password: 'pass'
|
password: 'pass'
|
||||||
});
|
});
|
||||||
@ -373,7 +373,7 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
it('should fail if wrong credentials', async({page, server}) => {
|
it('should fail if wrong credentials', async({page, server}) => {
|
||||||
// Use unique user/password since Chrome caches credentials per origin.
|
// Use unique user/password since Chrome caches credentials per origin.
|
||||||
server.setAuth('/empty.html', 'user2', 'pass2');
|
server.setAuth('/empty.html', 'user2', 'pass2');
|
||||||
await page.authenticate({
|
await page.interception.authenticate({
|
||||||
username: 'foo',
|
username: 'foo',
|
||||||
password: 'bar'
|
password: 'bar'
|
||||||
});
|
});
|
||||||
@ -383,13 +383,13 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
it('should allow disable authentication', async({page, server}) => {
|
it('should allow disable authentication', async({page, server}) => {
|
||||||
// Use unique user/password since Chrome caches credentials per origin.
|
// Use unique user/password since Chrome caches credentials per origin.
|
||||||
server.setAuth('/empty.html', 'user3', 'pass3');
|
server.setAuth('/empty.html', 'user3', 'pass3');
|
||||||
await page.authenticate({
|
await page.interception.authenticate({
|
||||||
username: 'user3',
|
username: 'user3',
|
||||||
password: 'pass3'
|
password: 'pass3'
|
||||||
});
|
});
|
||||||
let response = await page.goto(server.EMPTY_PAGE);
|
let response = await page.goto(server.EMPTY_PAGE);
|
||||||
expect(response.status()).toBe(200);
|
expect(response.status()).toBe(200);
|
||||||
await page.authenticate(null);
|
await page.interception.authenticate(null);
|
||||||
// Navigate to a different origin to bust Chrome's credential caching.
|
// Navigate to a different origin to bust Chrome's credential caching.
|
||||||
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
||||||
expect(response.status()).toBe(401);
|
expect(response.status()).toBe(401);
|
||||||
|
|||||||
@ -166,21 +166,21 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skip(FFOX || WEBKIT)('Page.setOfflineMode', function() {
|
describe.skip(FFOX || WEBKIT)('Interception.setOfflineMode', function() {
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
await page.setOfflineMode(true);
|
await page.interception.setOfflineMode(true);
|
||||||
let error = null;
|
let error = null;
|
||||||
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
||||||
expect(error).toBeTruthy();
|
expect(error).toBeTruthy();
|
||||||
await page.setOfflineMode(false);
|
await page.interception.setOfflineMode(false);
|
||||||
const response = await page.reload();
|
const response = await page.reload();
|
||||||
expect(response.status()).toBe(200);
|
expect(response.status()).toBe(200);
|
||||||
});
|
});
|
||||||
it('should emulate navigator.onLine', async({page, server}) => {
|
it('should emulate navigator.onLine', async({page, server}) => {
|
||||||
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
||||||
await page.setOfflineMode(true);
|
await page.interception.setOfflineMode(true);
|
||||||
expect(await page.evaluate(() => window.navigator.onLine)).toBe(false);
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(false);
|
||||||
await page.setOfflineMode(false);
|
await page.interception.setOfflineMode(false);
|
||||||
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user