');
const element = await page.$('text/a b');
expect(
await element?.evaluate(e => {
return e.textContent;
})
).toBe('a b');
});
});
describe('in ElementHandles', function () {
it('should query existing element', async () => {
const {page} = getTestState();
await page.setContent('
a
');
const elementHandle = (await page.$('div'))!;
expect(await elementHandle.$(`text/a`)).toBeTruthy();
expect((await elementHandle.$$(`text/a`)).length).toBe(1);
});
it('should return null for non-existing element', async () => {
const {page} = getTestState();
await page.setContent('
');
const elementHandle = (await page.$('div'))!;
expect(await elementHandle.$(`text/a`)).toBeFalsy();
expect((await elementHandle.$$(`text/a`)).length).toBe(0);
});
});
});
describe('XPath selectors', function () {
describe('in Page', function () {
it('should query existing element', async () => {
const {page} = getTestState();
await page.setContent('
');
expect(await page.$('xpath/html/body/section')).toBeTruthy();
expect((await page.$$('xpath/html/body/section')).length).toBe(1);
});
it('should return empty array for non-existing element', async () => {
const {page} = getTestState();
expect(
await page.$('xpath/html/body/non-existing-element')
).toBeFalsy();
expect(
(await page.$$('xpath/html/body/non-existing-element')).length
).toBe(0);
});
it('should return first element', async () => {
const {page} = getTestState();
await page.setContent('
a
');
const element = await page.$('xpath/html/body/div');
expect(
await element?.evaluate(e => {
return e.textContent === 'a';
})
).toBeTruthy();
});
it('should return multiple elements', async () => {
const {page} = getTestState();
await page.setContent('
');
const elements = await page.$$('xpath/html/body/div');
expect(elements.length).toBe(2);
});
});
describe('in ElementHandles', function () {
it('should query existing element', async () => {
const {page} = getTestState();
await page.setContent('
a
');
const elementHandle = (await page.$('div'))!;
expect(await elementHandle.$(`xpath/span`)).toBeTruthy();
expect((await elementHandle.$$(`xpath/span`)).length).toBe(1);
});
it('should return null for non-existing element', async () => {
const {page} = getTestState();
await page.setContent('
a
');
const elementHandle = (await page.$('div'))!;
expect(await elementHandle.$(`xpath/span`)).toBeFalsy();
expect((await elementHandle.$$(`xpath/span`)).length).toBe(0);
});
});
});
});