Skip to content

Commit 42035f7

Browse files
committed
feat: Support HTTP Basic auth in addition to form auth, and document this
1 parent ad0ccb1 commit 42035f7

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

.github/actions/auth/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# auth
22

3-
Log in using Playwright, then write authenticated session state to a file for reuse in other Playwright sessions.
3+
Authenticate with Playwright and save session state. Supports HTTP Basic and form authentication.
44

55
## Usage
66

.github/actions/auth/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "Auth"
2-
description: "Log in using Playwright, then write authenticated session state to a file for reuse in other Playwright sessions."
2+
description: "Authenticate with Playwright and save session state. Supports HTTP Basic and form authentication."
33

44
inputs:
55
login_url:
@@ -22,4 +22,4 @@ runs:
2222

2323
branding:
2424
icon: "lock"
25-
color: "blue"
25+
color: "blue"

.github/actions/auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "auth",
33
"version": "1.0.0",
4-
"description": "Log in using Playwright, then write authenticated session state to a file for reuse in other Playwright sessions.",
4+
"description": "Authenticate with Playwright and save session state. Supports HTTP Basic and form authentication.",
55
"main": "dist/index.js",
66
"module": "dist/index.js",
77
"scripts": {

.github/actions/auth/src/index.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,40 @@ export default async function () {
2929
headless: true,
3030
executablePath: process.env.CI ? "/usr/bin/google-chrome" : undefined,
3131
});
32-
context = await browser.newContext();
32+
context = await browser.newContext({
33+
// Try HTTP Basic authentication
34+
httpCredentials: {
35+
username: username,
36+
password: password,
37+
},
38+
});
3339
page = await context.newPage();
3440

35-
// Log in
41+
// Navigate to login page
3642
core.info("Navigating to login page");
3743
await page.goto(loginUrl);
38-
core.info("Filling username");
39-
await page.getByLabel(/username/i).fill(username);
40-
core.info("Filling password");
41-
await page.getByLabel(/password/i).fill(password);
42-
core.info("Logging in");
43-
await page
44-
.getByLabel(/password/i)
45-
.locator("xpath=ancestor::form")
46-
.evaluate((form) => (form as HTMLFormElement).submit());
44+
45+
// Check for a login form
46+
const usernameField = await page.getByLabel(/username/i).first();
47+
const passwordField = await page.getByLabel(/password/i).first();
48+
if (
49+
(await usernameField.count()) > 0 &&
50+
(await passwordField.count()) > 0
51+
) {
52+
// Try form authentication
53+
core.info("Filling username");
54+
await page.getByLabel(/username/i).fill(username);
55+
core.info("Filling password");
56+
await page.getByLabel(/password/i).fill(password);
57+
core.info("Logging in");
58+
await page
59+
.getByLabel(/password/i)
60+
.locator("xpath=ancestor::form")
61+
.evaluate((form) => (form as HTMLFormElement).submit());
62+
} else {
63+
core.info("No login form detected");
64+
// This occurs if HTTP Basic auth succeeded, or if the page does not require authentication.
65+
}
4766

4867
// Write authenticated session state to a file and output its path
4968
await context.storageState({ path: sessionStatePath });

0 commit comments

Comments
 (0)