Skip to content

Commit

Permalink
fix: making progressions more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Huellen committed Sep 17, 2024
1 parent 3969551 commit e73f744
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 55 deletions.
13 changes: 6 additions & 7 deletions src/common/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@ export const _interact = async (page: Page, interaction: Interaction) => {
/* Element Interactions */
const { annotation } = interaction;

const element = await page.locator(
`[x-pretzelduck-annotation="(PD:${annotation})"]`,
);
const selector = `[x-pretzelduck-annotation="(PD:${annotation})"]`;

const element = await page.locator(selector);

if (type === 'click') {
await element.dispatchEvent('click');

return element;
}

if (type === 'input') {
const { value } = interaction;

await element.focus();
await element.fill(value);

return element;
}

return selector;
};
82 changes: 39 additions & 43 deletions src/common/progress.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
import type { Locator } from '@playwright/test';
import type { Page } from '@playwright/test';
import type { ModifiedField } from '../types/ModifiableField';
import type { TestOptions } from '../types/TestOptions';

import { replaceLast } from './utils/replaceLast';

export const progress = async (
element: Locator,
{ timeout }: TestOptions['decisions']['progressions'],
) =>
element.evaluate(
(element) => {
const annotation = element.getAttribute('x-pretzelduck-annotation');
export const progress = async (page: Page, selector: string) =>
page.evaluate((selector) => {
const element = document.querySelector(selector);

if (annotation == null) {
return;
}

element.removeAttribute('x-pretzelduck-annotation');
if (element == null) {
return;
}

const modifiedField = element.getAttribute(
'x-pretzelduck-modified-field',
) as ModifiedField;
const annotation = element.getAttribute('x-pretzelduck-annotation');

switch (modifiedField) {
case 'text-content': {
const { textContent } = element;
if (annotation == null) {
return;
}

if (textContent == null) {
return;
}
element.removeAttribute('x-pretzelduck-annotation');

const newTextContent = replaceLast(textContent, annotation, '');
const modifiedField = element.getAttribute(
'x-pretzelduck-modified-field',
) as ModifiedField;

element.textContent = newTextContent;
switch (modifiedField) {
case 'text-content': {
const { textContent } = element;

break;
if (textContent == null) {
return;
}
case 'placeholder':
case 'value': {
const attribute = modifiedField;

const attributeValue = element.getAttribute(attribute);
const newTextContent = replaceLast(textContent, annotation, '');

if (attributeValue == null) {
return;
}
element.textContent = newTextContent;

const lastAnnotationIndex = attributeValue.lastIndexOf(annotation);
const newValue = attributeValue.slice(0, lastAnnotationIndex);
break;
}
case 'placeholder':
case 'value': {
const attribute = modifiedField;

element.setAttribute(attribute, newValue);
const attributeValue = element.getAttribute(attribute);

break;
if (attributeValue == null) {
return;
}

const lastAnnotationIndex = attributeValue.lastIndexOf(annotation);
const newValue = attributeValue.slice(0, lastAnnotationIndex);

element.setAttribute(attribute, newValue);

break;
}
},
undefined,
{
timeout,
},
);
}
}, selector);
6 changes: 3 additions & 3 deletions src/common/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const _test = (

const previousPage = page.url();

const element = await _interact(page, interaction);
const selector = await _interact(page, interaction);
interactionCount++;

await page.waitForLoadState('domcontentloaded');
Expand All @@ -82,8 +82,8 @@ export const _test = (

const currentPage = page.url();

if (enabled && previousPage === currentPage && element !== undefined) {
await Promise.allSettled([progress(element, progressions)]);
if (enabled && previousPage === currentPage && selector !== undefined) {
await progress(page, selector);
}
} while (interactionCount < maxInteractions);

Expand Down
2 changes: 0 additions & 2 deletions src/types/TestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export type TestOptions = {
progressions: {
enabled: boolean;
type: 'forced';
timeout: number;
};
temperature: number;
maxEntropy: number;
Expand Down Expand Up @@ -50,7 +49,6 @@ export const DEFAULT_TEST_OPTIONS: TestOptions = {
progressions: {
enabled: true,
type: 'forced',
timeout: 5000,
},
temperature: 0.3,
maxEntropy: 0.05,
Expand Down

0 comments on commit e73f744

Please sign in to comment.