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

[EXPERIMENTAL] feature(webpage-impact): add support for custom user interactions #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion example-manifests/measure-webpage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ initialize:
config:
url: https://www.tngtech.com
scrollToBottom: true
computeReloadRati: true
computeReloadRatio: true
customUserInteraction: '/absolute/path/to/your/script.js'
'co2js':
method: Co2js
path: '@tngtech/if-webpage-plugins'
Expand Down
27 changes: 27 additions & 0 deletions src/lib/webpage-impact/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ The follwing config parameters are optional:
- `accept`: string https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
- `accept-encoding`: array of allowed encodings (a single encoding can also be passed as a string) https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

Experimental:

- `customUserInteraction`: absolute path to a Javascript Common JS module, that exports a function with the signature `userInteraction: (page: Page) => Promise<void>`. For an example see below. Setting `customUserInteraction` cannot be set together with `scrollToBottom`.

### Returns

- `network/data/bytes`: page weight in bytes
Expand Down Expand Up @@ -81,3 +85,26 @@ tree:
- webpage-impact
inputs:
```

## Experimental: Custom user interactions

Example script:

```js
const userInteraction = async page => {
// on tngtech.com click on the "Leistungen" link
await page.locator('#menu > ul > li:nth-child(1) > a').click();
await page.waitForNavigation();

// for debugging purposes
await page.screenshot({
path: '/absolute/path/image.png',
});
};

exports.userInteraction = userInteraction;
```

Puppeteer docs on page interactions: https://pptr.dev/guides/page-interactions

TODO: How to deal with the reload option?
43 changes: 39 additions & 4 deletions src/lib/webpage-impact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type WebpageImpactOptions = {
reload: boolean;
cacheEnabled: boolean;
scrollToBottom?: boolean;
customUserInteraction?: string;
};

type ResourceBase = {
Expand All @@ -36,6 +37,10 @@ type Resource = ResourceBase & {transferSize: number};

type Device = keyof typeof KnownDevices;

interface CustomUserInteraction {
userInteraction: (page: Page) => Promise<void>;
}

const LOGGER_PREFIX = 'WebpageImpact';

const ALLOWED_ENCODINGS = [
Expand Down Expand Up @@ -176,6 +181,7 @@ const WebpageImpactUtils = () => {
reload: false,
cacheEnabled: false,
scrollToBottom: config?.scrollToBottom,
customUserInteraction: config?.customUserInteraction,
});

let reloadedResources: Resource[] | undefined;
Expand Down Expand Up @@ -203,7 +209,12 @@ const WebpageImpactUtils = () => {
const loadPageResources = async (
page: Page,
url: string,
{reload, cacheEnabled, scrollToBottom}: WebpageImpactOptions
{
reload,
cacheEnabled,
scrollToBottom,
customUserInteraction,
}: WebpageImpactOptions
): Promise<Resource[]> => {
try {
await page.setCacheEnabled(cacheEnabled);
Expand Down Expand Up @@ -254,14 +265,19 @@ const WebpageImpactUtils = () => {

if (!reload) {
await page.goto(url, {waitUntil: 'networkidle0'});
if (customUserInteraction) {
const module = await import(customUserInteraction);
if (iscustomUserInteraction(module)) {
await module.userInteraction(page);
}
}
} else {
console.log('before reload');
await page.reload({waitUntil: 'networkidle0'});
}

if (scrollToBottom) {
// await page.screenshot({path: './TOP.png'});
if (!customUserInteraction && scrollToBottom) {
await page.evaluate(scrollToBottomOfPage);
// await page.screenshot({path: './BOTTOM.png'});
}

await cdpSession.detach();
Expand All @@ -274,6 +290,15 @@ const WebpageImpactUtils = () => {
}
};

const iscustomUserInteraction = (
customUserInteraction: any
): customUserInteraction is CustomUserInteraction => {
return (
'userInteraction' in customUserInteraction &&
typeof customUserInteraction['userInteraction'] === 'function'
);
};

const mergeCdpData = (
cdpResponses: Record<string, ResourceBase>,
cdpTransferSizes: Record<string, {transferSize: number}>
Expand Down Expand Up @@ -375,6 +400,7 @@ const WebpageImpactUtils = () => {
mobileDevice: z.string().optional(),
emulateNetworkConditions: z.string().optional(),
scrollToBottom: z.boolean().optional(),
customUserInteraction: z.string().optional(),
headers: z
.object({
accept: z.string().optional(),
Expand Down Expand Up @@ -417,6 +443,15 @@ const WebpageImpactUtils = () => {
PredefinedNetworkConditions
).join(', ')}.`,
}
)
.refine(
data => {
return !(data?.scrollToBottom && data?.customUserInteraction);
},
{
message:
'`scrollToBottom: true` and `customUserInteraction` cannot be provided together.',
}
);

return validate<z.infer<typeof configSchema>>(configSchema, config);
Expand Down