Skip to content

Commit

Permalink
feat: sync various improvements (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Feb 21, 2022
1 parent d0de312 commit 3d68273
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
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;
}
}
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 (
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

0 comments on commit 3d68273

Please sign in to comment.