Lesson 18 of 28
javascriptTesting Electron Apps – Spectron & Playwright
11 mins
Course progress64%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 18 of 28
javascript11 mins
Keep these ideas in mind as you work through the lesson.
npm install --save-dev spectron mocha
const { Application } = require('spectron');
const assert = require('assert');
describe('App launch', function(){
this.timeout(10000);
let app;
beforeEach(()=>{ app = new Application({ path: require('electron'), args: ['.'] }); return app.start(); });
afterEach(()=> app && app.stop());
it('shows a window', async()=>{ const count = await app.client.getWindowCount(); assert.equal(count, 1); });
});
npm install --save-dev @playwright/test electron
// tests/electron.test.js
const { test, expect } = require('@playwright/test');
const { _electron: electron } = require('playwright');
test('app has title', async () => {
const app = await electron.launch({ args: ['.'] });
const window = await app.firstWindow();
await expect(window).toHaveTitle(/MyApp/);
});
Playwright can also record videos and run headless CI.
Use any Node test runner (Jest, Mocha) because the main process is just a Node script.
// main.test.js
const { app } = require('electron');
test('app is not null', () => { expect(app).toBeDefined(); });