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

End to end tests #66

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
29 changes: 29 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
jobs:
test:
env:
PB_ADMIN_BASIC: ${{ secrets.PB_ADMIN_BASIC }}
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ src/pb.d.ts
/pocketbase-data

.firebase
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
77 changes: 77 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"check:format": "prettier --check ."
},
"devDependencies": {
"@playwright/test": "^1.48.2",
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/adapter-node": "^5.2.7",
"@sveltejs/kit": "^2.5.27",
Expand All @@ -39,6 +40,7 @@
"dependencies": {
"@types/eventsource": "^1.1.15",
"@types/node": "^22.9.0",
"dotenv": "^16.4.5",
"eventsource": "^2.0.2",
"pocketbase": "^0.21.5",
"vite-plugin-node-polyfills": "^0.22.0"
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
import dotenv from "dotenv";
dotenv.config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./src/test",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://localhost:5173/",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry"
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] }
},

{
name: "firefox",
use: { ...devices["Desktop Firefox"] }
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] }
}

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: "npm run dev",
url: "http://localhost:5173/",
reuseExistingServer: !process.env.CI
}
});
11 changes: 10 additions & 1 deletion src/lib/components/AuthButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
goHome();
}

async function loginWithBasicAuth() {
const username = prompt("Enter your username:");
const password = prompt("Enter your password:");
const authData = await pb.collection("users").authWithPassword(username!, password!);
console.log(authData);
document.cookie = pb.authStore.exportToCookie({ httpOnly: false });
goHome();
}

function logout() {
pb.authStore.clear();
document.cookie = pb.authStore.exportToCookie({ httpOnly: false });
Expand All @@ -59,7 +68,7 @@
>{#if children}{@render children()}{:else}Logg ut{/if}</button
>
{:else}
<button onclick={login} class={rest.class || ""}
<button onclick={login} oncontextmenu={loginWithBasicAuth} class={rest.class || ""}
>{#if children}{@render children()}{:else}Logg inn{/if}</button
>
{/if}
50 changes: 50 additions & 0 deletions src/test/send-order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { test, expect } from "@playwright/test";

test("test", async ({ page, browserName }) => {
if (browserName === "webkit") {
test.skip();
}

const [username, password] = process.env.PB_ADMIN_BASIC!.split(":");
page.on("dialog", async (dialog) => {
if (dialog.type() === "prompt") {
if (dialog.message().includes("Enter your username:")) {
await dialog.accept(username);
} else if (dialog.message().includes("Enter your password:")) {
await dialog.accept(password);
} else {
await dialog.dismiss();
}
}
});

// log in
await page.goto("/");
await page.waitForLoadState("networkidle");
await page.getByRole("button", { name: "Logg inn", exact: true }).click({
button: "right"
});

await page.getByRole("link", { name: "Admin" }).click();
await page.getByRole("link", { name: "Bestillingsdisk" }).click();

// add mocca
await page.getByText("Mocca 10,-").click();
await page.getByRole("button", { name: "+" }).click();
await expect(page.getByRole("cell", { name: "Mocca" })).toBeVisible();
await expect(page.locator("tfoot")).toContainText("10,-");

// add hot chocolate
await page.getByText("Varm sjokolade").click();
await page.getByRole("button", { name: "+" }).click();
await expect(page.getByRole("cell", { name: "Varm sjokolade" })).toBeVisible();
await expect(page.locator("tfoot")).toContainText("20,-");

// finish order
await page.getByRole("button", { name: "Ferdig" }).click();
await expect(page.getByRole("cell", { name: "Ingenting" })).toBeVisible();

// logout
await page.getByText("Tilbake").click();
await page.getByRole("button", { name: "Logg ut" }).click();
});
Loading