Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync various improvements #25

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ export class PuppeteerRunnerExtension extends RunnerExtension {

const targetPage = await getTargetPageForStep(browser, page, step, timeout);
let targetFrame: Frame | null = null;
if (!targetPage) {
if (!targetPage && step.target) {
const frames = page.frames();
for (const f of frames) {
if (f.isOOPFrame() && f.url() === step.target) {
targetFrame = f;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we break after the first frame found?

}
}
if (!targetFrame) {
targetFrame = await page.waitForFrame(step.target, { timeout });
}
}
const pageOrFrame = targetPage || targetFrame;
if (!pageOrFrame) {
Expand Down Expand Up @@ -114,11 +117,26 @@ export class PuppeteerRunnerExtension extends RunnerExtension {
(el: Element) => (el as HTMLInputElement).type
);
if (typeableInputTypes.has(inputType)) {
await element.evaluate((el: Element) => {
/* c8 ignore next 1 */
(el as HTMLInputElement).value = '';
});
await element.type(step.value);
const textToType = await element.evaluate(
(el: Element, newValue: string) => {
/* c8 ignore next 10 */
const input = el as HTMLInputElement;
if (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we invert this so the shorter code comes first?

newValue.length > input.value.length &&
newValue.startsWith(input.value)
) {
const originalValue = input.value;
// Move cursor to the end of the common prefix.
input.value = '';
input.value = originalValue;
return newValue.substring(originalValue.length);
}
input.value = '';
return newValue;
},
step.value
);
await element.type(textToType);
} else {
await element.focus();
await element.evaluate((el: Element, value: string) => {
Expand Down Expand Up @@ -566,6 +584,7 @@ interface Page {
$$<T extends Element = Element>(
selector: string
): Promise<Array<ElementHandle<T>>>;
waitForFrame(url: string, opts: { timeout: number }): Promise<Frame>;
}

interface Frame {
Expand Down
1 change: 1 addition & 0 deletions test/resources/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<input id="url" type="url">
<input id="week" type="week">
<input id="prefilled" value="abc">
<input id="partially-prefilled" value="abc">
<pre id="log"></div>
<script>
window.addEventListener('input', (e) => {
Expand Down
27 changes: 27 additions & 0 deletions test/runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,33 @@ describe('Runner', () => {
assert.strictEqual(value, 'cba');
});

it('should be able to override the value in text inputs that are partially prefilled', async () => {
const runner = await createRunner(
{
title: 'test',
steps: [
{
type: 'navigate',
url: `${HTTP_PREFIX}/input.html`,
},
{
type: 'change',
target: 'main',
selectors: ['#partially-prefilled'],
value: 'abcdef',
},
],
},
new PuppeteerRunnerExtension(browser, page)
);
await runner.run();
const value = await page.$eval(
'#partially-prefilled',
(e) => (e as HTMLSelectElement).value
);
assert.strictEqual(value, 'abcdef');
});

it('should be able to replay viewport change', async () => {
const runner = await createRunner(
{
Expand Down