Skip to content

Conversation

@abalone0204
Copy link
Contributor

@abalone0204 abalone0204 commented Apr 18, 2018

Still Work in progress, this is my first contribution to puppeteer.

Just want to tell you guys this project does benefit tons of developers, great job 😎

Description

Add $eval to ElementHandle and also refactor the frame.$eval with elementHandle.$eval.

Fixes #2401.

Progress

  • feature implmentation
  • code review
  • $eval:
    • lint: npm run lint
      • eslint
      • doc lint
    • API guidelines
    • Unit test
  • $$eval:
    • lint: npm run lint
      • eslint
      • doc lint
    • API guidelines
    • Unit test

Add `$eval`, `$$eval`,`mainFrame` to ElementHandle.

Fixes puppeteer#2401
Remove trailing space.
* @return {!Promise<(!Object|undefined)>}
*/
async $eval(selector, pageFunction, ...args) {
return this.mainFrame().$eval(selector, pageFunction, ...args);
Copy link
Contributor

@yanivefraim yanivefraim Apr 18, 2018

Choose a reason for hiding this comment

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

@abalone0204, thanks for your first PR!

as far as I understand, the idea is to have a selector on the subtree of a selected element (from the issue: I'd like to get a reference to a DOM node and run one-liners against the subtree.)

here you have a new selector on mainFrame, which is not necessarily on element's subtree.

For example:

<div class="a">not-a-child-div</div>
<div id="my-id">
  <div class="a">a-child-div</div>
</div> 

Now, with your implementation, the following test will fail:

const handle = await page.$('#my-id');
expect(await handle.$eval('.a', node => node.innerText)).toBe('a-child-div');

it can actually be a good test case for you (:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yanivefraim Thanks!
I can dig on it now.

Reimplement the `$eval` function
Test `$eval`.
* @return {!Promise<(!Object|undefined)>}
*/
async $eval(selector, pageFunction, ...args) {
const elementHandle = await this.$(selector);
Copy link
Contributor

Choose a reason for hiding this comment

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

once you have this in place, you can re-use this inside Frame.$eval, similarly to how it's done for Frame.$.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, Just finished it.

Refactor `frame.$eval` with `elementHandle.$eval` according to reviewer.
Add doc for `elementHandle` and provide an example.
Move `$eval` to right place.
@aslushnikov
Copy link
Contributor

@abalone0204 the code is good. Do you plan to do $$eval as a part of this PR?

@abalone0204
Copy link
Contributor Author

@aslushnikov Thank you. Glad to hear that.
Yes, I'm working on it now.

@aslushnikov
Copy link
Contributor

@abalone0204 Someone marked checkboxes for "$$eval" for this PR; however, I don't see any code related to this. Some kind of error?

@yanivefraim
Copy link
Contributor

@abalone0204 do you need help here? another option is to add $eval and leave $$eval for another PR

Copy link
Contributor

@aslushnikov aslushnikov left a comment

Choose a reason for hiding this comment

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

I'll merge this as-is; the page.$$eval is yet to be implemented.

@aslushnikov aslushnikov merged commit 88b9968 into puppeteer:master May 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants