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

Implement page.$$ method #463

Merged
merged 3 commits into from
Aug 23, 2017

Conversation

aslushnikov
Copy link
Contributor

This patch implements page.$$ that runs document.querySelectorAll
in page and returns results as an array of ElementHandle instances.

Fixes #384.

docs/api.md Outdated
- `selector` <[string]> Selector to query page for
- returns: <[Promise]<[Array]<[ElementHandle]>>> Promise which resolves to ElementHandles pointing to the page elements.

The method queries page for all elements matching selector. If there are no such elements within the page, the method will resolve to `[]`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's have the description here as above.

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

test/test.js Outdated
it('should query existing elements', SX(async function() {
await page.setContent('<div>A</div><br/><div>B</div>');
let elements = await page.$$('div');
expect(elements.length).toBe(2);
Copy link
Contributor

@JoelEinbinder JoelEinbinder Aug 22, 2017

Choose a reason for hiding this comment

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

Let's dump the content, so we at least know that it is creating ElementHandles.

expect(await Promise.all(elements.map(e => e.evaluate(g => g.textContent)))).toEqual(['A','B'])

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.

@@ -27,6 +27,7 @@
+ [event: 'requestfinished'](#event-requestfinished)
+ [event: 'response'](#event-response)
+ [page.$(selector)](#pageselector)
+ [page.$$(selector)](#pageselector)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be #pageselector-1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, this is a bug in the toc-generator.

@@ -85,6 +86,7 @@
+ [dialog.type](#dialogtype)
* [class: Frame](#class-frame)
+ [frame.$(selector)](#frameselector)
+ [frame.$$(selector)](#frameselector)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be #frameselector-1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto here

docs/api.md Outdated

The method runs `document.querySelectorAll` within the page. If no elements match the selector, the return value resolve to `[]`.

Shortcut for [page.mainFrame().$$(selector)](#frameselector).
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be #frameselector-1

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

docs/api.md Outdated
@@ -978,6 +988,11 @@ puppeteer.launch().then(async browser => {

The method queries page for the selector. If there's no such element within the page, the method will resolve to `null`.

#### frame.$$(selector)
- `selector` <[string]> Selector to query page for
- returns: <[Promise]<[Array]<[ElementHandle]>>> Promise which resolves to ElementHandles pointing to the page elements.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder, maybe all the page instances should be replaced by frame in frame API descriptions when appropriate?

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.

This patch implements page.$$ which runs document.querySelectorAll
in page and returns results as an array of ElementHandle instances.

Fixes puppeteer#384.
@aslushnikov aslushnikov merged commit 151d512 into puppeteer:master Aug 23, 2017
const promises = elements.map(element => element.evaluate(e => e.textContent));
expect(await Promise.all(promises)).toEqual(['A', 'B']);
}));
it('should return ampty array if nothing is found', SX(async function() {

Choose a reason for hiding this comment

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

ampty typo 😂 ?

@aslushnikov aslushnikov deleted the query-selector-all branch August 27, 2017 22:09
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.

4 participants