-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Switch to Playwright for automated tests #133
Comments
microsoft/playwright#2632 (comment) shows how to activate menu items. |
This script worked, saved as const { _electron: electron } = require('playwright');
(async () => {
const electronApp = await electron.launch({ args: ['main.js'] });
// Evaluation expression in the Electron context.
const appPath = await electronApp.evaluate(async ({ app }) => {
// This runs in the main Electron process, parameter here is always
// the result of the require('electron') in the main app script.
return app.getAppPath();
});
console.log(appPath);
// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
console.log('Window title:', await window.title());
await window.screenshot({ path: 'test-loading.png' });
// Direct Electron console to Node terminal.
window.on('console', console.log);
// Wait for "Run SQL" link to appear
await window.waitForSelector('#run-sql-link');
console.log('Window title:', await window.title());
await window.screenshot({ path: 'test-homepage.png' });
await electronApp.close();
})(); |
I'm going to try using Mocha for that, and running it with |
CI failed: https://github.com/simonw/datasette-app/runs/7309831752?check_suite_focus=true
|
https://github.com/microsoft/playwright-github-action suggests:
But I'm testing an Electron app, so Electron should have installed a browser already I think. |
This demo repo looks useful: https://github.com/tanshuai/electron-playwright-e2e-test-quick-start Relevant bits from https://github.com/tanshuai/electron-playwright-e2e-test-quick-start/blob/1e2c653bc2d0af85d98de1c58e56a888f17fe671/package.json {
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "xvfb-maybe -- playwright test"
},
"devDependencies": {
"@playwright/test": "^1.16.3",
"electron": "^15.3.1",
"playwright": "^1.16.3",
"xvfb-maybe": "^0.2.1"
}
}
Here's the commit that added |
On my Mac:
The tests passed for me locally.
|
Ran this to add it as a dev dependency:
I'll call it with |
Failed with the same error. Maybe I need to bump up the timeout for this test: Lines 14 to 17 in 326150b
It's waiting for all of the stuff to install on the loading screen, maybe that takes longer than 10s in CI sometimes? |
https://playwright.dev/docs/api/class-page#page-wait-for-selector says the default timeout is 30s, not 10s. I think this is a Mocha timeout. Can be changed with |
This example doesn't use Mocha, it uses Playwright test directly: https://github.com/spaceagetv/electron-playwright-example/blob/master/e2e-tests/main.spec.ts |
Failed again, this time with the 30s timeout:
https://github.com/simonw/datasette-app/runs/7310066151?check_suite_focus=true |
https://playwright.dev/docs/intro#installation says I need to do this for
|
I had to rename |
Same failure again: https://github.com/simonw/datasette-app/runs/7310434199?check_suite_focus=true
|
I'd like to get it to record a video. Here's a clue: https://github.com/microsoft/playwright/pull/10810/files#diff-25a018cac50f8f5e3a9f2a7691a61391be2e5995e74d9cc03a058be43163a795 const app = await playwright._electron.launch({
args: [path.join(__dirname, 'electron-window-app.js')],
recordVideo: { dir: testInfo.outputPath('video') }
}); |
That didn't seem to work: https://github.com/simonw/datasette-app/actions/runs/2659525079 |
I tried running
I wonder if that's because the second time you run them they take less time, because the Python virtual environment has already been setup? |
Also, the run that I used Here's the first video, which looks like it didn't install everything within the time limit. I converted it like this:
bc74c2a51bd91fe6f6cb815e6b99b6c7.mp4And a subsequent video showing it was much faster the second time: a1f192a041f93158bba4e02d75139dcd.mp4I'll try bumping up the time limit again. |
I'm dropping |
I'm going to see if I can retry the failed test once, using this pattern: https://github.community/t/how-to-retry-a-failed-step-in-github-actions-workflow/125880/2 |
The tests passed using that trick! Wish I knew why. |
I downloaded and installed the build from https://github.com/simonw/datasette-app/runs/7310850328?check_suite_focus=true - it worked! I ran |
I'm going to bump the time limit up to 90s and see if that means I don't need to retry the test. |
It timed out at 30s, there must be another timeout being applied. |
Tests passed after 57s. I'm going to leave the test retry in though, mainly as it's a useful pattern to have recorded somewhere. |
Wrote this up as a TIL: https://til.simonwillison.net/electron/testing-electron-playwright |
It looks like Playwright might be a better option for automated UI testing than Spectron.
https://playwright.dev/docs/api/class-electron/ has some details.
https://github.com/spaceagetv/electron-playwright-example are really good examples.
Tip via Reddit: https://www.reddit.com/r/electronjs/comments/qfn98f/comment/hi4uvj0/
The text was updated successfully, but these errors were encountered: