/**
* 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.
*/
import expect from 'expect';
import {Puppeteer} from 'puppeteer';
import {ElementHandle} from 'puppeteer-core/internal/api/ElementHandle.js';
import sinon from 'sinon';
import {
getTestState,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
shortWaitForArrayToHaveAtLeastNElements,
} from './mocha-utils.js';
import utils from './utils.js';
describe('ElementHandle specs', function () {
setupTestBrowserHooks();
setupTestPageAndContextHooks();
describe('ElementHandle.boundingBox', function () {
it('should work', async () => {
const {page, server} = getTestState();
await page.setViewport({width: 500, height: 500});
await page.goto(server.PREFIX + '/grid.html');
const elementHandle = (await page.$('.box:nth-of-type(13)'))!;
const box = await elementHandle.boundingBox();
expect(box).toEqual({x: 100, y: 50, width: 50, height: 50});
});
it('should handle nested frames', async () => {
const {page, server, isChrome} = getTestState();
await page.setViewport({width: 500, height: 500});
await page.goto(server.PREFIX + '/frames/nested-frames.html');
const nestedFrame = page.frames()[1]!.childFrames()[1]!;
const elementHandle = (await nestedFrame.$('div'))!;
const box = await elementHandle.boundingBox();
if (isChrome) {
expect(box).toEqual({x: 28, y: 182, width: 264, height: 18});
} else {
expect(box).toEqual({x: 28, y: 182, width: 254, height: 18});
}
});
it('should return null for invisible elements', async () => {
const {page} = getTestState();
await page.setContent('
hi
');
const element = (await page.$('div'))!;
expect(await element.boundingBox()).toBe(null);
});
it('should force a layout', async () => {
const {page} = getTestState();
await page.setViewport({width: 500, height: 500});
await page.setContent(
'
'
);
let element = (await waitFor)!;
expect(element).toBeDefined();
const innerWaitFor = element.waitForSelector('.bar') as Promise<
ElementHandle
>;
await element.evaluate(el => {
el.innerHTML = '
bar1
';
});
element = (await innerWaitFor)!;
expect(element).toBeDefined();
expect(
await element.evaluate(el => {
return (el as HTMLElement).innerText;
})
).toStrictEqual('bar1');
});
});
describe('Element.waitForXPath', () => {
it('should wait correctly with waitForXPath on an element', async () => {
const {page} = getTestState();
// Set the page content after the waitFor has been started.
await page.setContent(
`
el1
el2
el3
`
);
const el1 = (await page.waitForSelector(
'#el1'
)) as ElementHandle;
for (const path of ['//div', './/div']) {
const e = (await el1.waitForXPath(
path
)) as ElementHandle;
expect(
await e.evaluate(el => {
return el.id;
})
).toStrictEqual('el2');
}
});
});
describe('ElementHandle.hover', function () {
it('should work', async () => {
const {page, server} = getTestState();
await page.goto(server.PREFIX + '/input/scrollable.html');
const button = (await page.$('#button-6'))!;
await button.hover();
expect(
await page.evaluate(() => {
return document.querySelector('button:hover')!.id;
})
).toBe('button-6');
});
});
describe('ElementHandle.isIntersectingViewport', function () {
it('should work', async () => {
const {page, server} = getTestState();
await page.goto(server.PREFIX + '/offscreenbuttons.html');
for (let i = 0; i < 11; ++i) {
const button = (await page.$('#btn' + i))!;
// All but last button are visible.
const visible = i < 10;
expect(await button.isIntersectingViewport()).toBe(visible);
}
});
it('should work with threshold', async () => {
const {page, server} = getTestState();
await page.goto(server.PREFIX + '/offscreenbuttons.html');
// a button almost cannot be seen
// sometimes we expect to return false by isIntersectingViewport1
const button = (await page.$('#btn11'))!;
expect(
await button.isIntersectingViewport({
threshold: 0.001,
})
).toBe(false);
});
it('should work with threshold of 1', async () => {
const {page, server} = getTestState();
await page.goto(server.PREFIX + '/offscreenbuttons.html');
// a button almost cannot be seen
// sometimes we expect to return false by isIntersectingViewport1
const button = (await page.$('#btn0'))!;
expect(
await button.isIntersectingViewport({
threshold: 1,
})
).toBe(true);
});
});
describe('Custom queries', function () {
afterEach(() => {
Puppeteer.clearCustomQueryHandlers();
});
it('should register and unregister', async () => {
const {page} = getTestState();
await page.setContent('');
// Register.
Puppeteer.registerCustomQueryHandler('getById', {
queryOne: (_element, selector) => {
return document.querySelector(`[id="${selector}"]`);
},
});
const element = (await page.$(
'getById/foo'
)) as ElementHandle;
expect(
await page.evaluate(element => {
return element.id;
}, element)
).toBe('foo');
const handlerNamesAfterRegistering = Puppeteer.customQueryHandlerNames();
expect(handlerNamesAfterRegistering.includes('getById')).toBeTruthy();
// Unregister.
Puppeteer.unregisterCustomQueryHandler('getById');
try {
await page.$('getById/foo');
throw new Error('Custom query handler name not set - throw expected');
} catch (error) {
expect(error).not.toStrictEqual(
new Error('Custom query handler name not set - throw expected')
);
}
const handlerNamesAfterUnregistering =
Puppeteer.customQueryHandlerNames();
expect(handlerNamesAfterUnregistering.includes('getById')).toBeFalsy();
});
it('should throw with invalid query names', () => {
try {
Puppeteer.registerCustomQueryHandler('1/2/3', {
queryOne: () => {
return document.querySelector('foo');
},
});
throw new Error(
'Custom query handler name was invalid - throw expected'
);
} catch (error) {
expect(error).toStrictEqual(
new Error('Custom query handler names may only contain [a-zA-Z]')
);
}
});
it('should work for multiple elements', async () => {
const {page} = getTestState();
await page.setContent(
'
'
);
Puppeteer.registerCustomQueryHandler('getByClass', {
queryAll: (_element, selector) => {
return [...document.querySelectorAll(`.${selector}`)];
},
});
const elements = await page.$$eval('getByClass/foo', divs => {
return divs.length;
});
expect(elements).toBe(2);
});
it('should wait correctly with waitForSelector', async () => {
const {page} = getTestState();
Puppeteer.registerCustomQueryHandler('getByClass', {
queryOne: (element, selector) => {
return (element as Element).querySelector(`.${selector}`);
},
});
const waitFor = page.waitForSelector('getByClass/foo');
// Set the page content after the waitFor has been started.
await page.setContent(
'
Foo1
'
);
const element = await waitFor;
expect(element).toBeDefined();
});
it('should wait correctly with waitForSelector on an element', async () => {
const {page} = getTestState();
Puppeteer.registerCustomQueryHandler('getByClass', {
queryOne: (element, selector) => {
return (element as Element).querySelector(`.${selector}`);
},
});
const waitFor = page.waitForSelector('getByClass/foo') as Promise<
ElementHandle
>;
// Set the page content after the waitFor has been started.
await page.setContent(
'
bar2
Foo1
'
);
let element = (await waitFor)!;
expect(element).toBeDefined();
const innerWaitFor = element.waitForSelector('getByClass/bar') as Promise<
ElementHandle
>;
await element.evaluate(el => {
el.innerHTML = '
bar1
';
});
element = (await innerWaitFor)!;
expect(element).toBeDefined();
expect(
await element.evaluate(el => {
return el.innerText;
})
).toStrictEqual('bar1');
});
it('should wait correctly with waitFor', async () => {
/* page.waitFor is deprecated so we silence the warning to avoid test noise */
sinon.stub(console, 'warn').callsFake(() => {});
const {page} = getTestState();
Puppeteer.registerCustomQueryHandler('getByClass', {
queryOne: (element, selector) => {
return (element as Element).querySelector(`.${selector}`);
},
});
const waitFor = page.waitForSelector('getByClass/foo');
// Set the page content after the waitFor has been started.
await page.setContent(
'
Foo1
'
);
const element = await waitFor;
expect(element).toBeDefined();
});
it('should work when both queryOne and queryAll are registered', async () => {
const {page} = getTestState();
await page.setContent(
'
Foo2
'
);
Puppeteer.registerCustomQueryHandler('getByClass', {
queryOne: (element, selector) => {
return (element as Element).querySelector(`.${selector}`);
},
queryAll: (element, selector) => {
return [...(element as Element).querySelectorAll(`.${selector}`)];
},
});
const element = (await page.$('getByClass/foo'))!;
expect(element).toBeDefined();
const elements = await page.$$('getByClass/foo');
expect(elements.length).toBe(3);
});
it('should eval when both queryOne and queryAll are registered', async () => {
const {page} = getTestState();
await page.setContent(
'