|
| 1 | +// @ts-check |
| 2 | +import { test, expect, _electron as electron } from '@playwright/test'; |
| 3 | +import { last as _last } from 'lodash'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +const wait = (ms = 2000) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 7 | + |
| 8 | +/** |
| 9 | + * Gets the last element of array. |
| 10 | + * @template T |
| 11 | + * @param {T[] | null | undefined} arr |
| 12 | + * @returns {T} |
| 13 | + */ |
| 14 | +const last = (arr) => { |
| 15 | + const lastElement = _last(arr); |
| 16 | + expect(lastElement).not.toBeUndefined(); |
| 17 | + // @ts-ignore |
| 18 | + return lastElement; |
| 19 | +}; |
| 20 | + |
| 21 | +test.describe('e2e', () => { |
| 22 | + /** @type { import('@playwright/test').ElectronApplication } */ |
| 23 | + let electronApp; |
| 24 | + |
| 25 | + test.beforeEach(async () => { |
| 26 | + electronApp = await electron.launch({ args: ['start.js'], cwd: path.join(__dirname, '../..') }); |
| 27 | + }); |
| 28 | + |
| 29 | + test('loadingView option is working', async () => { |
| 30 | + const window = await electronApp.firstWindow(); |
| 31 | + |
| 32 | + expect(await window.title()).toBe('Loading'); |
| 33 | + expect(window.url()).toMatch(/renderer\/loading\.html$/); |
| 34 | + }); |
| 35 | + |
| 36 | + test('new window can be correctly created', async () => { |
| 37 | + await wait(); |
| 38 | + const window = await electronApp.firstWindow(); |
| 39 | + await window.click('button#new'); |
| 40 | + await wait(); |
| 41 | + const windows = electronApp.windows(); |
| 42 | + |
| 43 | + expect(windows.length).toBeGreaterThan(1); |
| 44 | + expect(last(windows).url()).toMatch(/renderer\/window\.html$/); |
| 45 | + }); |
| 46 | + |
| 47 | + test('new online window can be correctly created', async () => { |
| 48 | + await wait(); |
| 49 | + const window = await electronApp.firstWindow(); |
| 50 | + await window.click('button#new-online'); |
| 51 | + await wait(); |
| 52 | + const windows = electronApp.windows(); |
| 53 | + |
| 54 | + expect(windows.length).toBeGreaterThan(1); |
| 55 | + expect(last(windows).url()).toMatch(/https(.+)?github\.com/); |
| 56 | + }); |
| 57 | + |
| 58 | + test('window can be correctly closed', async () => { |
| 59 | + await wait(); |
| 60 | + const window = await electronApp.firstWindow(); |
| 61 | + await window.click('button#new'); |
| 62 | + await wait(); |
| 63 | + const newWindow = last(electronApp.windows()); |
| 64 | + await newWindow.click('button#close'); |
| 65 | + await wait(); |
| 66 | + const windows = electronApp.windows(); |
| 67 | + |
| 68 | + expect(windows).toHaveLength(1); |
| 69 | + expect(await windows[0].title()).toBe('main'); |
| 70 | + }); |
| 71 | + |
| 72 | + test.afterEach(async () => { |
| 73 | + await electronApp.close(); |
| 74 | + // @ts-ignore |
| 75 | + electronApp = null; |
| 76 | + }); |
| 77 | +}); |
0 commit comments