Skip to content

Implement object handles #382

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

Closed
pavelfeldman opened this issue Aug 18, 2017 · 9 comments
Closed

Implement object handles #382

pavelfeldman opened this issue Aug 18, 2017 · 9 comments
Assignees

Comments

@pavelfeldman
Copy link
Contributor

  • Add page.object(func) that returns a new ObjectHandle type.
  • Expose ObjectHandle.json() that would return result similar to present page.evaluate. That will allow access to the raw object handlers for non-trivial operations.
  • Inherit ElementHandle from ObjectHandle if possible
  • Allow object handles to be disposed, expose evaluateOn on the objects
  • Provide support for object groups (arenas) for bulk dispose
@aslushnikov
Copy link
Contributor

The ObjectHandle may have a objectHandle.property('foo') method that returns value of the property named foo. This will address #438.

@aslushnikov aslushnikov self-assigned this Aug 24, 2017
aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Sep 8, 2017
This patch:
- teaches `page.evaluate` to accept ElementHandles as parameters
- removes `ElementHandle.evaluate` method since it's not needed any
  more

References puppeteer#382
aslushnikov added a commit that referenced this issue Sep 12, 2017
This patch:
- teaches `page.evaluate` to accept ElementHandles as parameters
- removes `ElementHandle.evaluate` method since it's not needed any
  more

References #382
aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Oct 2, 2017
This patch:
- gives meaningful names to doclint tests
- supports classes inheritance in documentation linter. When class A
  extends class B, all methods of class B are added to documentation of
  class A.

This is a prerequisite for Object Handles: ElementHandle will be
extending ObjectHandle.

References puppeteer#382
aslushnikov added a commit that referenced this issue Oct 2, 2017
This patch:
- gives meaningful names to doclint tests
- supports classes inheritance in documentation linter. When class A
  extends class B, all methods of class B are added to documentation of
  class A.

This is a prerequisite for Object Handles: ElementHandle will be
extending ObjectHandle.

References #382
aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Oct 6, 2017
This patch:
- introduces ExecutionContext class that incapsulates javascript
  execution context. An examples of execution contexts are workers and
  frames
- introduces ObjectHandle that holds a references to the javascript
  object in ExecutionContext
- inherits ElementHandle from ObjectHandle

Fixes puppeteer#382.
aslushnikov added a commit that referenced this issue Oct 6, 2017
This patch:
- introduces ExecutionContext class that incapsulates javascript
  execution context. An examples of execution contexts are workers and
  frames
- introduces JSHandle that holds a references to the javascript
  object in ExecutionContext
- inherits ElementHandle from JSHandle

Fixes #382.
@gajus
Copy link

gajus commented Oct 8, 2017

The ObjectHandle may have a objectHandle.property('foo') method that returns value of the property named foo. This will address #438.

How does one obtain the property value from the JSHandle?

const totalCostElement = await page.$('.review-payment__cost-breakdown strong');

const totalCostElementTextContent = await totalCostElement.getProperty('textContent');

This returns JSHandle object.

@gajus
Copy link

gajus commented Oct 8, 2017

Turns out you simply need to use toString.

This API is rather odd. Whats the reason for JSHandle abstraction in the first place?

@pavelfeldman
Copy link
Contributor Author

@aslushnikov let's remove toString before it is misused broadly. I don't remember reviewing it btw, I remember renaming old toString into jsonString, but that was it...

@gajus
Copy link

gajus commented Oct 9, 2017

@aslushnikov let's remove toString before it is misused broadly. I don't remember reviewing it btw, I remember renaming old toString into jsonString, but that was it...

Whats the correct way to get the property value?

@aslushnikov
Copy link
Contributor

@gajus it's JSHandle.jsonValue

aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Oct 10, 2017
This patch:
- updates JSHandle.toString to make a nicer description for primitives
- excludes JSHandle.toString from documentation to avoid its abuse

References puppeteer#382
@gajus
Copy link

gajus commented Oct 10, 2017

Returns a JSON representation of the object. The JSON is generated by running JSON.stringify on the object in page and consequent JSON.parse in puppeteer.

This description makes it in no way clear that jsonValue returns the value of a property.

aslushnikov added a commit that referenced this issue Oct 10, 2017
This patch:
- updates JSHandle.toString to make a nicer description for primitives
- excludes JSHandle.toString from documentation to avoid its abuse

References #382
@aslushnikov
Copy link
Contributor

aslushnikov commented Oct 10, 2017

@gajus jsonValue returns a value of the object.

If you want a value of particular object property, you'd need to use both getProperty() and jsonValue():

const navigatorHandle = await page.evaluateHandle(() => window.navigator);
const uaHandle = await navigatorHandle.getProperty('navigator');
const ua = await uaHandle.jsonValue();

Alternatively, you can use the evaluate method:

const navigatorHandle = await page.evaluateHandle(() => window.navigator);
const ua = await page.evaluate(obj => obj.userAgent, navigatorHandle);

ithinkihaveacat pushed a commit to ithinkihaveacat/puppeteer that referenced this issue Oct 31, 2017
…#993)

This patch:
- updates JSHandle.toString to make a nicer description for primitives
- excludes JSHandle.toString from documentation to avoid its abuse

References puppeteer#382
@AgainPsychoX
Copy link

When do I need to call dispose of the handles?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants