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

examples: Add e2e testing with dAppwright #450

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
79 changes: 79 additions & 0 deletions .github/workflows/ci-playwright.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: playwright-test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
playwright-test:
runs-on: ubuntu-latest
services:
sapphire-localnet-ci:
image: ghcr.io/oasisprotocol/sapphire-localnet:latest
ports:
- 8545:8545
- 8546:8546
env:
OASIS_DEPOSIT_BINARY: /oasis-deposit -test-mnemonic -n 5
options: >-
--rm
--health-cmd="test -f /CONTAINER_READY"
--health-start-period=90s
env:
SAPPHIRE_LOCALNET_HTTP_PROXY_PORT: 3001
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- uses: pnpm/action-setup@v4
name: Install pnpm
id: pnpm-install
with:
version: 8
run_install: false
- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install
- name: Build JS client
run: make -C clients/js build
- name: Build Integrations
run: make -C integrations build
- uses: JarvusInnovations/background-action@v1
name: RPC proxy will error if non-encrypted calls are made
with:
run: pnpm run proxy &
wait-on: http://127.0.0.1:${{ env.SAPPHIRE_LOCALNET_HTTP_PROXY_PORT }}
tail: true
log-output-resume: true
wait-for: 31sec
log-output: true
log-output-if: true
working-directory: clients/js
- name: Install Playwright dependencies
run: pnpm test:setup
working-directory: examples/wagmi-v2
- name: Run playwright tests (with xvfb-run to support headed extension test)
working-directory: examples/wagmi-v2
run: xvfb-run pnpm test
- name: Upload playwright test-results
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: playwright-test-results
path: examples/wagmi-v2/test-results
retention-days: 5
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![ci-lint](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-lint.yaml/badge.svg)](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-lint.yaml)
[![ci-test](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-test.yaml/badge.svg)](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-test.yaml)
[![ci-test](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/contracts-test.yaml/badge.svg)](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/contracts-test.yaml)
[![ci-test](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-playwright.yaml/badge.svg)](https://github.com/oasisprotocol/sapphire-paratime/actions/workflows/ci-playwright.yaml)

The Sapphire ParaTime is the official confidential EVM Compatible ParaTime
providing a smart contract development environment with EVM compatibility
Expand Down
2 changes: 2 additions & 0 deletions examples/wagmi-v2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ node_modules
dist
dist-ssr
*.local
playwright-report
test-results

# Editor directories and files
.vscode/*
Expand Down
6 changes: 5 additions & 1 deletion examples/wagmi-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
"lint": "biome check .",
"format": "biome format --write .",
"preview": "vite preview",
"test": "playwright test",
"test:setup": "playwright install --with-deps",
"start:server": "vite --port 3000"
},
"dependencies": {
"@oasisprotocol/sapphire-wagmi-v2": "workspace:^",
"@oasisprotocol/sapphire-paratime": "workspace:^",
"@oasisprotocol/sapphire-wagmi-v2": "workspace:^",
"@playwright/test": "^1.48.2",
"@tanstack/react-query": "5.0.5",
"@tenkeylabs/dappwright": "^2.8.6",
"abitype": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
46 changes: 46 additions & 0 deletions examples/wagmi-v2/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { defineConfig, devices } from "@playwright/test";

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
timeout: 2 * 60 * 1000,
testDir: "./test/",
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 ? 1 : 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"], ["list", { printSteps: true }]],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
baseURL: process.env.FRONTEND_URL || "http://localhost:3000/",
trace: "on-first-retry",
headless: false,
screenshot: {
mode: "only-on-failure",
fullPage: true,
},
},
/* Configure projects for major browsers */
projects: [
{
name: 'main',
testDir: './test/',
use: {
...devices['Desktop Chrome'],
},
},
],
/* Run your local dev server before starting the tests */
webServer: {
command: 'pnpm run start:server',
url: process.env.FRONTEND_URL || "http://localhost:3000/",
reuseExistingServer: !process.env.CI,
stdout: 'pipe',
stderr: 'pipe',
},
});
4 changes: 2 additions & 2 deletions examples/wagmi-v2/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function App() {
</span>
<br />
Write Tx Calldata:&nbsp;
<span id="isWriteEnveloped">
<span id="isWriteEnveloped" data-testid="is-write-enveloped">
{isCalldataEnveloped(writeTxInfo?.input) ? 'encrypted' : 'plaintext'}
</span>
</>
Expand All @@ -199,7 +199,7 @@ function App() {
</button>
{readResult !== undefined && (
<>
<span id="readResult">{readResult.toString()}</span>
<span id="readResult" data-testid="read-result">{readResult.toString()}</span>
</>
)}
<br />
Expand Down
60 changes: 60 additions & 0 deletions examples/wagmi-v2/test/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { BrowserContext, expect, test as baseTest } from "@playwright/test";
import dappwright, { Dappwright, MetaMaskWallet } from "@tenkeylabs/dappwright";

export const test = baseTest.extend<{
context: BrowserContext;
wallet: Dappwright;
}>({
context: async ({}, use) => {
// Launch context with extension
const [wallet, _, context] = await dappwright.bootstrap("", {
wallet: "metamask",
CedarMist marked this conversation as resolved.
Show resolved Hide resolved
version: MetaMaskWallet.recommendedVersion,
seed: "test test test test test test test test test test test junk", // Hardhat's default https://hardhat.org/hardhat-network/docs/reference#accounts
headless: false,
});

// Add Sapphire Localnet as a custom network
await wallet.addNetwork({
networkName: "Sapphire Localnet",
rpc: "http://localhost:8545",
chainId: 23293,
symbol: "ROSE",
});

await use(context);
},

wallet: async ({ context }, use) => {
const metamask = await dappwright.getWallet("metamask", context);

await use(metamask);
},
});

test.beforeEach(async ({ wallet, page }) => {
aefhm marked this conversation as resolved.
Show resolved Hide resolved
// Use first account from seed. dAppwright adds two accounts by default.
await wallet.switchAccount(1); // Selector queries as a string
await page.bringToFront();
});

test("deploy contract and send encrypted transaction", async ({ wallet, page }) => {
await page.goto("http://localhost:3000");

// Use address of first account
await page.getByText("Injected (Sapphire)").click();
await wallet.approve();
await expect(page.getByText("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")).toBeVisible();
aefhm marked this conversation as resolved.
Show resolved Hide resolved

await page.getByText("Deploy").click();
await wallet.confirmTransaction();
await expect(page.getByText("Contract:")).toBeVisible();

await page.getByText("Write").click();
await wallet.confirmTransaction();
await expect(page.getByText("Contract:")).toBeVisible();
await expect(page.getByTestId('is-write-enveloped')).toHaveText('encrypted');

await page.getByText("Read").click();
await expect(page.getByTestId("read-result")).not.toBeEmpty();
});
Loading
Loading