Introduce page.goBack/page.goForward (#93)

This patch introduces page.goBack/page.goForward methods
to navigate the page history.

References #89.
This commit is contained in:
Pavel Feldman 2017-07-18 19:11:37 -07:00 committed by Andrey Lushnikov
parent 98c3894c84
commit f154d537f7
3 changed files with 64 additions and 2 deletions

View File

@ -35,6 +35,8 @@
* [page.evaluateOnInitialized(pageFunction, ...args)](#pageevaluateoninitializedpagefunction-args) * [page.evaluateOnInitialized(pageFunction, ...args)](#pageevaluateoninitializedpagefunction-args)
* [page.focus(selector)](#pagefocusselector) * [page.focus(selector)](#pagefocusselector)
* [page.frames()](#pageframes) * [page.frames()](#pageframes)
* [page.goBack(options)](#pagegobackoptions)
* [page.goForward(options)](#pagegoforwardoptions)
* [page.httpHeaders()](#pagehttpheaders) * [page.httpHeaders()](#pagehttpheaders)
* [page.injectFile(filePath)](#pageinjectfilefilepath) * [page.injectFile(filePath)](#pageinjectfilefilepath)
* [page.keyboard](#pagekeyboard) * [page.keyboard](#pagekeyboard)
@ -343,11 +345,23 @@ This is a shortcut for [page.mainFrame().evaluate()](#frameevaluatefun-args) met
#### page.frames() #### page.frames()
- returns: <[Array]<[Frame]>> An array of all frames attached to the page. - 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() #### page.httpHeaders()
- returns: <[Object]> Key-value set of additional http headers which will be sent with every request. - returns: <[Object]> Key-value set of additional http headers which will be sent with every request.
#### page.injectFile(filePath) #### page.injectFile(filePath)
- `filePath` <[string]> Path to the javascript file to be injected into page. - `filePath` <[string]> Path to the javascript file to be injected into page.
- returns: <[Promise]> Promise which resolves when file gets successfully evaluated in page. - returns: <[Promise]> Promise which resolves when file gets successfully evaluated in page.

View File

@ -291,7 +291,7 @@ class Page extends EventEmitter {
/** /**
* @param {!Object=} options * @param {!Object=} options
* @return {!Promise<?Response>} * @return {!Promise<!Response>}
*/ */
async waitForNavigation(options) { async waitForNavigation(options) {
const watcher = new NavigatorWatcher(this._client, this._networkManager, options); const watcher = new NavigatorWatcher(this._client, this._networkManager, options);
@ -299,6 +299,36 @@ class Page extends EventEmitter {
return responses.get(this.mainFrame().url()) || null; 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 * @param {!Page.Viewport} viewport
* @return {!Promise} * @return {!Promise}

View File

@ -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() { describe('Page.setInPageCallback', function() {
it('should work', SX(async function() { it('should work', SX(async function() {
await page.setInPageCallback('callController', function(a, b) { await page.setInPageCallback('callController', function(a, b) {