mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: remove type castings (#12322)
This commit is contained in:
parent
97637036c1
commit
e254f746ee
@ -107,9 +107,7 @@ If you are using TypeScript, you may have to provide an explicit type to the fir
|
|||||||
## Example 2
|
## Example 2
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// if you don't provide HTMLInputElement here, TS will error
|
await page.$$eval('input', elements => {
|
||||||
// as `value` is not on `Element`
|
|
||||||
await page.$$eval('input', (elements: HTMLInputElement[]) => {
|
|
||||||
return elements.map(e => e.value);
|
return elements.map(e => e.value);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -119,10 +117,7 @@ The compiler should be able to infer the return type from the `pageFunction` you
|
|||||||
## Example 3
|
## Example 3
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// The compiler can infer the return type in this case, but if it can't
|
const allInputValues = await page.$$eval('input', elements =>
|
||||||
// or if you want to be more explicit, provide it as the generic type.
|
elements.map(e => e.textContent)
|
||||||
const allInputValues = await page.$$eval<string[]>(
|
|
||||||
'input',
|
|
||||||
(elements: HTMLInputElement[]) => elements.map(e => e.textContent)
|
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
@ -1232,9 +1232,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
|
|||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* // if you don't provide HTMLInputElement here, TS will error
|
* await page.$$eval('input', elements => {
|
||||||
* // as `value` is not on `Element`
|
|
||||||
* await page.$$eval('input', (elements: HTMLInputElement[]) => {
|
|
||||||
* return elements.map(e => e.value);
|
* return elements.map(e => e.value);
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
@ -1246,11 +1244,8 @@ export abstract class Page extends EventEmitter<PageEvents> {
|
|||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* // The compiler can infer the return type in this case, but if it can't
|
* const allInputValues = await page.$$eval('input', elements =>
|
||||||
* // or if you want to be more explicit, provide it as the generic type.
|
* elements.map(e => e.textContent)
|
||||||
* const allInputValues = await page.$$eval<string[]>(
|
|
||||||
* 'input',
|
|
||||||
* (elements: HTMLInputElement[]) => elements.map(e => e.textContent)
|
|
||||||
* );
|
* );
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
|
@ -441,8 +441,8 @@ describe('Page.click', function () {
|
|||||||
server.CROSS_PROCESS_PREFIX + '/input/button.html'
|
server.CROSS_PROCESS_PREFIX + '/input/button.html'
|
||||||
);
|
);
|
||||||
const frame = page.frames()[1];
|
const frame = page.frames()[1];
|
||||||
await frame!.$eval('button', (button: Element) => {
|
await frame!.$eval('button', button => {
|
||||||
return (button as HTMLElement).style.setProperty('position', 'fixed');
|
return button.style.setProperty('position', 'fixed');
|
||||||
});
|
});
|
||||||
await frame!.click('button');
|
await frame!.click('button');
|
||||||
expect(
|
expect(
|
||||||
|
@ -58,7 +58,7 @@ describe('ElementHandle specs', function () {
|
|||||||
'<div style="width: 100px; height: 100px">hello</div>'
|
'<div style="width: 100px; height: 100px">hello</div>'
|
||||||
);
|
);
|
||||||
using elementHandle = (await page.$('div'))!;
|
using elementHandle = (await page.$('div'))!;
|
||||||
await page.evaluate((element: HTMLElement) => {
|
await page.evaluate(element => {
|
||||||
return (element.style.height = '200px');
|
return (element.style.height = '200px');
|
||||||
}, elementHandle);
|
}, elementHandle);
|
||||||
const box = await elementHandle.boundingBox();
|
const box = await elementHandle.boundingBox();
|
||||||
@ -260,7 +260,7 @@ describe('ElementHandle specs', function () {
|
|||||||
|
|
||||||
await page.goto(server.PREFIX + '/input/button.html');
|
await page.goto(server.PREFIX + '/input/button.html');
|
||||||
using button = (await page.$('button'))!;
|
using button = (await page.$('button'))!;
|
||||||
await page.evaluate((button: HTMLElement) => {
|
await page.evaluate(button => {
|
||||||
return button.remove();
|
return button.remove();
|
||||||
}, button);
|
}, button);
|
||||||
let error!: Error;
|
let error!: Error;
|
||||||
@ -277,7 +277,7 @@ describe('ElementHandle specs', function () {
|
|||||||
|
|
||||||
await page.goto(server.PREFIX + '/input/button.html');
|
await page.goto(server.PREFIX + '/input/button.html');
|
||||||
using button = (await page.$('button'))!;
|
using button = (await page.$('button'))!;
|
||||||
await page.evaluate((button: HTMLElement) => {
|
await page.evaluate(button => {
|
||||||
return (button.style.display = 'none');
|
return (button.style.display = 'none');
|
||||||
}, button);
|
}, button);
|
||||||
const error = await button.click().catch(error_ => {
|
const error = await button.click().catch(error_ => {
|
||||||
@ -293,7 +293,7 @@ describe('ElementHandle specs', function () {
|
|||||||
|
|
||||||
await page.goto(server.PREFIX + '/input/button.html');
|
await page.goto(server.PREFIX + '/input/button.html');
|
||||||
using button = (await page.$('button'))!;
|
using button = (await page.$('button'))!;
|
||||||
await page.evaluate((button: HTMLElement) => {
|
await page.evaluate(button => {
|
||||||
return (button.parentElement!.style.display = 'none');
|
return (button.parentElement!.style.display = 'none');
|
||||||
}, button);
|
}, button);
|
||||||
const error = await button.click().catch(error_ => {
|
const error = await button.click().catch(error_ => {
|
||||||
@ -478,7 +478,7 @@ describe('ElementHandle specs', function () {
|
|||||||
expect(element2).toBeDefined();
|
expect(element2).toBeDefined();
|
||||||
expect(
|
expect(
|
||||||
await element2.evaluate(el => {
|
await element2.evaluate(el => {
|
||||||
return (el as HTMLElement).innerText;
|
return el.innerText;
|
||||||
})
|
})
|
||||||
).toStrictEqual('bar1');
|
).toStrictEqual('bar1');
|
||||||
});
|
});
|
||||||
|
@ -210,7 +210,7 @@ describe('Emulation', () => {
|
|||||||
await page.emulate(iPhone);
|
await page.emulate(iPhone);
|
||||||
await page.goto(server.PREFIX + '/input/button.html');
|
await page.goto(server.PREFIX + '/input/button.html');
|
||||||
using button = (await page.$('button'))!;
|
using button = (await page.$('button'))!;
|
||||||
await page.evaluate((button: HTMLElement) => {
|
await page.evaluate(button => {
|
||||||
return (button.style.marginTop = '200px');
|
return (button.style.marginTop = '200px');
|
||||||
}, button);
|
}, button);
|
||||||
await button.click();
|
await button.click();
|
||||||
|
@ -386,7 +386,7 @@ describe('Evaluation specs', function () {
|
|||||||
await element.dispose();
|
await element.dispose();
|
||||||
let error!: Error;
|
let error!: Error;
|
||||||
await page
|
await page
|
||||||
.evaluate((e: HTMLElement) => {
|
.evaluate(e => {
|
||||||
return e.textContent;
|
return e.textContent;
|
||||||
}, element)
|
}, element)
|
||||||
.catch(error_ => {
|
.catch(error_ => {
|
||||||
|
@ -168,7 +168,7 @@ describe('input tests', function () {
|
|||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.$eval('input', input => {
|
page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).click();
|
return input.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
expect(fileChooser1 === fileChooser2).toBe(true);
|
expect(fileChooser1 === fileChooser2).toBe(true);
|
||||||
@ -192,12 +192,12 @@ describe('input tests', function () {
|
|||||||
]);
|
]);
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', input => {
|
await page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).files!.length;
|
return input.files!.length;
|
||||||
})
|
})
|
||||||
).toBe(1);
|
).toBe(1);
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', input => {
|
await page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).files![0]!.name;
|
return input.files![0]!.name;
|
||||||
})
|
})
|
||||||
).toBe('file-to-upload.txt');
|
).toBe('file-to-upload.txt');
|
||||||
});
|
});
|
||||||
@ -209,8 +209,7 @@ describe('input tests', function () {
|
|||||||
return chooser.accept([FILE_TO_UPLOAD]);
|
return chooser.accept([FILE_TO_UPLOAD]);
|
||||||
});
|
});
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', async picker => {
|
await page.$eval('input', async pick => {
|
||||||
const pick = picker as HTMLInputElement;
|
|
||||||
pick.click();
|
pick.click();
|
||||||
await new Promise(x => {
|
await new Promise(x => {
|
||||||
return (pick.oninput = x);
|
return (pick.oninput = x);
|
||||||
@ -234,8 +233,7 @@ describe('input tests', function () {
|
|||||||
return chooser.accept([FILE_TO_UPLOAD]);
|
return chooser.accept([FILE_TO_UPLOAD]);
|
||||||
});
|
});
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', async picker => {
|
await page.$eval('input', async pick => {
|
||||||
const pick = picker as HTMLInputElement;
|
|
||||||
pick.click();
|
pick.click();
|
||||||
await new Promise(x => {
|
await new Promise(x => {
|
||||||
return (pick.oninput = x);
|
return (pick.oninput = x);
|
||||||
@ -247,8 +245,7 @@ describe('input tests', function () {
|
|||||||
return chooser.accept([]);
|
return chooser.accept([]);
|
||||||
});
|
});
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', async picker => {
|
await page.$eval('input', async pick => {
|
||||||
const pick = picker as HTMLInputElement;
|
|
||||||
pick.click();
|
pick.click();
|
||||||
await new Promise(x => {
|
await new Promise(x => {
|
||||||
return (pick.oninput = x);
|
return (pick.oninput = x);
|
||||||
@ -301,8 +298,7 @@ describe('input tests', function () {
|
|||||||
return chooser.accept(['file-does-not-exist.txt']);
|
return chooser.accept(['file-does-not-exist.txt']);
|
||||||
});
|
});
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('input', async picker => {
|
await page.$eval('input', async pick => {
|
||||||
const pick = picker as HTMLInputElement;
|
|
||||||
pick.click();
|
pick.click();
|
||||||
await new Promise(x => {
|
await new Promise(x => {
|
||||||
return (pick.oninput = x);
|
return (pick.oninput = x);
|
||||||
@ -325,7 +321,7 @@ describe('input tests', function () {
|
|||||||
const [fileChooser] = await Promise.all([
|
const [fileChooser] = await Promise.all([
|
||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.$eval('input', input => {
|
page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).click();
|
return input.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
await fileChooser.accept([]);
|
await fileChooser.accept([]);
|
||||||
@ -350,7 +346,7 @@ describe('input tests', function () {
|
|||||||
const [fileChooser1] = await Promise.all([
|
const [fileChooser1] = await Promise.all([
|
||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.$eval('input', input => {
|
page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).click();
|
return input.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
await fileChooser1.cancel();
|
await fileChooser1.cancel();
|
||||||
@ -358,7 +354,7 @@ describe('input tests', function () {
|
|||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.$eval('input', input => {
|
page.$eval('input', input => {
|
||||||
return (input as HTMLInputElement).click();
|
return input.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -369,7 +365,7 @@ describe('input tests', function () {
|
|||||||
const [fileChooser] = await Promise.all([
|
const [fileChooser] = await Promise.all([
|
||||||
page.waitForFileChooser(),
|
page.waitForFileChooser(),
|
||||||
page.$eval('input', input => {
|
page.$eval('input', input => {
|
||||||
return (input as HTMLElement).click();
|
return input.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
await fileChooser.cancel();
|
await fileChooser.cancel();
|
||||||
|
@ -750,7 +750,7 @@ describe('Locator', function () {
|
|||||||
await page.setContent(`<div onclick="window.clicked = true">test</div>`);
|
await page.setContent(`<div onclick="window.clicked = true">test</div>`);
|
||||||
await page
|
await page
|
||||||
.locator(() => {
|
.locator(() => {
|
||||||
return document.getElementsByTagName('div')[0] as HTMLDivElement;
|
return document.getElementsByTagName('div')[0]!;
|
||||||
})
|
})
|
||||||
.click();
|
.click();
|
||||||
await expect(
|
await expect(
|
||||||
|
@ -285,7 +285,7 @@ describe('Page', function () {
|
|||||||
const [popup] = await Promise.all([
|
const [popup] = await Promise.all([
|
||||||
waitEvent<Page>(page, 'popup'),
|
waitEvent<Page>(page, 'popup'),
|
||||||
page.$eval('a', a => {
|
page.$eval('a', a => {
|
||||||
return (a as HTMLAnchorElement).click();
|
return a.click();
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
expect(
|
expect(
|
||||||
@ -1830,7 +1830,7 @@ describe('Page', function () {
|
|||||||
path: path.join(__dirname, '../assets/injectedstyle.css'),
|
path: path.join(__dirname, '../assets/injectedstyle.css'),
|
||||||
});
|
});
|
||||||
using styleHandle = (await page.$('style'))!;
|
using styleHandle = (await page.$('style'))!;
|
||||||
const styleContent = await page.evaluate((style: HTMLStyleElement) => {
|
const styleContent = await page.evaluate(style => {
|
||||||
return style.innerHTML;
|
return style.innerHTML;
|
||||||
}, styleHandle);
|
}, styleHandle);
|
||||||
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css'));
|
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css'));
|
||||||
@ -2144,11 +2144,9 @@ describe('Page', function () {
|
|||||||
await page.select('select');
|
await page.select('select');
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('select', select => {
|
await page.$eval('select', select => {
|
||||||
return Array.from((select as HTMLSelectElement).options).every(
|
return Array.from(select.options).every(option => {
|
||||||
option => {
|
return !option.selected;
|
||||||
return !option.selected;
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
).toEqual(true);
|
).toEqual(true);
|
||||||
});
|
});
|
||||||
@ -2160,11 +2158,9 @@ describe('Page', function () {
|
|||||||
await page.select('select');
|
await page.select('select');
|
||||||
expect(
|
expect(
|
||||||
await page.$eval('select', select => {
|
await page.$eval('select', select => {
|
||||||
return Array.from((select as HTMLSelectElement).options).filter(
|
return Array.from(select.options).filter(option => {
|
||||||
option => {
|
return option.selected;
|
||||||
return option.selected;
|
})[0]!.value;
|
||||||
}
|
|
||||||
)[0]!.value;
|
|
||||||
})
|
})
|
||||||
).toEqual('');
|
).toEqual('');
|
||||||
});
|
});
|
||||||
|
@ -43,7 +43,7 @@ describe('querySelector', function () {
|
|||||||
const text = await page.$eval(
|
const text = await page.$eval(
|
||||||
'section',
|
'section',
|
||||||
(e, div) => {
|
(e, div) => {
|
||||||
return e.textContent! + (div as HTMLElement).textContent!;
|
return e.textContent! + div.textContent!;
|
||||||
},
|
},
|
||||||
divHandle
|
divHandle
|
||||||
);
|
);
|
||||||
@ -108,7 +108,7 @@ describe('querySelector', function () {
|
|||||||
return (
|
return (
|
||||||
sections.reduce((acc, section) => {
|
sections.reduce((acc, section) => {
|
||||||
return acc + Number(section.textContent);
|
return acc + Number(section.textContent);
|
||||||
}, 0) + Number((div as HTMLElement).textContent)
|
}, 0) + Number(div.textContent)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
divHandle
|
divHandle
|
||||||
@ -161,7 +161,7 @@ describe('querySelector', function () {
|
|||||||
const elements = await page.$$('div');
|
const elements = await page.$$('div');
|
||||||
expect(elements).toHaveLength(2);
|
expect(elements).toHaveLength(2);
|
||||||
const promises = elements.map(element => {
|
const promises = elements.map(element => {
|
||||||
return page.evaluate((e: HTMLElement) => {
|
return page.evaluate(e => {
|
||||||
return e.textContent;
|
return e.textContent;
|
||||||
}, element);
|
}, element);
|
||||||
});
|
});
|
||||||
@ -330,7 +330,7 @@ describe('querySelector', function () {
|
|||||||
const elements = await html.$$('div');
|
const elements = await html.$$('div');
|
||||||
expect(elements).toHaveLength(2);
|
expect(elements).toHaveLength(2);
|
||||||
const promises = elements.map(element => {
|
const promises = elements.map(element => {
|
||||||
return page.evaluate((e: HTMLElement) => {
|
return page.evaluate(e => {
|
||||||
return e.textContent;
|
return e.textContent;
|
||||||
}, element);
|
}, element);
|
||||||
});
|
});
|
||||||
@ -463,7 +463,7 @@ describe('querySelector', function () {
|
|||||||
return (
|
return (
|
||||||
sections.reduce((acc, section) => {
|
sections.reduce((acc, section) => {
|
||||||
return acc + Number(section.textContent);
|
return acc + Number(section.textContent);
|
||||||
}, 0) + Number((div as HTMLElement).textContent)
|
}, 0) + Number(div.textContent)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
divHandle
|
divHandle
|
||||||
|
@ -141,7 +141,7 @@ describe('cooperative request interception', function () {
|
|||||||
`);
|
`);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.$eval('form', form => {
|
page.$eval('form', form => {
|
||||||
return (form as HTMLFormElement).submit();
|
return form.submit();
|
||||||
}),
|
}),
|
||||||
page.waitForNavigation(),
|
page.waitForNavigation(),
|
||||||
]);
|
]);
|
||||||
@ -631,7 +631,7 @@ describe('cooperative request interception', function () {
|
|||||||
void (page.$eval(
|
void (page.$eval(
|
||||||
'iframe',
|
'iframe',
|
||||||
(frame, url) => {
|
(frame, url) => {
|
||||||
return ((frame as HTMLIFrameElement).src = url as string);
|
return (frame.src = url as string);
|
||||||
},
|
},
|
||||||
server.EMPTY_PAGE
|
server.EMPTY_PAGE
|
||||||
),
|
),
|
||||||
|
@ -70,7 +70,7 @@ describe('request interception', function () {
|
|||||||
`);
|
`);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.$eval('form', form => {
|
page.$eval('form', form => {
|
||||||
return (form as HTMLFormElement).submit();
|
return form.submit();
|
||||||
}),
|
}),
|
||||||
page.waitForNavigation(),
|
page.waitForNavigation(),
|
||||||
]);
|
]);
|
||||||
@ -550,7 +550,7 @@ describe('request interception', function () {
|
|||||||
void (page.$eval(
|
void (page.$eval(
|
||||||
'iframe',
|
'iframe',
|
||||||
(frame, url) => {
|
(frame, url) => {
|
||||||
return ((frame as HTMLIFrameElement).src = url as string);
|
return (frame.src = url);
|
||||||
},
|
},
|
||||||
server.EMPTY_PAGE
|
server.EMPTY_PAGE
|
||||||
),
|
),
|
||||||
|
@ -88,12 +88,10 @@ export async function detachFrame(
|
|||||||
pageOrFrame: Page | Frame,
|
pageOrFrame: Page | Frame,
|
||||||
frameId: string
|
frameId: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await pageOrFrame.evaluate(detachFrame, frameId);
|
await pageOrFrame.evaluate(frameId => {
|
||||||
|
|
||||||
function detachFrame(frameId: string) {
|
|
||||||
const frame = document.getElementById(frameId) as HTMLIFrameElement;
|
const frame = document.getElementById(frameId) as HTMLIFrameElement;
|
||||||
frame.remove();
|
frame.remove();
|
||||||
}
|
}, frameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function navigateFrame(
|
export async function navigateFrame(
|
||||||
@ -101,15 +99,17 @@ export async function navigateFrame(
|
|||||||
frameId: string,
|
frameId: string,
|
||||||
url: string
|
url: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await pageOrFrame.evaluate(navigateFrame, frameId, url);
|
await pageOrFrame.evaluate(
|
||||||
|
(frameId, url) => {
|
||||||
function navigateFrame(frameId: string, url: string) {
|
const frame = document.getElementById(frameId) as HTMLIFrameElement;
|
||||||
const frame = document.getElementById(frameId) as HTMLIFrameElement;
|
frame.src = url;
|
||||||
frame.src = url;
|
return new Promise(x => {
|
||||||
return new Promise(x => {
|
return (frame.onload = x);
|
||||||
return (frame.onload = x);
|
});
|
||||||
});
|
},
|
||||||
}
|
frameId,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const dumpFrames = async (
|
export const dumpFrames = async (
|
||||||
|
@ -225,7 +225,7 @@ describe('waittask specs', function () {
|
|||||||
return (resolved = true);
|
return (resolved = true);
|
||||||
});
|
});
|
||||||
expect(resolved).toBe(false);
|
expect(resolved).toBe(false);
|
||||||
await page.evaluate((element: HTMLElement) => {
|
await page.evaluate(element => {
|
||||||
return element.remove();
|
return element.remove();
|
||||||
}, div);
|
}, div);
|
||||||
await waitForFunction;
|
await waitForFunction;
|
||||||
|
Loading…
Reference in New Issue
Block a user