docs: update automated-testing.md (#47017)

* Update automated-testing.md

* fixed lint error
This commit is contained in:
Taiki Komoda 2025-06-30 17:56:48 +09:00 committed by GitHub
commit fa15332587
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -74,46 +74,22 @@ describe('keyboard input', () => {
Furthermore, WebdriverIO allows you to access Electron APIs to get static information about your application: Furthermore, WebdriverIO allows you to access Electron APIs to get static information about your application:
```js @ts-nocheck ```js @ts-nocheck
import { browser, $, expect } from '@wdio/globals' import { browser } from '@wdio/globals'
describe('when the make smaller button is clicked', () => { describe('trigger message modal', async () => {
it('should decrease the window height and width by 10 pixels', async () => { it('message modal can be triggered from a test', async () => {
const boundsBefore = await browser.electron.browserWindow('getBounds') await browser.electron.execute(
expect(boundsBefore.width).toEqual(210) (electron, param1, param2, param3) => {
expect(boundsBefore.height).toEqual(310) const appWindow = electron.BrowserWindow.getFocusedWindow()
electron.dialog.showMessageBox(appWindow, {
await $('.make-smaller').click() message: 'Hello World!',
const boundsAfter = await browser.electron.browserWindow('getBounds') detail: `${param1} + ${param2} + ${param3} = ${param1 + param2 + param3}`
expect(boundsAfter.width).toEqual(200)
expect(boundsAfter.height).toEqual(300)
}) })
}) },
``` 1,
2,
or to retrieve other Electron process information: 3
)
```js @ts-nocheck
import fs from 'node:fs'
import path from 'node:path'
import { browser, expect } from '@wdio/globals'
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' }))
const { name, version } = packageJson
describe('electron APIs', () => {
it('should retrieve app metadata through the electron API', async () => {
const appName = await browser.electron.app('getName')
expect(appName).toEqual(name)
const appVersion = await browser.electron.app('getVersion')
expect(appVersion).toEqual(version)
})
it('should pass args through to the launched application', async () => {
// custom args are set in the wdio.conf.js file as they need to be set before WDIO starts
const argv = await browser.electron.mainProcess('argv')
expect(argv).toContain('--foo')
expect(argv).toContain('--bar=baz')
}) })
}) })
``` ```
@ -206,7 +182,7 @@ npm install --save-dev @playwright/test
``` ```
:::caution Dependencies :::caution Dependencies
This tutorial was written with `@playwright/test@1.41.1`. Check out This tutorial was written with `@playwright/test@1.52.0`. Check out
[Playwright's releases][playwright-releases] page to learn about [Playwright's releases][playwright-releases] page to learn about
changes that might affect the code below. changes that might affect the code below.
::: :::
@ -218,10 +194,10 @@ To point this API to your Electron app, you can pass the path to your main proce
entry point (here, it is `main.js`). entry point (here, it is `main.js`).
```js {5} @ts-nocheck ```js {5} @ts-nocheck
const { test, _electron: electron } = require('@playwright/test') import { test, _electron as electron } from '@playwright/test'
test('launch app', async () => { test('launch app', async () => {
const electronApp = await electron.launch({ args: ['main.js'] }) const electronApp = await electron.launch({ args: ['.'] })
// close app // close app
await electronApp.close() await electronApp.close()
}) })
@ -231,10 +207,10 @@ After that, you will access to an instance of Playwright's `ElectronApp` class.
is a powerful class that has access to main process modules for example: is a powerful class that has access to main process modules for example:
```js {5-10} @ts-nocheck ```js {5-10} @ts-nocheck
const { test, _electron: electron } = require('@playwright/test') import { test, _electron as electron } from '@playwright/test'
test('get isPackaged', async () => { test('get isPackaged', async () => {
const electronApp = await electron.launch({ args: ['main.js'] }) const electronApp = await electron.launch({ args: ['.'] })
const isPackaged = await electronApp.evaluate(async ({ app }) => { const isPackaged = await electronApp.evaluate(async ({ app }) => {
// This runs in Electron's main process, parameter here is always // This runs in Electron's main process, parameter here is always
// the result of the require('electron') in the main app script. // the result of the require('electron') in the main app script.
@ -250,10 +226,10 @@ It can also create individual [Page][playwright-page] objects from Electron Brow
For example, to grab the first BrowserWindow and save a screenshot: For example, to grab the first BrowserWindow and save a screenshot:
```js {6-7} @ts-nocheck ```js {6-7} @ts-nocheck
const { test, _electron: electron } = require('@playwright/test') import { test, _electron as electron } from '@playwright/test'
test('save screenshot', async () => { test('save screenshot', async () => {
const electronApp = await electron.launch({ args: ['main.js'] }) const electronApp = await electron.launch({ args: ['.'] })
const window = await electronApp.firstWindow() const window = await electronApp.firstWindow()
await window.screenshot({ path: 'intro.png' }) await window.screenshot({ path: 'intro.png' })
// close app // close app
@ -265,7 +241,7 @@ Putting all this together using the Playwright test-runner, let's create a `exam
test file with a single test and assertion: test file with a single test and assertion:
```js title='example.spec.js' @ts-nocheck ```js title='example.spec.js' @ts-nocheck
const { test, expect, _electron: electron } = require('@playwright/test') import { test, expect, _electron as electron } from '@playwright/test'
test('example test', async () => { test('example test', async () => {
const electronApp = await electron.launch({ args: ['.'] }) const electronApp = await electron.launch({ args: ['.'] })