/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* 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.
*/
module.exports.addTests = function({testRunner, expect, product, FFOX, CHROME, WEBKIT}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Page.$eval', function() {
it('should work with css selector', async({page, server}) => {
await page.setContent('');
const idAttribute = await page.$eval('css=section', e => e.id);
expect(idAttribute).toBe('testAttribute');
});
it('should work with zs selector', async({page, server}) => {
await page.setContent('');
const idAttribute = await page.$eval('zs="43543"', e => e.id);
expect(idAttribute).toBe('testAttribute');
});
it('should work with xpath selector', async({page, server}) => {
await page.setContent('');
const idAttribute = await page.$eval('xpath=/html/body/section', e => e.id);
expect(idAttribute).toBe('testAttribute');
});
it('should auto-detect css selector', async({page, server}) => {
await page.setContent('');
const idAttribute = await page.$eval('section', e => e.id);
expect(idAttribute).toBe('testAttribute');
});
it('should respect visibility', async({page, server}) => {
let error = null;
await page.setContent('');
await page.$eval({selector: 'css=section', visible: true}, e => e.id).catch(e => error = e);
expect(error.message).toContain('failed to find element matching selector "[visible] css=section"');
expect(await page.$eval({selector: 'css=section', visible: false}, e => e.id)).toBe('testAttribute');
error = null;
await page.setContent('');
await page.$eval({selector: 'css=section', visible: false}, e => e.id).catch(e => error = e);
expect(error.message).toContain('failed to find element matching selector "[hidden] css=section"');
expect(await page.$eval({selector: 'css=section', visible: true}, e => e.id)).toBe('testAttribute');
});
it('should accept arguments', async({page, server}) => {
await page.setContent('');
const text = await page.$eval('section', (e, suffix) => e.textContent + suffix, ' world!');
expect(text).toBe('hello world!');
});
it('should accept ElementHandles as arguments', async({page, server}) => {
await page.setContent('
world
');
const divHandle = await page.$('div');
const text = await page.$eval('section', (e, div) => e.textContent + div.textContent, divHandle);
expect(text).toBe('hello world');
});
it('should throw error if no element is found', async({page, server}) => {
let error = null;
await page.$eval('section', e => e.id).catch(e => error = e);
expect(error.message).toContain('failed to find element matching selector "section"');
});
it('should support >> syntax', async({page, server}) => {
await page.setContent('');
const text = await page.$eval('css=section >> css=div', (e, suffix) => e.textContent + suffix, ' world!');
expect(text).toBe('hello world!');
});
it('should support >> syntax with different engines', async({page, server}) => {
await page.setContent('');
const text = await page.$eval('xpath=/html/body/section >> css=div >> zs="hello"', (e, suffix) => e.textContent + suffix, ' world!');
expect(text).toBe('hello world!');
});
it('should support spaces with >> syntax', async({page, server}) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
const text = await page.$eval(' css = div >>css=div>>css = span ', e => e.textContent);
expect(text).toBe('Hello from root2');
});
it('should enter shadow roots with >> syntax', async({page, server}) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
const text1 = await page.$eval('css=div >> css=span', e => e.textContent);
expect(text1).toBe('Hello from root1');
const text2 = await page.$eval('css=div >> css=*:nth-child(2) >> css=span', e => e.textContent);
expect(text2).toBe('Hello from root2');
const nonExisting = await page.$('css=div div >> css=span');
expect(nonExisting).not.toBeTruthy();
const text3 = await page.$eval('css=section div >> css=span', e => e.textContent);
expect(text3).toBe('Hello from root1');
const text4 = await page.$eval('xpath=/html/body/section/div >> css=div >> css=span', e => e.textContent);
expect(text4).toBe('Hello from root2');
const text5 = await page.$eval('zs=section div >> css=div >> css=span', e => e.textContent);
expect(text5).toBe('Hello from root2');
});
});
describe('Page.$$eval', function() {
it('should work with css selector', async({page, server}) => {
await page.setContent('hello
beautiful
world!
');
const divsCount = await page.$$eval('css=div', divs => divs.length);
expect(divsCount).toBe(3);
});
it('should work with zs selector', async({page, server}) => {
await page.setContent('hello
beautiful
world!
');
const divsCount = await page.$$eval('zs=div', divs => divs.length);
expect(divsCount).toBe(3);
});
it('should work with xpath selector', async({page, server}) => {
await page.setContent('hello
beautiful
world!
');
const divsCount = await page.$$eval('xpath=/html/body/div', divs => divs.length);
expect(divsCount).toBe(3);
});
it('should auto-detect css selector', async({page, server}) => {
await page.setContent('hello
beautiful
world!
');
const divsCount = await page.$$eval('div', divs => divs.length);
expect(divsCount).toBe(3);
});
it('should support >> syntax', async({page, server}) => {
await page.setContent('hello
beautiful
world!
Not this one');
const spansCount = await page.$$eval('css=div >> css=span', spans => spans.length);
expect(spansCount).toBe(3);
});
it('should enter shadow roots with >> syntax', async({page, server}) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
const spansCount = await page.$$eval('css=div >> css=div >> css=span', spans => spans.length);
expect(spansCount).toBe(2);
});
it('should respect visibility', async({page, server}) => {
await page.setContent('');
expect(await page.$$eval({selector: 'css=section', visible: true}, x => x.length)).toBe(1);
expect(await page.$$eval({selector: 'css=section', visible: false}, x => x.length)).toBe(2);
expect(await page.$$eval({selector: 'css=section'}, x => x.length)).toBe(3);
});
});
describe('Page.$', function() {
it('should query existing element with css selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('css=section');
expect(element).toBeTruthy();
});
it('should query existing element with zs selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('zs="test"');
expect(element).toBeTruthy();
});
it('should query existing element with xpath selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('xpath=/html/body/section');
expect(element).toBeTruthy();
});
it('should return null for non-existing element', async({page, server}) => {
const element = await page.$('non-existing-element');
expect(element).toBe(null);
});
it('should auto-detect xpath selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('//html/body/section');
expect(element).toBeTruthy();
});
it('should auto-detect zs selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('"test"');
expect(element).toBeTruthy();
});
it('should auto-detect css selector', async({page, server}) => {
await page.setContent('');
const element = await page.$('section');
expect(element).toBeTruthy();
});
it('should support >> syntax', async({page, server}) => {
await page.setContent('');
const element = await page.$('css=section >> css=div');
expect(element).toBeTruthy();
});
it('should respect visibility', async({page, server}) => {
await page.setContent('');
expect(await page.$({selector: 'css=section', visible: true})).toBeTruthy();
expect(await page.$({selector: 'css=section', visible: false})).not.toBeTruthy();
expect(await page.$({selector: 'css=section'})).toBeTruthy();
await page.setContent('');
expect(await page.$({selector: 'css=section', visible: true})).not.toBeTruthy();
expect(await page.$({selector: 'css=section', visible: false})).toBeTruthy();
expect(await page.$({selector: 'css=section'})).toBeTruthy();
});
});
describe('Page.$$', function() {
it('should query existing elements', async({page, server}) => {
await page.setContent('A
B
');
const elements = await page.$$('div');
expect(elements.length).toBe(2);
const promises = elements.map(element => page.evaluate(e => e.textContent, element));
expect(await Promise.all(promises)).toEqual(['A', 'B']);
});
it('should return empty array if nothing is found', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
const elements = await page.$$('div');
expect(elements.length).toBe(0);
});
it('should respect visibility', async({page, server}) => {
await page.setContent('');
expect((await page.$$({selector: 'css=section', visible: true})).length).toBe(1);
expect((await page.$$({selector: 'css=section', visible: false})).length).toBe(2);
expect((await page.$$({selector: 'css=section'})).length).toBe(3);
});
});
describe('Path.$x', function() {
it('should query existing element', async({page, server}) => {
await page.setContent('');
const elements = await page.$x('/html/body/section');
expect(elements[0]).toBeTruthy();
expect(elements.length).toBe(1);
});
it('should return empty array for non-existing element', async({page, server}) => {
const element = await page.$x('/html/body/non-existing-element');
expect(element).toEqual([]);
});
it('should return multiple elements', async({page, sever}) => {
await page.setContent('');
const elements = await page.$x('/html/body/div');
expect(elements.length).toBe(2);
});
});
describe('ElementHandle.$', function() {
it('should query existing element', async({page, server}) => {
await page.goto(server.PREFIX + '/playground.html');
await page.setContent('');
const html = await page.$('html');
const second = await html.$('.second');
const inner = await second.$('.inner');
const content = await page.evaluate(e => e.textContent, inner);
expect(content).toBe('A');
});
it('should query existing element with zs selector', async({page, server}) => {
await page.goto(server.PREFIX + '/playground.html');
await page.setContent('');
const html = await page.$('zs=html');
const second = await html.$('zs=.second');
const inner = await second.$('zs=.inner');
const content = await page.evaluate(e => e.textContent, inner);
expect(content).toBe('A');
});
it('should return null for non-existing element', async({page, server}) => {
await page.setContent('');
const html = await page.$('html');
const second = await html.$('.third');
expect(second).toBe(null);
});
it('should respect visibility', async({page, server}) => {
await page.goto(server.PREFIX + '/playground.html');
await page.setContent('');
const second = await page.$('html .second');
let inner = await second.$({selector: '.inner', visible: true});
expect(inner).not.toBeTruthy();
inner = await second.$({selector: '.inner', visible: false});
expect(await inner.evaluate(e => e.textContent)).toBe('A');
await inner.evaluate(e => e.style.display = 'block');
inner = await second.$({selector: '.inner', visible: true});
expect(await inner.evaluate(e => e.textContent)).toBe('A');
});
});
describe('ElementHandle.$eval', function() {
it('should work', async({page, server}) => {
await page.setContent('');
const tweet = await page.$('.tweet');
const content = await tweet.$eval('.like', node => node.innerText);
expect(content).toBe('100');
});
it('should retrieve content from subtree', async({page, server}) => {
const htmlContent = 'not-a-child-div
';
await page.setContent(htmlContent);
const elementHandle = await page.$('#myId');
const content = await elementHandle.$eval('.a', node => node.innerText);
expect(content).toBe('a-child-div');
});
it('should throw in case of missing selector', async({page, server}) => {
const htmlContent = 'not-a-child-div
';
await page.setContent(htmlContent);
const elementHandle = await page.$('#myId');
const errorMessage = await elementHandle.$eval('.a', node => node.innerText).catch(error => error.message);
expect(errorMessage).toBe(`Error: failed to find element matching selector ".a"`);
});
});
describe('ElementHandle.$$eval', function() {
it('should work', async({page, server}) => {
await page.setContent('');
const tweet = await page.$('.tweet');
const content = await tweet.$$eval('.like', nodes => nodes.map(n => n.innerText));
expect(content).toEqual(['100', '10']);
});
it('should retrieve content from subtree', async({page, server}) => {
const htmlContent = 'not-a-child-div
';
await page.setContent(htmlContent);
const elementHandle = await page.$('#myId');
const content = await elementHandle.$$eval('.a', nodes => nodes.map(n => n.innerText));
expect(content).toEqual(['a1-child-div', 'a2-child-div']);
});
it('should not throw in case of missing selector', async({page, server}) => {
const htmlContent = 'not-a-child-div
';
await page.setContent(htmlContent);
const elementHandle = await page.$('#myId');
const nodesLength = await elementHandle.$$eval('.a', nodes => nodes.length);
expect(nodesLength).toBe(0);
});
});
describe('ElementHandle.$$', function() {
it('should query existing elements', async({page, server}) => {
await page.setContent('A
B
');
const html = await page.$('html');
const elements = await html.$$('div');
expect(elements.length).toBe(2);
const promises = elements.map(element => page.evaluate(e => e.textContent, element));
expect(await Promise.all(promises)).toEqual(['A', 'B']);
});
it('should return empty array for non-existing elements', async({page, server}) => {
await page.setContent('A
B');
const html = await page.$('html');
const elements = await html.$$('div');
expect(elements.length).toBe(0);
});
});
describe('ElementHandle.$x', function() {
it('should query existing element', async({page, server}) => {
await page.goto(server.PREFIX + '/playground.html');
await page.setContent('');
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')]`);
const content = await page.evaluate(e => e.textContent, inner[0]);
expect(content).toBe('A');
});
it('should return null for non-existing element', async({page, server}) => {
await page.setContent('');
const html = await page.$('html');
const second = await html.$x(`/div[contains(@class, 'third')]`);
expect(second).toEqual([]);
});
});
describe('zselector', () => {
it('query', async ({page}) => {
await page.setContent(`yo
ya
ye
`);
expect(await page.$eval(`zs="ya"`, e => e.outerHTML)).toBe('ya
');
await page.setContent(``);
expect(await page.$eval(`zs=[foo="bar space"]`, e => e.outerHTML)).toBe('');
await page.setContent(`yo
`);
expect(await page.$eval(`zs=span`, e => e.outerHTML)).toBe('');
expect(await page.$eval(`zs=div > span`, e => e.outerHTML)).toBe('');
expect(await page.$eval(`zs=div span`, e => e.outerHTML)).toBe('');
expect(await page.$eval(`zs="yo" > span`, e => e.outerHTML)).toBe('');
expect(await page.$eval(`zs="yo" span`, e => e.outerHTML)).toBe('');
expect(await page.$eval(`zs=span ^`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ div`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ "yo"`, e => e.outerHTML)).toBe('yo
');
await page.setContent(`yo
yo
`);
expect(await page.$eval(`zs="yo"#0`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs="yo"#1`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs="yo" ~ DIV#1`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ div#1`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ div#0`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ "yo"#1 ^ > div`, e => e.outerHTML)).toBe('yo
');
expect(await page.$eval(`zs=span ~ "yo"#1 ^ > div#1`, e => e.outerHTML)).toBe('yo
');
await page.setContent(`yo
yo
`);
expect(await page.$eval(`zs="yo"`, e => e.outerHTML)).toBe('yo
');
expect(await page.$$eval(`zs="yo"`, es => es.map(e => e.outerHTML).join('\n'))).toBe('yo
\nyo
');
expect(await page.$$eval(`zs="yo"#1`, es => es.map(e => e.outerHTML).join('\n'))).toBe('yo
');
expect(await page.$$eval(`zs="yo" ~ span`, es => es.map(e => e.outerHTML).join('\n'))).toBe('\n\n');
expect(await page.$$eval(`zs="yo"#1 ~ span`, es => es.map(e => e.outerHTML).join('\n'))).toBe('\n');
expect(await page.$$eval(`zs="yo" ~ span#0`, es => es.map(e => e.outerHTML).join('\n'))).toBe('\n');
expect(await page.$$eval(`zs="yo" ~ span#1`, es => es.map(e => e.outerHTML).join('\n'))).toBe('\n');
});
it('create', async ({page}) => {
await page.setContent(`yo
ya
ya
`);
expect(await page._createSelector('zs', await page.$('div'))).toBe('"yo"');
expect(await page._createSelector('zs', await page.$('div:nth-child(2)'))).toBe('"ya"');
expect(await page._createSelector('zs', await page.$('div:nth-child(3)'))).toBe('"ya"#1');
await page.setContent(``);
expect(await page._createSelector('zs', await page.$('img'))).toBe('img[alt="foo bar"]');
await page.setContent(`yo
`);
expect(await page._createSelector('zs', await page.$('span'))).toBe('"yo"~SPAN');
expect(await page._createSelector('zs', await page.$('span:nth-child(2)'))).toBe('SPAN#1');
});
it('children of various display parents', async ({page}) => {
await page.setContent(`yo
`);
expect(await page._createSelector('zs', await page.$('span'))).toBe('"yo"');
await page.setContent(`yo
`);
expect(await page._createSelector('zs', await page.$('span'))).toBe('"yo"');
// "display: none" makes all children text invisible - fallback to tag name.
await page.setContent(`yo
`);
expect(await page._createSelector('zs', await page.$('span'))).toBe('SPAN');
});
it('boundary', async ({page}) => {
await page.setContent(`
hey
hey
hey
yo
hello
hello
hello
unique
ya
ya
`);
expect(await page._createSelector('zs', await page.$('#target'))).toBe('"ya"~"hey"~"hello"');
expect(await page.$eval(`zs="ya"~"hey"~"hello"`, e => e.outerHTML)).toBe('
hello
');
expect(await page.$eval(`zs="ya"~"hey"~"unique"`, e => e.outerHTML).catch(e => e.message)).toBe('Error: failed to find element matching selector "zs="ya"~"hey"~"unique""');
expect(await page.$$eval(`zs="ya" ~ "hey" ~ "hello"`, es => es.map(e => e.outerHTML).join('\n'))).toBe('
hello
\n
hello
');
});
});
};