2018-11-21 02:45:37 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2020-04-09 05:56:25 +00:00
|
|
|
const expect = require('expect');
|
2020-05-07 10:54:55 +00:00
|
|
|
const {
|
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
|
|
|
} = require('./mocha-utils');
|
2018-11-21 02:45:37 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('querySelector', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Page.$eval', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2018-11-21 02:45:37 +00:00
|
|
|
|
|
|
|
await page.setContent('<section id="testAttribute">43543</section>');
|
2020-05-07 10:54:55 +00:00
|
|
|
const idAttribute = await page.$eval('section', (e) => e.id);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(idAttribute).toBe('testAttribute');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept arguments', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<section>hello</section>');
|
2020-05-07 10:54:55 +00:00
|
|
|
const text = await page.$eval(
|
|
|
|
'section',
|
|
|
|
(e, suffix) => e.textContent + suffix,
|
|
|
|
' world!'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(text).toBe('hello world!');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept ElementHandles as arguments', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<section>hello</section><div> world</div>');
|
|
|
|
const divHandle = await page.$('div');
|
2020-05-07 10:54:55 +00:00
|
|
|
const text = await page.$eval(
|
|
|
|
'section',
|
|
|
|
(e, div) => e.textContent + div.textContent,
|
|
|
|
divHandle
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(text).toBe('hello world');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw error if no element is found', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.$eval('section', (e) => e.id)
|
|
|
|
.catch((error_) => (error = error_));
|
|
|
|
expect(error.message).toContain(
|
|
|
|
'failed to find element matching selector "section"'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Page.$$eval', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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) => divs.length);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(divsCount).toBe(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Page.$', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should query existing element', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<section>test</section>');
|
|
|
|
const element = await page.$('section');
|
|
|
|
expect(element).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return null for non-existing element', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
const element = await page.$('non-existing-element');
|
|
|
|
expect(element).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.$$', function () {
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should query existing elements', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<div>A</div><br/><div>B</div>');
|
|
|
|
const elements = await page.$$('div');
|
|
|
|
expect(elements.length).toBe(2);
|
2020-05-07 10:54:55 +00:00
|
|
|
const promises = elements.map((element) =>
|
|
|
|
page.evaluate((e) => e.textContent, element)
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const elements = await page.$$('div');
|
|
|
|
expect(elements.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Path.$x', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should query existing element', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<section>test</section>');
|
|
|
|
const elements = await page.$x('/html/body/section');
|
|
|
|
expect(elements[0]).toBeTruthy();
|
|
|
|
expect(elements.length).toBe(1);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return empty array for non-existing element', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent('<div></div><div></div>');
|
|
|
|
const elements = await page.$x('/html/body/div');
|
|
|
|
expect(elements.length).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.$', function () {
|
|
|
|
it('should query existing element', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
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>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$('.second');
|
|
|
|
const inner = await second.$('.inner');
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await page.evaluate((e) => e.textContent, inner);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(content).toBe('A');
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return null for non-existing element', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<html><body><div class="second"><div class="inner">B</div></div></body></html>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$('.third');
|
|
|
|
expect(second).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('ElementHandle.$eval', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const tweet = await page.$('.tweet');
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await tweet.$eval('.like', (node) => node.innerText);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(content).toBe('100');
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should retrieve content from subtree', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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>';
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent(htmlContent);
|
|
|
|
const elementHandle = await page.$('#myId');
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await elementHandle.$eval('.a', (node) => node.innerText);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(content).toBe('a-child-div');
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw in case of missing selector', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const htmlContent =
|
|
|
|
'<div class="a">not-a-child-div</div><div id="myId"></div>';
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent(htmlContent);
|
|
|
|
const elementHandle = await page.$('#myId');
|
2020-05-07 10:54:55 +00:00
|
|
|
const errorMessage = await elementHandle
|
|
|
|
.$eval('.a', (node) => node.innerText)
|
|
|
|
.catch((error) => error.message);
|
|
|
|
expect(errorMessage).toBe(
|
|
|
|
`Error: failed to find element matching selector ".a"`
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
});
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('ElementHandle.$$eval', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const tweet = await page.$('.tweet');
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await tweet.$$eval('.like', (nodes) =>
|
|
|
|
nodes.map((n) => n.innerText)
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(content).toEqual(['100', '10']);
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should retrieve content from subtree', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
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>';
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent(htmlContent);
|
|
|
|
const elementHandle = await page.$('#myId');
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await elementHandle.$$eval('.a', (nodes) =>
|
|
|
|
nodes.map((n) => n.innerText)
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const htmlContent =
|
|
|
|
'<div class="a">not-a-child-div</div><div id="myId"></div>';
|
2018-11-21 02:45:37 +00:00
|
|
|
await page.setContent(htmlContent);
|
|
|
|
const elementHandle = await page.$('#myId');
|
2020-05-07 10:54:55 +00:00
|
|
|
const nodesLength = await elementHandle.$$eval(
|
|
|
|
'.a',
|
|
|
|
(nodes) => nodes.length
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(nodesLength).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('ElementHandle.$$', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should query existing elements', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<html><body><div>A</div><br/><div>B</div></body></html>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const elements = await html.$$('div');
|
|
|
|
expect(elements.length).toBe(2);
|
2020-05-07 10:54:55 +00:00
|
|
|
const promises = elements.map((element) =>
|
|
|
|
page.evaluate((e) => e.textContent, element)
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<html><body><span>A</span><br/><span>B</span></body></html>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const elements = await html.$$('div');
|
|
|
|
expect(elements.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.$x', function () {
|
|
|
|
it('should query existing element', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 02:45:37 +00:00
|
|
|
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>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$x(`./body/div[contains(@class, 'second')]`);
|
|
|
|
const inner = await second[0].$x(`./div[contains(@class, 'inner')]`);
|
2020-05-07 10:54:55 +00:00
|
|
|
const content = await page.evaluate((e) => e.textContent, inner[0]);
|
2018-11-21 02:45:37 +00:00
|
|
|
expect(content).toBe('A');
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return null for non-existing element', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<html><body><div class="second"><div class="inner">B</div></div></body></html>'
|
|
|
|
);
|
2018-11-21 02:45:37 +00:00
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$x(`/div[contains(@class, 'third')]`);
|
|
|
|
expect(second).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|