Skip to content

Commit

Permalink
feat(Page): implement page.setOfflineMode (#1032)
Browse files Browse the repository at this point in the history
This patch implements page.setOfflineMode method.

Fixes #63.
  • Loading branch information
aslushnikov committed Oct 13, 2017
1 parent fd88eb5 commit a02347e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api.md
Expand Up @@ -68,6 +68,7 @@
+ [page.setCookie(...cookies)](#pagesetcookiecookies)
+ [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
+ [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
+ [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
+ [page.setRequestInterceptionEnabled(value)](#pagesetrequestinterceptionenabledvalue)
+ [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
+ [page.setViewport(viewport)](#pagesetviewportviewport)
Expand Down Expand Up @@ -900,6 +901,10 @@ 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).
#### page.setOfflineMode(enabled)
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- returns: <[Promise]>

#### page.setRequestInterceptionEnabled(value)
- `value` <[boolean]> Whether to enable request interception.
- returns: <[Promise]>
Expand Down
18 changes: 18 additions & 0 deletions lib/NetworkManager.js
Expand Up @@ -32,6 +32,8 @@ class NetworkManager extends EventEmitter {
/** @type {!Object<string, string>} */
this._extraHTTPHeaders = {};

this._offline = false;

/** @type {?{username: string, password: string}} */
this._credentials = null;
/** @type {!Set<string>} */
Expand Down Expand Up @@ -78,6 +80,22 @@ class NetworkManager extends EventEmitter {
return Object.assign({}, this._extraHTTPHeaders);
}

/**
* @param {boolean} value
*/
async setOfflineMode(value) {
if (this._offline === value)
return;
this._offline = value;
await this._client.send('Network.emulateNetworkConditions', {
offline: this._offline,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: -1,
uploadThroughput: -1
});
}

/**
* @param {string} userAgent
*/
Expand Down
7 changes: 7 additions & 0 deletions lib/Page.js
Expand Up @@ -149,6 +149,13 @@ class Page extends EventEmitter {
return this._networkManager.setRequestInterceptionEnabled(value);
}

/**
* @param {boolean} enabled
*/
setOfflineMode(enabled) {
return this._networkManager.setOfflineMode(enabled);
}

/**
* @param {!Object} event
*/
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -328,6 +328,25 @@ describe('Page', function() {
}));
});

describe('Page.setOfflineMode', function() {
it('should work', SX(async function() {
await page.setOfflineMode(true);
let error = null;
await page.goto(EMPTY_PAGE).catch(e => error = e);
expect(error).toBeTruthy();
await page.setOfflineMode(false);
const response = await page.reload();
expect(response.status).toBe(200);
}));
it('should emulate navigator.onLine', SX(async function() {
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
await page.setOfflineMode(true);
expect(await page.evaluate(() => window.navigator.onLine)).toBe(false);
await page.setOfflineMode(false);
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
}));
});

describe('Page.evaluateHandle', function() {
it('should work', SX(async function() {
const windowHandle = await page.evaluateHandle(() => window);
Expand Down

0 comments on commit a02347e

Please sign in to comment.