fix(selectors): nicer errors if the selector engine returns a bad value (#8259)

Co-authored-by: Pavel Feldman <pavel.feldman@gmail.com>
This commit is contained in:
Joel Einbinder 2021-08-23 16:54:02 -04:00 committed by GitHub
parent 4dac4772ca
commit 0ed3c79d51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -157,8 +157,11 @@ export class InjectedScript {
queryResults[index] = all;
}
for (const element of all)
for (const element of all) {
if (!('nodeName' in element))
throw new Error(`Expected a Node but got ${Object.prototype.toString.call(element)}`);
result.push({ element, capture });
}
}
return this._querySelectorRecursively(result, selector, index + 1, queryCache);
}

View file

@ -133,3 +133,19 @@ it('should not rely on engines working from the root', async ({ playwright, brow
await page.setContent(`<input id=input1 value=value1><input id=input2 value=value2>`);
expect(await page.$eval('input >> __value=value2', e => e.id)).toBe('input2');
});
it('should throw a nice error if the selector returns a bad value', async ({ playwright, browser }) => {
const page = await browser.newPage();
const createFakeEngine = () => ({
query(root, selector) {
return [document.body];
},
queryAll(root, selector) {
return [[document.body]];
},
});
await playwright.selectors.register('__fake', createFakeEngine);
const error = await page.$('__fake=value2').catch(e => e);
expect(error.message).toContain('Expected a Node but got [object Array]');
});