Skip to content

Commit

Permalink
add e2e tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Feb 23, 2025
1 parent 694e1ec commit f82ffa8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ jobs:
fcitx5-js.tgz
fcitx5-js-dev.tar.bz2
- name: Test
run: |
npx playwright install
npx playwright install-deps
pnpm run test
- name: Setup tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
pnpm-lock.yaml
preview/index.js
preview/index.js.map
test-results
10 changes: 9 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{
"type": "shell",
"label": "Preview",
"command": "npx serve -l 9000 -S preview",
"command": "pnpm run preview",
"group": {
"kind": "build"
}
Expand Down Expand Up @@ -70,6 +70,14 @@
"group": {
"kind": "build"
}
},
{
"type": "shell",
"label": "Test",
"command": "pnpm run test",
"group": {
"kind": "build"
}
}
]
}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
"dist"
],
"scripts": {
"lint": "eslint page",
"lint:fix": "eslint page --fix",
"lint": "eslint page tests",
"lint:fix": "eslint page tests --fix",
"check": "tsc --noEmit",
"build": "node scripts/build.mjs",
"prepack": "bash scripts/prepack.sh",
"postpack": "bash scripts/postpack.sh"
"postpack": "bash scripts/postpack.sh",
"preview": "serve -l 9000 -S preview",
"test": "playwright test --browser all",
"test:chromium": "playwright test --browser chromium",
"test:firefox": "playwright test --browser firefox",
"test:webkit": "playwright test --browser webkit"
},
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@antfu/eslint-config": "^3.12.1",
"@playwright/test": "^1.50.1",
"@types/node": "^22.13.5",
"@types/textarea-caret": "^3.0.3",
"@types/uzip": "^0.20201231.2",
"error-stack-parser": "^2.1.4",
Expand Down
11 changes: 11 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from '@playwright/test';

export default defineConfig({
testDir: 'tests',
fullyParallel: true,
webServer: {
command: 'pnpm run preview',
port: 9000,
reuseExistingServer: true
}
})
2 changes: 1 addition & 1 deletion preview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<button>click me</button>
<script type="module">
import { fcitxReady } from "./Fcitx5.js"
window.fcitxReady = fcitxReady
await fcitxReady
window.fcitx.enable()
window.fcitx.setInputMethods(['pinyin'])
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions tests/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare global {
interface Window {
fcitxReady: Promise<void>
}
}

export {}
39 changes: 39 additions & 0 deletions tests/test-generic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test } from '@playwright/test'
import { init } from './util'

test('keyboard-us', async ({ page }) => {
const addons: string[] = []
page.on('console', (msg) => {
const match = msg.text().match(/Loaded addon (\S+)/)
if (match) {
addons.push(match[1])
}
})

await init(page)
expect(addons.sort()).toEqual(['clipboard', 'imselector', 'keyboard', 'quickphrase', 'unicode', 'wasmfrontend', 'webpanel'])

const textarea = page.locator('textarea')
await textarea.click()
await page.keyboard.press('q')
await expect(textarea).toHaveValue('q')

const input = page.locator('input')
await input.click()
await page.keyboard.press('w')
await expect(input).toHaveValue('w')
})

test('keyboard-th', async ({ page }) => {
await init(page)

await page.evaluate(async () => {
window.fcitx.setInputMethods(['keyboard-th'])
})
const textarea = page.locator('textarea')
await textarea.click()
for (const key of 'l;ylfu') {
await page.keyboard.press(key)
}
await expect(textarea).toHaveValue('สวัสดี')
})
10 changes: 10 additions & 0 deletions tests/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type {
Page,
} from '@playwright/test'

export async function init(page: Page) {
await page.goto('http://localhost:9000')
return page.evaluate(() => {
return window.fcitxReady
})
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"esModuleInterop": true,
"isolatedModules": true
},
"include": ["page/*.ts"]
"include": ["page/*.ts", "tests/*.ts"]
}

0 comments on commit f82ffa8

Please sign in to comment.