From 9292a56eafabf8dbe8ef0fb0fffe0db0ca4020eb Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 11 Sep 2017 19:20:02 -0700 Subject: [PATCH] [api] Teach page.evaluate to accept element handles as parameters (#725) This patch: - teaches `page.evaluate` to accept ElementHandles as parameters - removes `ElementHandle.evaluate` method since it's not needed any more References #382 --- docs/api.md | 59 ++++++++++++++------------------ lib/ElementHandle.js | 34 +++++++------------ lib/FrameManager.js | 51 ++++++++++++++++++++++++---- lib/Page.js | 2 +- test/test.js | 80 +++++++++++++++++++------------------------- 5 files changed, 118 insertions(+), 108 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5e9be546..22b0b376 100644 --- a/docs/api.md +++ b/docs/api.md @@ -116,7 +116,6 @@ * [class: ElementHandle](#class-elementhandle) + [elementHandle.click([options])](#elementhandleclickoptions) + [elementHandle.dispose()](#elementhandledispose) - + [elementHandle.evaluate(pageFunction, ...args)](#elementhandleevaluatepagefunction-args) + [elementHandle.hover()](#elementhandlehover) + [elementHandle.tap()](#elementhandletap) + [elementHandle.uploadFile(...filePaths)](#elementhandleuploadfilefilepaths) @@ -333,7 +332,7 @@ Shortcut for [page.mainFrame().$$(selector)](#frameselector-1). #### page.$eval(selector, pageFunction[, ...args]) - `selector` <[string]> A [selector] to query page for - `pageFunction` <[function]> Function to be evaluated in browser context -- `...args` <...[Serializable]> Arguments to pass to `pageFunction` +- `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction` - returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of `pageFunction` This method runs `document.querySelector` within the page and passes it as the first argument to `pageFunction`. If there's no element matching `selector`, the method throws an error. @@ -450,22 +449,16 @@ List of all available devices is available in the source code: [DeviceDescriptor #### page.evaluate(pageFunction, ...args) - `pageFunction` <[function]|[string]> Function to be evaluated in the page context -- `...args` <...[Serializable]> Arguments to pass to `pageFunction` +- `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction` - returns: <[Promise]<[Serializable]>> Resolves to the return value of `pageFunction` If the function, passed to the `page.evaluate`, returns a [Promise], then `page.evaluate` would wait for the promise to resolve and return it's value. ```js -const puppeteer = require('puppeteer'); - -puppeteer.launch().then(async browser => { - const page = await browser.newPage(); - const result = await page.evaluate(() => { - return Promise.resolve(8 * 7); - }); - console.log(result); // prints "56" - browser.close(); +const result = await page.evaluate(() => { + return Promise.resolve(8 * 7); }); +console.log(result); // prints "56" ``` A string can also be passed in instead of a function. @@ -474,6 +467,13 @@ A string can also be passed in instead of a function. console.log(await page.evaluate('1 + 2')); // prints "3" ``` +[ElementHandle] instances could be passed as arguments to the `page.evaluate`: +```js +const bodyHandle = await page.$('body'); +const html = await page.evaluate(body => body.innerHTML, bodyHandle); +await bodyHandle.dispose(); +``` + Shortcut for [page.mainFrame().evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args). #### page.evaluateOnNewDocument(pageFunction, ...args) @@ -1113,7 +1113,7 @@ The method runs `document.querySelectorAll` within the frame. If no elements mat #### frame.$eval(selector, pageFunction[, ...args]) - `selector` <[string]> A [selector] to query frame for - `pageFunction` <[function]> Function to be evaluated in browser context -- `...args` <...[Serializable]> Arguments to pass to `pageFunction` +- `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction` - returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of `pageFunction` This method runs `document.querySelector` within the frame and passes it as the first argument to `pageFunction`. If there's no element matching `selector`, the method throws an error. @@ -1138,28 +1138,29 @@ Adds a `