puppeteer/test/src/queryselector.spec.ts

492 lines
16 KiB
TypeScript
Raw Normal View History

/**
2024-01-03 10:11:33 +00:00
* @license
* Copyright 2018 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import expect from 'expect';
import {Puppeteer} from 'puppeteer';
import type {CustomQueryHandler} from 'puppeteer-core/internal/common/CustomQueryHandler.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
2020-05-07 10:54:55 +00:00
describe('querySelector', function () {
setupTestBrowserHooks();
describe('Page.$eval', function () {
2020-05-07 10:54:55 +00:00
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<section id="testAttribute">43543</section>');
const idAttribute = await page.$eval('section', e => {
return e.id;
});
expect(idAttribute).toBe('testAttribute');
});
2020-05-07 10:54:55 +00:00
it('should accept arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<section>hello</section>');
2020-05-07 10:54:55 +00:00
const text = await page.$eval(
'section',
(e, suffix) => {
return e.textContent! + suffix;
},
2020-05-07 10:54:55 +00:00
' world!'
);
expect(text).toBe('hello world!');
});
2020-05-07 10:54:55 +00:00
it('should accept ElementHandles as arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<section>hello</section><div> world</div>');
using divHandle = (await page.$('div'))!;
2020-05-07 10:54:55 +00:00
const text = await page.$eval(
'section',
(e, div) => {
return e.textContent! + (div as HTMLElement).textContent!;
},
2020-05-07 10:54:55 +00:00
divHandle
);
expect(text).toBe('hello world');
});
2020-05-07 10:54:55 +00:00
it('should throw error if no element is found', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
let error!: Error;
2020-05-07 10:54:55 +00:00
await page
.$eval('section', e => {
return e.id;
})
.catch(error_ => {
return (error = error_);
});
2020-05-07 10:54:55 +00:00
expect(error.message).toContain(
'failed to find element matching selector "section"'
);
});
});
// The tests for $$eval are repeated later in this file in the test group 'QueryAll'.
// This is done to also test a query handler where QueryAll returns an Element[]
// as opposed to NodeListOf<Element>.
describe('Page.$$eval', function () {
2020-05-07 10:54:55 +00:00
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<div>hello</div><div>beautiful</div><div>world!</div>'
);
const divsCount = await page.$$eval('div', divs => {
return divs.length;
});
expect(divsCount).toBe(3);
});
it('should accept extra arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<div>hello</div><div>beautiful</div><div>world!</div>'
);
const divsCountPlus5 = await page.$$eval(
'div',
(divs, two, three) => {
return divs.length + (two as number) + (three as number);
},
2,
3
);
expect(divsCountPlus5).toBe(8);
});
it('should accept ElementHandles as arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<section>2</section><section>2</section><section>1</section><div>3</div>'
);
using divHandle = (await page.$('div'))!;
const sum = await page.$$eval(
'section',
(sections, div) => {
return (
sections.reduce((acc, section) => {
return acc + Number(section.textContent);
}, 0) + Number((div as HTMLElement).textContent)
);
},
divHandle
);
expect(sum).toBe(8);
});
it('should handle many elements', async function () {
this.timeout(25_000);
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.evaluate(
`
for (var i = 0; i <= 1000; i++) {
const section = document.createElement('section');
section.textContent = i;
document.body.appendChild(section);
}
`
);
const sum = await page.$$eval('section', sections => {
return sections.reduce((acc, section) => {
return acc + Number(section.textContent);
}, 0);
});
expect(sum).toBe(500500);
});
});
describe('Page.$', function () {
2020-05-07 10:54:55 +00:00
it('should query existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<section>test</section>');
using element = (await page.$('section'))!;
expect(element).toBeTruthy();
});
2020-05-07 10:54:55 +00:00
it('should return null for non-existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using element = (await page.$('non-existing-element'))!;
expect(element).toBe(null);
});
});
2020-05-07 10:54:55 +00:00
describe('Page.$$', function () {
it('should query existing elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<div>A</div><br/><div>B</div>');
const elements = await page.$$('div');
expect(elements).toHaveLength(2);
const promises = elements.map(element => {
return page.evaluate((e: HTMLElement) => {
return e.textContent;
}, element);
});
expect(await Promise.all(promises)).toEqual(['A', 'B']);
});
2020-05-07 10:54:55 +00:00
it('should return empty array if nothing is found', async () => {
2023-06-21 19:41:09 +00:00
const {page, server} = await getTestState();
await page.goto(server.EMPTY_PAGE);
const elements = await page.$$('div');
expect(elements).toHaveLength(0);
});
});
describe('Page.$x', function () {
2020-05-07 10:54:55 +00:00
it('should query existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<section>test</section>');
const elements = await page.$x('/html/body/section');
expect(elements[0]).toBeTruthy();
expect(elements).toHaveLength(1);
});
2020-05-07 10:54:55 +00:00
it('should return empty array for non-existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
const element = await page.$x('/html/body/non-existing-element');
expect(element).toEqual([]);
});
2020-05-07 10:54:55 +00:00
it('should return multiple elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<div></div><div></div>');
const elements = await page.$x('/html/body/div');
expect(elements).toHaveLength(2);
});
});
2020-05-07 10:54:55 +00:00
describe('ElementHandle.$', function () {
it('should query existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page, server} = await getTestState();
await page.goto(server.PREFIX + '/playground.html');
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="second"><div class="inner">A</div></div></body></html>'
);
using html = (await page.$('html'))!;
using second = (await html.$('.second'))!;
using inner = await second.$('.inner');
2022-06-24 06:40:08 +00:00
const content = await page.evaluate(e => {
return e?.textContent;
}, inner);
expect(content).toBe('A');
});
it('should return null for non-existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="second"><div class="inner">B</div></div></body></html>'
);
using html = (await page.$('html'))!;
using second = await html.$('.third');
expect(second).toBe(null);
});
});
describe('ElementHandle.$eval', function () {
2020-05-07 10:54:55 +00:00
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="tweet"><div class="like">100</div><div class="retweets">10</div></div></body></html>'
);
using tweet = (await page.$('.tweet'))!;
const content = await tweet.$eval('.like', node => {
return (node as HTMLElement).innerText;
});
expect(content).toBe('100');
});
2020-05-07 10:54:55 +00:00
it('should retrieve content from subtree', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
const htmlContent =
'<div class="a">not-a-child-div</div><div id="myId"><div class="a">a-child-div</div></div>';
await page.setContent(htmlContent);
using elementHandle = (await page.$('#myId'))!;
const content = await elementHandle.$eval('.a', node => {
return (node as HTMLElement).innerText;
});
expect(content).toBe('a-child-div');
});
2020-05-07 10:54:55 +00:00
it('should throw in case of missing selector', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
const htmlContent =
'<div class="a">not-a-child-div</div><div id="myId"></div>';
await page.setContent(htmlContent);
using elementHandle = (await page.$('#myId'))!;
2020-05-07 10:54:55 +00:00
const errorMessage = await elementHandle
.$eval('.a', node => {
return (node as HTMLElement).innerText;
})
.catch(error => {
return error.message;
});
2020-05-07 10:54:55 +00:00
expect(errorMessage).toBe(
`Error: failed to find element matching selector ".a"`
);
});
});
describe('ElementHandle.$$eval', function () {
2020-05-07 10:54:55 +00:00
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="tweet"><div class="like">100</div><div class="like">10</div></div></body></html>'
);
using tweet = (await page.$('.tweet'))!;
const content = await tweet.$$eval('.like', nodes => {
return (nodes as HTMLElement[]).map(n => {
return n.innerText;
});
});
expect(content).toEqual(['100', '10']);
});
2020-05-07 10:54:55 +00:00
it('should retrieve content from subtree', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
const htmlContent =
'<div class="a">not-a-child-div</div><div id="myId"><div class="a">a1-child-div</div><div class="a">a2-child-div</div></div>';
await page.setContent(htmlContent);
using elementHandle = (await page.$('#myId'))!;
const content = await elementHandle.$$eval('.a', nodes => {
return (nodes as HTMLElement[]).map(n => {
return n.innerText;
});
});
expect(content).toEqual(['a1-child-div', 'a2-child-div']);
});
2020-05-07 10:54:55 +00:00
it('should not throw in case of missing selector', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
const htmlContent =
'<div class="a">not-a-child-div</div><div id="myId"></div>';
await page.setContent(htmlContent);
using elementHandle = (await page.$('#myId'))!;
const nodesLength = await elementHandle.$$eval('.a', nodes => {
return nodes.length;
});
expect(nodesLength).toBe(0);
});
});
describe('ElementHandle.$$', function () {
2020-05-07 10:54:55 +00:00
it('should query existing elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div>A</div><br/><div>B</div></body></html>'
);
using html = (await page.$('html'))!;
const elements = await html.$$('div');
expect(elements).toHaveLength(2);
const promises = elements.map(element => {
return page.evaluate((e: HTMLElement) => {
return e.textContent;
}, element);
});
expect(await Promise.all(promises)).toEqual(['A', 'B']);
});
2020-05-07 10:54:55 +00:00
it('should return empty array for non-existing elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><span>A</span><br/><span>B</span></body></html>'
);
using html = (await page.$('html'))!;
const elements = await html.$$('div');
expect(elements).toHaveLength(0);
});
});
2020-05-07 10:54:55 +00:00
describe('ElementHandle.$x', function () {
it('should query existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page, server} = await getTestState();
await page.goto(server.PREFIX + '/playground.html');
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="second"><div class="inner">A</div></div></body></html>'
);
using html = (await page.$('html'))!;
const second = await html.$x(`./body/div[contains(@class, 'second')]`);
const inner = await second[0]!.$x(`./div[contains(@class, 'inner')]`);
feat!: type inference for evaluation types (#8547) This PR greatly improves the types within Puppeteer: - **Almost everything** is auto-deduced. - Parameters don't need to be specified in the function. They are deduced from the spread. - Return types don't need to be specified. They are deduced from the function. (More on this below) - Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`. - [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons: 1. Setting them will indubitably break auto-deduction. 2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense). - [**BREAKING CHANGE**] `waitFor` is officially gone. To migrate to these changes, there are only four things you may need to change: - If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary). ⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType` ⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>` - If you set any type parameters in the *parameters* of an evaluation function, remove them. ⛔ `evaluate(a: number, b: number) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)` - If you set any type parameters in the method's declaration, remove them. ⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
const content = await page.evaluate(e => {
return e.textContent;
}, inner[0]!);
expect(content).toBe('A');
});
it('should return null for non-existing element', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
await page.setContent(
'<html><body><div class="second"><div class="inner">B</div></div></body></html>'
);
using html = (await page.$('html'))!;
const second = await html.$x(`/div[contains(@class, 'third')]`);
expect(second).toEqual([]);
});
});
// This is the same tests for `$$eval` and `$$` as above, but with a queryAll
// handler that returns an array instead of a list of nodes.
describe('QueryAll', function () {
const handler: CustomQueryHandler = {
queryAll: (element, selector) => {
return [...(element as Element).querySelectorAll(selector)];
},
};
before(() => {
Puppeteer.registerCustomQueryHandler('allArray', handler);
2020-10-23 14:28:38 +00:00
});
it('should have registered handler', async () => {
expect(
Puppeteer.customQueryHandlerNames().includes('allArray')
2020-10-23 14:28:38 +00:00
).toBeTruthy();
});
it('$$ should query existing elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<html><body><div>A</div><br/><div>B</div></body></html>'
);
using html = (await page.$('html'))!;
const elements = await html.$$('allArray/div');
expect(elements).toHaveLength(2);
const promises = elements.map(element => {
feat!: type inference for evaluation types (#8547) This PR greatly improves the types within Puppeteer: - **Almost everything** is auto-deduced. - Parameters don't need to be specified in the function. They are deduced from the spread. - Return types don't need to be specified. They are deduced from the function. (More on this below) - Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`. - [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons: 1. Setting them will indubitably break auto-deduction. 2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense). - [**BREAKING CHANGE**] `waitFor` is officially gone. To migrate to these changes, there are only four things you may need to change: - If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary). ⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType` ⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>` - If you set any type parameters in the *parameters* of an evaluation function, remove them. ⛔ `evaluate(a: number, b: number) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)` - If you set any type parameters in the method's declaration, remove them. ⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
return page.evaluate(e => {
return e.textContent;
}, element);
});
expect(await Promise.all(promises)).toEqual(['A', 'B']);
});
it('$$ should return empty array for non-existing elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<html><body><span>A</span><br/><span>B</span></body></html>'
);
using html = (await page.$('html'))!;
const elements = await html.$$('allArray/div');
expect(elements).toHaveLength(0);
});
it('$$eval should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<div>hello</div><div>beautiful</div><div>world!</div>'
);
const divsCount = await page.$$eval('allArray/div', divs => {
return divs.length;
});
expect(divsCount).toBe(3);
});
it('$$eval should accept extra arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<div>hello</div><div>beautiful</div><div>world!</div>'
);
const divsCountPlus5 = await page.$$eval(
'allArray/div',
(divs, two, three) => {
return divs.length + (two as number) + (three as number);
},
2,
3
);
expect(divsCountPlus5).toBe(8);
});
it('$$eval should accept ElementHandles as arguments', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent(
'<section>2</section><section>2</section><section>1</section><div>3</div>'
);
using divHandle = (await page.$('div'))!;
const sum = await page.$$eval(
'allArray/section',
(sections, div) => {
return (
sections.reduce((acc, section) => {
return acc + Number(section.textContent);
}, 0) + Number((div as HTMLElement).textContent)
);
},
divHandle
);
expect(sum).toBe(8);
});
it('$$eval should handle many elements', async function () {
this.timeout(25_000);
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.evaluate(
`
for (var i = 0; i <= 1000; i++) {
const section = document.createElement('section');
section.textContent = i;
document.body.appendChild(section);
}
`
);
const sum = await page.$$eval('allArray/section', sections => {
return sections.reduce((acc, section) => {
return acc + Number(section.textContent);
}, 0);
});
expect(sum).toBe(500500);
});
});
});