Skip to content

Commit

Permalink
Introduce page.goBack/page.goForward (puppeteer#93)
Browse files Browse the repository at this point in the history
This patch introduces page.goBack/page.goForward methods
to navigate the page history.

References puppeteer#89.
  • Loading branch information
pavelfeldman authored and aslushnikov committed Jul 19, 2017
1 parent 98c3894 commit f154d53
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
16 changes: 15 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* [page.evaluateOnInitialized(pageFunction, ...args)](#pageevaluateoninitializedpagefunction-args)
* [page.focus(selector)](#pagefocusselector)
* [page.frames()](#pageframes)
* [page.goBack(options)](#pagegobackoptions)
* [page.goForward(options)](#pagegoforwardoptions)
* [page.httpHeaders()](#pagehttpheaders)
* [page.injectFile(filePath)](#pageinjectfilefilepath)
* [page.keyboard](#pagekeyboard)
Expand Down Expand Up @@ -343,11 +345,23 @@ This is a shortcut for [page.mainFrame().evaluate()](#frameevaluatefun-args) met
#### page.frames()
- returns: <[Array]<[Frame]>> An array of all frames attached to the page.

#### page.goBack(options)
- `options` <[Object]> Navigation parameters, same as in [page.navigate](#pagenavigateurl-options).
- returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If
can not go back, resolves to null.

Navigate to the previous page in history.

#### page.goForward(options)
- `options` <[Object]> Navigation parameters, same as in [page.navigate](#pagenavigateurl-options).
- returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If
can not go back, resolves to null.

Navigate to the next page in history.

#### page.httpHeaders()
- returns: <[Object]> Key-value set of additional http headers which will be sent with every request.


#### page.injectFile(filePath)
- `filePath` <[string]> Path to the javascript file to be injected into page.
- returns: <[Promise]> Promise which resolves when file gets successfully evaluated in page.
Expand Down
32 changes: 31 additions & 1 deletion lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,44 @@ class Page extends EventEmitter {

/**
* @param {!Object=} options
* @return {!Promise<?Response>}
* @return {!Promise<!Response>}
*/
async waitForNavigation(options) {
const watcher = new NavigatorWatcher(this._client, this._networkManager, options);
const responses = await watcher.waitForNavigation();
return responses.get(this.mainFrame().url()) || null;
}

/**
* @param {!Object=} options
* @return {!Promise<?Response>}
*/
async goBack(options) {
return this._go(-1, options);
}

/**
* @param {!Object=} options
* @return {!Promise<?Response>}
*/
async goForward(options) {
return this._go(+1, options);
}

/**
* @param {!Object=} options
* @return {!Promise<?Response>}
*/
async _go(delta, options) {
const history = await this._client.send('Page.getNavigationHistory');
const entry = history.entries[history.currentIndex + delta];
if (!entry)
return null;
const result = this.waitForNavigation(options);
this._client.send('Page.navigateToHistoryEntry', {entryId: entry.id});
return result;
}

/**
* @param {!Page.Viewport} viewport
* @return {!Promise}
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ describe('Puppeteer', function() {
}));
});

describe('Page.goBack', function() {
it('should work', SX(async function() {
await page.navigate(EMPTY_PAGE);
await page.navigate(PREFIX + '/grid.html');

let response = await page.goBack();
expect(response.ok).toBe(true);
expect(response.url).toContain(EMPTY_PAGE);

response = await page.goForward();
expect(response.ok).toBe(true);
expect(response.url).toContain('/grid.html');

response = await page.goForward();
expect(response).toBe(null);
}));
});

describe('Page.setInPageCallback', function() {
it('should work', SX(async function() {
await page.setInPageCallback('callController', function(a, b) {
Expand Down

0 comments on commit f154d53

Please sign in to comment.