add more tests

This commit is contained in:
Brian Schiller
2019-02-04 11:11:55 -07:00
parent 9dc803037c
commit e7a236a531
+44 -1
View File
@@ -531,7 +531,7 @@ puppeteer.launch().then(async browser => {
});
});
// evaluate returns type of inner function
// evaluates return type of inner function
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
@@ -547,6 +547,7 @@ puppeteer.launch().then(async browser => {
console.log('body html has length', s.length);
});
// JSHandle.jsonValue produces compatible type
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
@@ -575,3 +576,45 @@ puppeteer.launch().then(async browser => {
a: '1'
});
});
// ElementHandles are well-typed
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const link: puppeteer.JSHandle = await page.evaluateHandle(
() => document.body.querySelector('a')
);
const linkEl: puppeteer.ElementHandle | null = link.asElement();
if (linkEl !== null) {
const href = await page.evaluate(
(el: HTMLElement): string | null => el.getAttribute('href'),
linkEl);
console.log('href is', href);
}
});
// test $$eval return type
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const paragraphContents: string[] = await page.$$eval(
'p', (ps: Element[]): string[] => ps.map(p => p.textContent || ''));
console.log('pgraph contents', paragraphContents);
});
// JSHandle of non-serializable works
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const reHandle: puppeteer.JSHandle = await page.evaluateHandle(
() => /\s*bananas?\s*/i,
);
const numMatchingEls: number = await page.$$eval(
'p', (els: Element[], re: RegExp) =>
els.filter(el => el.textContent && re.test(el.textContent)).length,
reHandle
);
console.log('there are', numMatchingEls, 'banana paragaphs');
});