diff --git a/README.md b/README.md index 6fbf300..433bebe 100644 --- a/README.md +++ b/README.md @@ -121,10 +121,6 @@ throwing an error saying the execution context has been destroyed. This function retries the evaluation several times to see if it can run the evaluation without an error. If it fails after the retries, it throws the error.
-Returns the BrowserWindow object that corresponds to the given Playwright page (with retries).
-This is basically a wrapper around [app.browserWindow(page)](https://playwright.dev/docs/api/class-electronapplication#electron-application-browser-window)
-that retries the operation.
Promise.<T>
Retries a given function until it returns a truthy value or the timeout is reached.
This offers similar functionality to Playwright's page.waitForFunction()
@@ -340,28 +336,6 @@ it throws the error.
the number of times to retry the evaluation
| | retryIntervalMs | |the interval between retries
| - - -## browserWindowWithRetry(app, page, options) ⇒ -Returns the BrowserWindow object that corresponds to the given Playwright page (with retries).
-This is basically a wrapper around [app.browserWindow(page)](https://playwright.dev/docs/api/class-electronapplication#electron-application-browser-window)
-that retries the operation.
A promise that resolves to the browser window.
-**Throws**: - --Will throw an error if all retry attempts fail.
- - -| Param | Description | -| --- | --- | -| app |The Electron application instance.
| -| page |The Playwright page instance.
| -| options |Optional configuration for retries.
| -| options.retries |The number of retry attempts. Defaults to 5.
| -| options.intervalMs |The interval between retries in milliseconds. Defaults to 200.
| - ## retryUntilTruthy(fn, [timeoutMs], [intervalMs]) ⇒Promise.<T>
@@ -820,8 +794,8 @@ This function retries a given function until it returns without throwing one of
| --- | --- | --- | --- |
| fn | function
| | The function to retry.
| | [options] |RetryOptions
| {}
| The options for retrying the function.
| -| [options.intervalMs] |number
| 200
| The delay between each retry attempt in milliseconds.
| -| [options.timeoutMs] |number
| 5000
| The maximum time to wait before giving up in milliseconds.
| +| [options.timeout] |number
| 5000
| The maximum time to wait before giving up in milliseconds.
| +| [options.poll] |number
| 200
| The delay between each retry attempt in milliseconds.
| | [options.errorMatch] |string
\| Array.<string>
\| RegExp
| "['context or browser has been closed', 'Promise was collected', 'Execution context was destroyed']"
| String(s) or regex to match against error message. If the error does not match, it will throw immediately. If it does match, it will retry.
| **Example** diff --git a/example-project/e2e-tests/e2e.spec.ts b/example-project/e2e-tests/e2e.spec.ts index bb775e4..590ff1b 100644 --- a/example-project/e2e-tests/e2e.spec.ts +++ b/example-project/e2e-tests/e2e.spec.ts @@ -26,6 +26,7 @@ import { ipcRendererInvoke, ipcRendererSend, parseElectronApp, + retryUntilTruthy, stubDialog, waitForMenuItemStatus, } from '../../src' // <-- replace with 'electron-playwright-helpers' @@ -436,3 +437,49 @@ test('dialog.showSaveDialog stubbing', async () => { const filePath2 = await ipcMainInvokeHandler(app, 'get-opened-file') expect(filePath2).toBe('/path/to/new-saved-file.txt') }) + +test.describe('retryUntilTruthy()', () => { + test('retryUntilTruthy() returns true', async () => { + const page = latestPage() + if (!page) { + throw new Error('No page found') + } + const result = await retryUntilTruthy(() => + page.evaluate(() => document.getElementById('new-window')) + ) + expect(result).toBeTruthy() + }) + + test('retryUntilTruthy() timeout when returning false', async () => { + const page = latestPage() + if (!page) { + throw new Error('No page found') + } + await expect( + retryUntilTruthy( + () => + page.evaluate(() => document.getElementById('non-existent-element')), + { timeout: 500 } + ) + ).rejects.toThrow('Timeout after 500ms') + }) + + test('retryUntilTruthy() return truthy after a few iterations', async () => { + const page = latestPage() + if (!page) { + throw new Error('No page found') + } + await expect( + retryUntilTruthy(() => + page.evaluate(() => { + const w = window as Window & { counter?: number } + if (!w.counter) { + w.counter = 0 + } + w.counter++ + return w.counter > 3 + }) + ) + ).resolves.toBeTruthy() + }) +}) diff --git a/src/general_helpers.ts b/src/general_helpers.ts index 106620a..9556475 100644 --- a/src/general_helpers.ts +++ b/src/general_helpers.ts @@ -1,4 +1,4 @@ -import type { ElectronApplication, JSHandle, Page } from 'playwright-core' +import type { ElectronApplication } from 'playwright-core' import type { PageFunctionOn } from 'playwright-core/types/structs' import { retry, RetryOptions } from './utilities' @@ -19,7 +19,7 @@ export async function electronWaitForFunction(
electronApp: ElectronApplication,
property: P,
value: MenuItemPartial[P],
- options: RetryOptions = {}
+ options: Partial (
}
await mI.click()
}, menuItem.commandId),
- options
+ { disable: true, ...options }
)
}
@@ -114,7 +114,7 @@ export function getMenuItemAttribute