Skip to content

Commit

Permalink
Merge pull request #36 from ChainSafe/willem/integration-testing-fram…
Browse files Browse the repository at this point in the history
…ework

Add e2e testing framework for WebZjs
  • Loading branch information
willemolding authored Oct 16, 2024
2 parents d00cec4 + 74cfcef commit ab44d54
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 125 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Playwright Tests
on:
pull_request:
push:
branches: main

jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Just
uses: extractions/setup-just@v2

- name: install wasm-pack
uses: jetli/[email protected]
with:
version: latest

- name: build pkg
run: just build

- uses: actions/setup-node@v4
with:
node-version: lts/*

- uses: pnpm/action-setup@v4

- name: Install dependencies
run: pnpm i

- name: Install Playwright Browsers
working-directory: ./packages/e2e-tests/
run: npx playwright install --with-deps

- name: Run Playwright tests
run: pnpm run test:e2e
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"preinstall": "npx only-allow pnpm",
"build": "parcel build --no-cache --no-autoinstall packages/demo-wallet",
"serve": "pnpm --filter ./packages/demo-wallet run serve",
"start:dev": "parcel --no-autoinstall packages/demo-wallet"
"start:dev": "parcel --no-autoinstall packages/demo-wallet",
"test:e2e": "pnpm --filter ./packages/e2e-tests test"
},
"dependencies": {
"@webzjs/webz-core": "workspace:^",
Expand Down
110 changes: 0 additions & 110 deletions packages/demo-wallet/tsconfig.json

This file was deleted.

6 changes: 6 additions & 0 deletions packages/e2e-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
!dist/serve.json
23 changes: 23 additions & 0 deletions packages/e2e-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# WebZ e2e Tests

This package uses playwright to test the highest level API of WebZjs running in a browser. This allows testing the interaction of different WebWorkers with the page, as well as testing on different browsers.

## Writing Tests

New tests should be added as files named `.spec.ts` inside the `e2e` directory.

Tests should use the `page.evaluate` method from playwright to execute javascript code in the browser page where the wallet exists and return a result to the test runner to check. e.g.

```typescript
test('Test some webz functionality..', async ({ page }) => {
// code here runs in the test runner (node), not in the browser
let result = await page.evaluate(async () => {
// everything here executes in the web page
// - do something with window.webWallet..
return // the result to the test runner
});
expect(result).toBe(something);
});
```

The provided page has already initialized the Wasm environment and created a web wallet accessible in tests as `window.webWallet`
18 changes: 18 additions & 0 deletions packages/e2e-tests/dist/serve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"headers": [
{
"source": "**/*",
"headers": [
{
"key": "Cross-Origin-Opener-Policy",
"value": "same-origin"
},
{
"key": "Cross-Origin-Embedder-Policy",
"value": "require-corp"
}
]
}
]
}

25 changes: 25 additions & 0 deletions packages/e2e-tests/e2e/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test, expect } from '@playwright/test';
import { WebWallet } from "@webzjs/webz-core";

declare global {
interface Window { webWallet: WebWallet; }
}

const SEED = "mix sample clay sweet planet lava giraffe hand fashion switch away pool rookie earth purity truly square trumpet goose move actor save jaguar volume";
const BIRTHDAY = 2657762;

test.beforeEach(async ({ page }) => {
await page.goto("http://127.0.0.1:8081");
await page.waitForFunction(() => window.webWallet !== undefined);
await page.evaluate(async ({seed, birthday}) => {
await window.webWallet.create_account(seed, 0, birthday);
}, { seed: SEED, birthday: BIRTHDAY });
});

test('Account was added', async ({ page }) => {
let result = await page.evaluate(async () => {
let summary = await window.webWallet.get_wallet_summary();
return summary?.account_balances.length;
});
expect(result).toBe(1);
});
19 changes: 19 additions & 0 deletions packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "e2e-tests",
"version": "1.0.0",
"description": "",
"source": "src/index.html",
"scripts": {
"pretest": "parcel build",
"test": "playwright test"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.47.2",
"@types/node": "^22.7.4",
"@webzjs/webz-core": "workspace:^",
"serve": "^14.2.3"
}
}
79 changes: 79 additions & 0 deletions packages/e2e-tests/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './e2e',
/* 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://127.0.0.1:3000',

/* 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: 'serve -l 8081 ./dist -c ./serve.json',
url: 'http://127.0.0.1:8081',
// reuseExistingServer: !process.env.CI,
},
});
11 changes: 11 additions & 0 deletions packages/e2e-tests/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebZjs e2e testing</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="index.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions packages/e2e-tests/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import initWasm, { initThreadPool, WebWallet } from "@webzjs/webz-core";

const N_THREADS = 10;
const MAINNET_LIGHTWALLETD_PROXY = "https://zcash-mainnet.chainsafe.dev";

async function loadPage() {
await new Promise((resolve) => {
window.addEventListener("load", resolve);
});

// Code to executed once the page has loaded
await initWasm();
await initThreadPool(N_THREADS);
window.webWallet = new WebWallet(
"main",
MAINNET_LIGHTWALLETD_PROXY,
1
);
console.log("WebWallet initialized");
console.log(webWallet);
}

loadPage();
Loading

0 comments on commit ab44d54

Please sign in to comment.