Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Page): implement page.setOfflineMode #1032

Merged
merged 1 commit into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check error message here? Also what is the error here? Timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoelEinbinder it's currently "Failed to navigate!", since we don't report errors for now. It won't be a timeout though: it gonna be ERR_INTERNET_DISCONNECTED

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok good for now I guess. Would be nice to report a more descriptive error here. Also how do I automate the dino game? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to report a more descriptive error here

Yes, hopefully it would be easier to do with plznavigate enabled.

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