ya
`);
expect(await selectors._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
');
});
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');
});
});
describe('text selector', () => {
it('query', async ({page}) => {
await page.setContent(`
yo
ya
\nye
`);
expect(await page.$eval(`text=ya`, e => e.outerHTML)).toBe('
ya
');
expect(await page.$eval(`text="ya"`, e => e.outerHTML)).toBe('
ya
');
expect(await page.$eval(`text=/^[ay]+$/`, e => e.outerHTML)).toBe('
ya
');
expect(await page.$eval(`text=/Ya/i`, e => e.outerHTML)).toBe('
ya
');
expect(await page.$eval(`text=ye`, e => e.outerHTML)).toBe('
\nye
');
await page.setContent(`
ye
ye
`);
expect(await page.$eval(`text="ye"`, e => e.outerHTML)).toBe('
ye
');
await page.setContent(`
yo
"ya
hello world!
`);
expect(await page.$eval(`text="\\"ya"`, e => e.outerHTML)).toBe('
"ya
');
expect(await page.$eval(`text=/hello/`, e => e.outerHTML)).toBe('
hello world!
');
expect(await page.$eval(`text=/^\\s*heLLo/i`, e => e.outerHTML)).toBe('
hello world!
');
await page.setContent(`
`);
expect(await page.$eval(`text=hey`, e => e.outerHTML)).toBe('
');
await page.setContent(`
yo
yo
`);
expect(await page.$$eval(`text=yo`, es => es.map(e => e.outerHTML).join('\n'))).toBe('
yo
\n
yo
');
});
it('create', async ({page}) => {
await page.setContent(`
yo
"ya
ye ye
`);
expect(await selectors._createSelector('text', await page.$('div'))).toBe('yo');
expect(await selectors._createSelector('text', await page.$('div:nth-child(2)'))).toBe('"\\"ya"');
expect(await selectors._createSelector('text', await page.$('div:nth-child(3)'))).toBe('"ye ye"');
await page.setContent(`
yo
`);
expect(await selectors._createSelector('text', await page.$('div:nth-child(2)'))).toBe('hey');
await page.setContent(`
`);
expect(await selectors._createSelector('text', await page.$('div'))).toBe('yo');
await page.setContent(`
`);
expect(await selectors._createSelector('text', await page.$('div'))).toBe('" \\"yo "');
});
});
describe('selectors.register', () => {
it('should work', async ({page}) => {
const createTagSelector = () => ({
name: 'tag',
create(root, target) {
return target.nodeName;
},
query(root, selector) {
return root.querySelector(selector);
},
queryAll(root, selector) {
return Array.from(root.querySelectorAll(selector));
}
});
await selectors.register(`(${createTagSelector.toString()})()`);
await page.setContent('
');
expect(await selectors._createSelector('tag', await page.$('div'))).toBe('DIV');
expect(await page.$eval('tag=DIV', e => e.nodeName)).toBe('DIV');
expect(await page.$eval('tag=SPAN', e => e.nodeName)).toBe('SPAN');
expect(await page.$$eval('tag=DIV', es => es.length)).toBe(2);
});
it('should update', async ({page}) => {
await page.setContent('
');
expect(await page.$eval('div', e => e.nodeName)).toBe('DIV');
const error = await page.$('dummy=foo').catch(e => e);
expect(error.message).toContain('Unknown engine dummy while parsing selector dummy=foo');
const createDummySelector = (name) => ({
name,
create(root, target) {
return target.nodeName;
},
query(root, selector) {
return root.querySelector(name);
},
queryAll(root, selector) {
return Array.from(root.querySelectorAll(name));
}
});
await selectors.register(createDummySelector, 'dummy');
expect(await page.$eval('dummy=foo', e => e.id)).toBe('d1');
expect(await page.$eval('css=span >> dummy=foo', e => e.id)).toBe('d2');
});
});
};