Skip to content

Commit

Permalink
Allow Playwright tests to be authenticated
Browse files Browse the repository at this point in the history
The `GH_TOKEN` environment variable can be used both locally and in CI
to avoid testing the GitHub OAuth flow (which is provided by the Octokit
library and can therefore be assumed to be well tested).

In local development the token can be retrieved by logging into Towtruck
and then copying the value of the `Token` cookie.

In CI, the correct value is automatically populated by GitHub Actions
into a secret.
  • Loading branch information
danlivings-dxw committed Dec 12, 2024
1 parent cfd0649 commit 1309d80
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
REDIRECT_URL_BASE: ${{ secrets.REDIRECT_URL_BASE }}
PLAYWRIGHT_GITHUB_USERNAME: ${{ secrets.PLAYWRIGHT_GITHUB_USERNAME }}
PLAYWRIGHT_GITHUB_PASSWORD: ${{ secrets.PLAYWRIGHT_GITHUB_PASSWORD }}
- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down
22 changes: 22 additions & 0 deletions e2es/auth.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test as setup } from '@playwright/test';

const USERNAME = process.env.PLAYWRIGHT_GITHUB_USERNAME;
const PASSWORD = process.env.PLAYWRIGHT_GITHUB_PASSWORD;

console.log(USERNAME, PASSWORD);

setup('authenticate', async ({ page, baseURL }, testInfo) => {
await page.goto(baseURL);
await page.getByRole('button', { name: 'Login with GitHub' }).click();

await page.waitForURL(/https:\/\/github.com\/login/);

await page.getByLabel('Username or email address').fill(USERNAME);
await page.getByLabel('Password').fill(PASSWORD);

await page.getByRole('button', { name: 'Sign in' }).click();

await page.waitForURL(baseURL);

await page.context().storageState({ path: testInfo.outputPath('user.json') });
});
File renamed without changes.
Binary file modified e2es/testData/towtruck.db
Binary file not shown.
6 changes: 5 additions & 1 deletion e2es/towtruck.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ test("has dependency info", async ({ page, baseURL }) => {

await expect(page).toHaveTitle(/Towtruck/);

page.getByText("There are 3 repositories that Towtruck is tracking for dxw.");
expect(await page.getByText("There is 1 organisation using Towtruck that you are a member of.").count()).toBe(1);

await page.getByRole("link", { name: "dxw", exact: true }).click();

expect(await page.getByText("There are 3 repositories that Towtruck is tracking for dxw.").count()).toBe(1);

const tableHeadings = [
"Name",
Expand Down
8 changes: 4 additions & 4 deletions playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ export default defineConfig({

projects: [
{
name: "setup data",
testMatch: /seed\.test\.data\.js/,
name: "setup",
testMatch: /.*\.setup\.js/,
},
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
dependencies: ["setup data"],
dependencies: ["setup"],
},
],

/* Run your local dev server before starting the tests */
webServer: {
command: "node ./e2es/seedTestData.js && script/server",
command: "script/server",
url: "http://127.0.0.1:3000",
reuseExistingServer: !process.env.CI,
},
Expand Down

0 comments on commit 1309d80

Please sign in to comment.