Skip to content
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

feat(Page): introduce Page.queryObjects #1005

Merged
merged 3 commits into from Oct 11, 2017

Conversation

aslushnikov
Copy link
Contributor

This patch introduces Page.queryObjects and
ExecutionContext.queryObjects methods to query JavaScript heap
for objects with a certain prototype.

Fixes #304.

This patch introduces Page.queryObjects and
ExecutionContext.queryObjects methods to query JavaScript heap
for objects with a certain prototype.

Fixes puppeteer#304.
test/test.js Outdated
await page.evaluate(() => window.map = new Map());
const prototypeHandle = await page.evaluateHandle(() => Map.prototype);
const objectsHandle = await page.queryObjects(prototypeHandle);
const count = await page.evaluate(objects => objects.length, objectsHandle);
Copy link
Collaborator

Choose a reason for hiding this comment

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

To make me feel safer, can we actually check that the object here is the map and is usable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@JoelEinbinder
Copy link
Collaborator

This is really exciting. It feels a bit clumsy to use, but maybe that is ok. I was hoping for something like page.queryObjects(string) -> Array<JSHandle>, but I can easily write that helper myself.

@aslushnikov
Copy link
Contributor Author

aslushnikov commented Oct 11, 2017

This is really exciting. It feels a bit clumsy to use, but maybe that is ok. I was hoping for something like page.queryObjects(string) -> Array, but I can easily write that helper myself.

Disposing an array of JSHandles is not very nice and talkative from protocol's point of view:

await Promise.all(handles.map(handle => handle.dispose()));

Most of the time you'll be satisfied with inspecting the objects with page.evaluate, thus this approach:

const objectPrototype = await page.evaluateHandle(() => Object.prototype);
const objectsHandle = await page.queryObjects(objectPrototype);
const count = await page.evaluate(objects => objects.length, objectsHandle);

Copy link
Contributor

@alexkozy alexkozy left a comment

Choose a reason for hiding this comment

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

Nice!

@aslushnikov aslushnikov merged commit 23c0ba0 into puppeteer:master Oct 11, 2017
ithinkihaveacat pushed a commit to ithinkihaveacat/puppeteer that referenced this pull request Oct 31, 2017
This patch introduces `Page.queryObjects` and
`ExecutionContext.queryObjects` methods to query JavaScript heap
for objects with a certain prototype.

Fixes puppeteer#304.
@aslushnikov aslushnikov deleted the query-objects branch January 25, 2018 21:53
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.

None yet

3 participants