/**
* 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.
*/
import { test as it, expect } from './pageTest';
it('should work for open shadow roots', async ({ page, server }) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
expect(await page.$eval(`id=target`, e => e.textContent)).toBe('Hello from root2');
expect(await page.$eval(`data-testid=foo`, e => e.textContent)).toBe('Hello from root1');
expect(await page.$$eval(`data-testid=foo`, els => els.length)).toBe(3);
expect(await page.$(`id:light=target`)).toBe(null);
expect(await page.$(`data-testid:light=foo`)).toBe(null);
expect(await page.$$(`data-testid:light=foo`)).toEqual([]);
});
it('should click on links in shadow dom', async ({ page, server, browserName, browserMajorVersion }) => {
await page.goto(server.PREFIX + '/shadow-dom-link.html');
expect(await page.evaluate(() => (window as any).clickCount)).toBe(0);
await page.click('#inner-link');
expect(await page.evaluate(() => (window as any).clickCount)).toBe(1);
});
it('should work with :visible', async ({ page }) => {
await page.setContent(`
`);
expect(await page.$('div:visible')).toBe(null);
const error = await page.waitForSelector(`div:visible`, { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('1000ms');
const promise = page.waitForSelector(`div:visible`, { state: 'attached' });
await page.$eval('#target2', div => div.textContent = 'Now visible');
const element = await promise;
expect(await element.evaluate(e => e.id)).toBe('target2');
expect(await page.$eval('div:visible', div => div.id)).toBe('target2');
});
it('should work with >> visible=', async ({ page }) => {
await page.setContent(`
`);
expect(await page.$('div >> visible=true')).toBe(null);
const error = await page.waitForSelector(`div >> visible=true`, { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('1000ms');
const promise = page.waitForSelector(`div >> visible=true`, { state: 'attached' });
await page.$eval('#target2', div => div.textContent = 'Now visible');
const element = await promise;
expect(await element.evaluate(e => e.id)).toBe('target2');
expect(await page.$eval('div >> visible=true', div => div.id)).toBe('target2');
});
it('should work with :nth-match', async ({ page }) => {
await page.setContent(`
`);
expect(await page.$(':nth-match(div, 3)')).toBe(null);
expect(await page.$eval(':nth-match(div, 1)', e => e.id)).toBe('target1');
expect(await page.$eval(':nth-match(div, 2)', e => e.id)).toBe('target2');
expect(await page.$eval(':nth-match(section > div, 2)', e => e.id)).toBe('target2');
expect(await page.$eval(':nth-match(section, div, 2)', e => e.id)).toBe('target1');
expect(await page.$eval(':nth-match(div, section, 3)', e => e.id)).toBe('target2');
expect(await page.$$eval(':is(:nth-match(div, 1), :nth-match(div, 2))', els => els.length)).toBe(2);
let error;
error = await page.$(':nth-match(div, bar, 0)').catch(e => e);
expect(error.message).toContain(`"nth-match" engine expects a one-based index as the last argument`);
error = await page.$(':nth-match(2)').catch(e => e);
expect(error.message).toContain(`"nth-match" engine expects non-empty selector list and an index argument`);
error = await page.$(':nth-match(div, bar, foo)').catch(e => e);
expect(error.message).toContain(`"nth-match" engine expects a one-based index as the last argument`);
const promise = page.waitForSelector(`:nth-match(div, 3)`, { state: 'attached' });
await page.$eval('section', section => {
const div = document.createElement('div');
div.setAttribute('id', 'target3');
section.appendChild(div);
});
const element = await promise;
expect(await element.evaluate(e => e.id)).toBe('target3');
});
it('should work with nth=', async ({ page }) => {
await page.setContent(`
`);
expect(await page.$('div >> nth=2')).toBe(null);
expect(await page.$eval('div >> nth=0', e => e.id)).toBe('target1');
expect(await page.$eval('div >> nth=1', e => e.id)).toBe('target2');
expect(await page.$eval('section > div >> nth=1', e => e.id)).toBe('target2');
expect(await page.$eval('section, div >> nth=1', e => e.id)).toBe('target1');
expect(await page.$eval('div, section >> nth=2', e => e.id)).toBe('target2');
const promise = page.waitForSelector(`div >> nth=2`, { state: 'attached' });
await page.$eval('section', section => {
const div = document.createElement('div');
div.setAttribute('id', 'target3');
section.appendChild(div);
});
const element = await promise;
expect(await element.evaluate(e => e.id)).toBe('target3');
await page.setContent(`
3
`);
expect(await page.$$eval(`//*[@id="t1"]|//*[@id="t3"]`, els => els.length)).toBe(2);
const e1 = await page.waitForSelector(`//*[@id="t1"]|//*[@id="t3"]`);
expect(e1).toBeTruthy();
expect(await e1.evaluate(e => e.id)).toBe('t1');
const e2 = await page.waitForSelector(`//*[@id="unknown"]|//*[@id="t2"]`);
expect(e2).toBeTruthy();
expect(await e2.evaluate(e => e.id)).toBe('t2');
await page.click(`//code|//span[@id="t2"]`);
});
it('data-testid on the handle should be relative', async ({ page }) => {
await page.setContent(`
1
2
`);
expect(await page.$eval(`data-testid=find-me`, e => e.id)).toBe('target1');
const div = await page.$('div');
expect(await div.$eval(`data-testid=find-me`, e => e.id)).toBe('target2');
expect(await page.$eval(`div >> data-testid=find-me`, e => e.id)).toBe('target2');
});
it('should properly determine visibility of display:contents elements', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/11202' });
await page.setContent(`
`);
await page.waitForSelector('"DISPLAY CONTENTS"');
await page.setContent(`
`);
await page.waitForSelector('article');
await page.setContent(`
`);
await page.waitForSelector('article');
await page.setContent(`
`);
await page.waitForSelector('article');
await page.setContent(`
`);
await page.waitForSelector('article', { state: 'hidden' });
});
it('should work with internal:has=', async ({ page, server }) => {
await page.goto(server.PREFIX + '/deep-shadow.html');
expect(await page.$$eval(`div >> internal:has="#target"`, els => els.length)).toBe(2);
expect(await page.$$eval(`div >> internal:has="[data-testid=foo]"`, els => els.length)).toBe(3);
expect(await page.$$eval(`div >> internal:has="[attr*=value]"`, els => els.length)).toBe(2);
await page.setContent(`
`);
expect(await page.$$eval(`section >> internal:has="span, div"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has="span, div"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has="br"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has="span, br"`, els => els.length)).toBe(2);
expect(await page.$$eval(`section >> internal:has="span, br, div"`, els => els.length)).toBe(2);
await page.setContent(`
hello
world
`);
expect(await page.$$eval(`div >> internal:has="text=world"`, els => els.length)).toBe(1);
expect(await page.$eval(`div >> internal:has="text=world"`, e => e.outerHTML)).toBe(`
world
`);
expect(await page.$$eval(`div >> internal:has="text=\\"hello\\""`, els => els.length)).toBe(1);
expect(await page.$eval(`div >> internal:has="text=\\"hello\\""`, e => e.outerHTML)).toBe(`
hello
`);
expect(await page.$$eval(`div >> internal:has="xpath=./span"`, els => els.length)).toBe(2);
expect(await page.$$eval(`div >> internal:has="span"`, els => els.length)).toBe(2);
expect(await page.$$eval(`div >> internal:has="span >> text=wor"`, els => els.length)).toBe(1);
expect(await page.$eval(`div >> internal:has="span >> text=wor"`, e => e.outerHTML)).toBe(`
world
`);
expect(await page.$eval(`div >> internal:has="span >> text=wor" >> span`, e => e.outerHTML)).toBe(`
world`);
const error1 = await page.$(`div >> internal:has=abc`).catch(e => e);
expect(error1.message).toContain('Malformed selector: internal:has=abc');
const error2 = await page.$(`internal:has="div"`).catch(e => e);
expect(error2.message).toContain('"internal:has" selector cannot be first');
const error3 = await page.$(`div >> internal:has=33`).catch(e => e);
expect(error3.message).toContain('Malformed selector: internal:has=33');
const error4 = await page.$(`div >> internal:has="span!"`).catch(e => e);
expect(error4.message).toContain('Unexpected token "!" while parsing selector "span!"');
});
it('should work with internal:has-not=', async ({ page }) => {
await page.setContent(`
`);
expect(await page.$$eval(`section >> internal:has-not="span"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has-not="span, div, br"`, els => els.length)).toBe(0);
expect(await page.$$eval(`section >> internal:has-not="br"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has-not="span, div"`, els => els.length)).toBe(1);
expect(await page.$$eval(`section >> internal:has-not="article"`, els => els.length)).toBe(2);
});
it('should work with internal:and=', async ({ page, server }) => {
await page.setContent(`
hello
world
hello2world2
`);
expect(await page.$$eval(`div >> internal:and="span"`, els => els.map(e => e.textContent))).toEqual([]);
expect(await page.$$eval(`div >> internal:and=".foo"`, els => els.map(e => e.textContent))).toEqual(['hello']);
expect(await page.$$eval(`div >> internal:and=".bar"`, els => els.map(e => e.textContent))).toEqual(['world']);
expect(await page.$$eval(`span >> internal:and="span"`, els => els.map(e => e.textContent))).toEqual(['hello2', 'world2']);
expect(await page.$$eval(`.foo >> internal:and="div"`, els => els.map(e => e.textContent))).toEqual(['hello']);
expect(await page.$$eval(`.bar >> internal:and="span"`, els => els.map(e => e.textContent))).toEqual(['world2']);
});
it('should work with internal:or=', async ({ page, server }) => {
await page.setContent(`
hello
world
`);
expect(await page.$$eval(`div >> internal:or="span"`, els => els.map(e => e.textContent))).toEqual(['hello', 'world']);
expect(await page.$$eval(`span >> internal:or="div"`, els => els.map(e => e.textContent))).toEqual(['hello', 'world']);
expect(await page.$$eval(`article >> internal:or="something"`, els => els.length)).toBe(0);
expect(await page.locator(`article >> internal:or="div"`).textContent()).toBe('hello');
expect(await page.locator(`article >> internal:or="span"`).textContent()).toBe('world');
expect(await page.locator(`div >> internal:or="article"`).textContent()).toBe('hello');
expect(await page.locator(`span >> internal:or="article"`).textContent()).toBe('world');
});
it('chaining should work with large DOM @smoke', async ({ page, server }) => {
await page.evaluate(() => {
let last = document.body;
for (let i = 0; i < 100; i++) {
const e = document.createElement('div');
last.appendChild(e);
last = e;
}
const target = document.createElement('span');
target.textContent = 'Found me!';
last.appendChild(target);
});
// Naive implementation generates C(100, 9) ~= 1.9*10^12 entries.
const selectors = [
'div >> div >> div >> div >> div >> div >> div >> div >> span',
'div div div div div div div div span',
'div div >> div div >> div div >> div div >> span',
];
const counts = [];
const times = [];
for (const selector of selectors) {
const time = Date.now();
counts.push(await page.$$eval(selector, els => els.length));
times.push({ selector, time: Date.now() - time });
}
expect(counts).toEqual([1, 1, 1]);
// Uncomment to see performance results.
// console.log(times);
});