feat: Page.setBypassServiceWorker (#10229)

Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com>
This commit is contained in:
Junyan 2023-05-23 19:51:32 +08:00 committed by GitHub
parent 640422680f
commit 81f73a55f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,19 @@
---
sidebar_label: Page.isServiceWorkerBypassed
---
# Page.isServiceWorkerBypassed() method
`true` if the service worker are being bypassed, `false` otherwise.
#### Signature:
```typescript
class Page {
isServiceWorkerBypassed(): boolean;
}
```
**Returns:**
boolean

View File

@ -117,6 +117,7 @@ page.off('request', logRequest);
| [isClosed()](./puppeteer.page.isclosed.md) | | Indicates that the page has been closed. | | [isClosed()](./puppeteer.page.isclosed.md) | | Indicates that the page has been closed. |
| [isDragInterceptionEnabled()](./puppeteer.page.isdraginterceptionenabled.md) | | <code>true</code> if drag events are being intercepted, <code>false</code> otherwise. | | [isDragInterceptionEnabled()](./puppeteer.page.isdraginterceptionenabled.md) | | <code>true</code> if drag events are being intercepted, <code>false</code> otherwise. |
| [isJavaScriptEnabled()](./puppeteer.page.isjavascriptenabled.md) | | <code>true</code> if the page has JavaScript enabled, <code>false</code> otherwise. | | [isJavaScriptEnabled()](./puppeteer.page.isjavascriptenabled.md) | | <code>true</code> if the page has JavaScript enabled, <code>false</code> otherwise. |
| [isServiceWorkerBypassed()](./puppeteer.page.isserviceworkerbypassed.md) | | <code>true</code> if the service worker are being bypassed, <code>false</code> otherwise. |
| [mainFrame()](./puppeteer.page.mainframe.md) | | The page's main frame. | | [mainFrame()](./puppeteer.page.mainframe.md) | | The page's main frame. |
| [metrics()](./puppeteer.page.metrics.md) | | Object containing metrics as key/value pairs. | | [metrics()](./puppeteer.page.metrics.md) | | Object containing metrics as key/value pairs. |
| [off(eventName, handler)](./puppeteer.page.off.md) | | | | [off(eventName, handler)](./puppeteer.page.off.md) | | |
@ -130,6 +131,7 @@ page.off('request', logRequest);
| [screenshot(options)](./puppeteer.page.screenshot_2.md) | | | | [screenshot(options)](./puppeteer.page.screenshot_2.md) | | |
| [select(selector, values)](./puppeteer.page.select.md) | | Triggers a <code>change</code> and <code>input</code> event once all the provided options have been selected. If there's no <code>&lt;select&gt;</code> element matching <code>selector</code>, the method throws an error. | | [select(selector, values)](./puppeteer.page.select.md) | | Triggers a <code>change</code> and <code>input</code> event once all the provided options have been selected. If there's no <code>&lt;select&gt;</code> element matching <code>selector</code>, the method throws an error. |
| [setBypassCSP(enabled)](./puppeteer.page.setbypasscsp.md) | | Toggles bypassing page's Content-Security-Policy. | | [setBypassCSP(enabled)](./puppeteer.page.setbypasscsp.md) | | Toggles bypassing page's Content-Security-Policy. |
| [setBypassServiceWorker(bypass)](./puppeteer.page.setbypassserviceworker.md) | | Toggles ignoring of service worker for each request. |
| [setCacheEnabled(enabled)](./puppeteer.page.setcacheenabled.md) | | Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled. | | [setCacheEnabled(enabled)](./puppeteer.page.setcacheenabled.md) | | Toggles ignoring cache for each request based on the enabled state. By default, caching is enabled. |
| [setContent(html, options)](./puppeteer.page.setcontent.md) | | Set the content of the page. | | [setContent(html, options)](./puppeteer.page.setcontent.md) | | Set the content of the page. |
| [setCookie(cookies)](./puppeteer.page.setcookie.md) | | | | [setCookie(cookies)](./puppeteer.page.setcookie.md) | | |

View File

@ -0,0 +1,25 @@
---
sidebar_label: Page.setBypassServiceWorker
---
# Page.setBypassServiceWorker() method
Toggles ignoring of service worker for each request.
#### Signature:
```typescript
class Page {
setBypassServiceWorker(bypass: boolean): Promise<void>;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ------- | ------------------------------------------------------- |
| bypass | boolean | Whether to bypass service worker and load from network. |
**Returns:**
Promise&lt;void&gt;

View File

@ -445,6 +445,13 @@ export class Page extends EventEmitter {
super(); super();
} }
/**
* `true` if the service worker are being bypassed, `false` otherwise.
*/
isServiceWorkerBypassed(): boolean {
throw new Error('Not implemented');
}
/** /**
* `true` if drag events are being intercepted, `false` otherwise. * `true` if drag events are being intercepted, `false` otherwise.
*/ */
@ -687,6 +694,16 @@ export class Page extends EventEmitter {
throw new Error('Not implemented'); throw new Error('Not implemented');
} }
/**
* Toggles ignoring of service worker for each request.
*
* @param bypass - Whether to bypass service worker and load from network.
*/
async setBypassServiceWorker(bypass: boolean): Promise<void>;
async setBypassServiceWorker(): Promise<void> {
throw new Error('Not implemented');
}
/** /**
* @param enabled - Whether to enable drag interception. * @param enabled - Whether to enable drag interception.
* *

View File

@ -157,6 +157,7 @@ export class CDPPage extends Page {
#fileChooserPromises = new Set<DeferredPromise<FileChooser>>(); #fileChooserPromises = new Set<DeferredPromise<FileChooser>>();
#disconnectPromise?: Promise<Error>; #disconnectPromise?: Promise<Error>;
#serviceWorkerBypassed = false;
#userDragInterceptionEnabled = false; #userDragInterceptionEnabled = false;
/** /**
@ -347,6 +348,10 @@ export class CDPPage extends Page {
return this.#client; return this.#client;
} }
override isServiceWorkerBypassed(): boolean {
return this.#serviceWorkerBypassed;
}
override isDragInterceptionEnabled(): boolean { override isDragInterceptionEnabled(): boolean {
return this.#userDragInterceptionEnabled; return this.#userDragInterceptionEnabled;
} }
@ -472,6 +477,11 @@ export class CDPPage extends Page {
return this.#frameManager.networkManager.setRequestInterception(value); return this.#frameManager.networkManager.setRequestInterception(value);
} }
override async setBypassServiceWorker(bypass: boolean): Promise<void> {
this.#serviceWorkerBypassed = bypass;
return this.#client.send('Network.setBypassServiceWorker', {bypass});
}
override async setDragInterception(enabled: boolean): Promise<void> { override async setDragInterception(enabled: boolean): Promise<void> {
this.#userDragInterceptionEnabled = enabled; this.#userDragInterceptionEnabled = enabled;
return this.#client.send('Input.setInterceptDrags', {enabled}); return this.#client.send('Input.setInterceptDrags', {enabled});

View File

@ -521,6 +521,18 @@
"parameters": ["webDriverBiDi"], "parameters": ["webDriverBiDi"],
"expectations": ["FAIL"] "expectations": ["FAIL"]
}, },
{
"testIdPattern": "[network.spec] network Page.setBypassServiceWorker *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[network.spec] network Page.setBypassServiceWorker *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox"],
"expectations": ["FAIL", "TIMEOUT"]
},
{ {
"testIdPattern": "[oopif.spec] *", "testIdPattern": "[oopif.spec] *",
"platforms": ["darwin", "linux", "win32"], "platforms": ["darwin", "linux", "win32"],

View File

@ -855,4 +855,44 @@ describe('network', function () {
} }
}); });
}); });
describe('Page.setBypassServiceWorker', async () => {
it('bypass for network', async () => {
const {page, server} = getTestState();
const responses = new Map();
page.on('response', r => {
return !isFavicon(r) && responses.set(r.url().split('/').pop(), r);
});
// Load and re-load to make sure serviceworker is installed and running.
await page.goto(server.PREFIX + '/serviceworkers/fetch/sw.html', {
waitUntil: 'networkidle2',
});
await page.evaluate(async () => {
return await (globalThis as any).activationPromise;
});
await page.reload({
waitUntil: 'networkidle2',
});
expect(page.isServiceWorkerBypassed()).toBe(false);
expect(responses.size).toBe(2);
expect(responses.get('sw.html').status()).toBe(200);
expect(responses.get('sw.html').fromServiceWorker()).toBe(true);
expect(responses.get('style.css').status()).toBe(200);
expect(responses.get('style.css').fromServiceWorker()).toBe(true);
await page.setBypassServiceWorker(true);
await page.reload({
waitUntil: 'networkidle2',
});
expect(page.isServiceWorkerBypassed()).toBe(true);
expect(responses.get('sw.html').status()).toBe(200);
expect(responses.get('sw.html').fromServiceWorker()).toBe(false);
expect(responses.get('style.css').status()).toBe(200);
expect(responses.get('style.css').fromServiceWorker()).toBe(false);
});
});
}); });