Skip to content

Commit

Permalink
feature(webpage-impact): add support for simple logins
Browse files Browse the repository at this point in the history
This is experimental. The login functionality expects
certain IDs to be present on HTML elements of the login
page. Otherwise it won't work. So it is far from being
satisfying, but let's try as a start.

Signed-off-by: alexzurbonsen <[email protected]>
  • Loading branch information
alexzurbonsen committed Oct 27, 2024
1 parent 89b300d commit 183eb01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/webpage-impact/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The follwing config parameters are optional:
- `headers`:
- `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
- `username`: The plugin supports simple logins with username and password. Provide the username if the page you want to measure requires such a login. The password needs to be stored in a shell environment variable called `WI_PASSWORD`. Please note, that the login may fail nevertheless because the plugin expects a certain interface that may not be provided by your page.

### Returns

Expand Down
18 changes: 17 additions & 1 deletion 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;
username?: string;
};

type ResourceBase = {
Expand Down Expand Up @@ -180,6 +181,7 @@ const WebpageImpactUtils = () => {
reload: false,
cacheEnabled: false,
scrollToBottom: config?.scrollToBottom,
username: config?.username,
});

let reloadedResources: Resource[] | undefined;
Expand Down Expand Up @@ -207,7 +209,7 @@ const WebpageImpactUtils = () => {
const loadPageResources = async (
page: Page,
url: string,
{reload, cacheEnabled, scrollToBottom}: WebpageImpactOptions
{reload, cacheEnabled, scrollToBottom, username}: WebpageImpactOptions
): Promise<Resource[]> => {
try {
await page.setCacheEnabled(cacheEnabled);
Expand Down Expand Up @@ -258,6 +260,20 @@ const WebpageImpactUtils = () => {

if (!reload) {
await page.goto(url, {waitUntil: 'networkidle0'});
if (username !== undefined) {
const password = process.env.WI_PASSWORD;
if (password === undefined) {
throw new Error(
`${LOGGER_PREFIX}: Username but no password provided. Store password in environment variable 'WI_PASSWORD'.`
);
}
await page.type('#username', username);
await page.type('#password', password);

await page.click('#submit');

await page.waitForNavigation();
}
} else {
await page.reload({waitUntil: 'networkidle0'});
}
Expand Down

0 comments on commit 183eb01

Please sign in to comment.