|
| 1 | +/* |
| 2 | + * Copyright 2026 Element Creations Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +import { expect, type Locator, type Page } from "@playwright/test"; |
| 9 | + |
| 10 | +import { test as base } from "./services.js"; |
| 11 | + |
| 12 | +// This fixture provides convenient handling of Element Web's toasts. |
| 13 | +export const test = base.extend<{ |
| 14 | + /** |
| 15 | + * Convenience functions for handling toasts. |
| 16 | + */ |
| 17 | + toasts: Toasts; |
| 18 | +}>({ |
| 19 | + toasts: async ({ page }, use) => { |
| 20 | + const toasts = new Toasts(page); |
| 21 | + await use(toasts); |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +class Toasts { |
| 26 | + public constructor(public readonly page: Page) {} |
| 27 | + |
| 28 | + /** |
| 29 | + * Assert that no toasts exist |
| 30 | + */ |
| 31 | + public async assertNoToasts(): Promise<void> { |
| 32 | + await expect(this.page.locator(".mx_Toast_toast")).not.toBeVisible(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Assert that a toast with the given title exists, and return it |
| 37 | + * |
| 38 | + * @param title - Expected title of the toast |
| 39 | + * @param timeout - Time to retry the assertion for in milliseconds. |
| 40 | + * Defaults to `timeout` in `TestConfig.expect`. |
| 41 | + * @returns the Locator for the matching toast |
| 42 | + */ |
| 43 | + public async getToast(title: string, timeout?: number): Promise<Locator> { |
| 44 | + const toast = this.getToastIfExists(title); |
| 45 | + await expect(toast).toBeVisible({ timeout }); |
| 46 | + return toast; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Find a toast with the given title, if it exists. |
| 51 | + * |
| 52 | + * @param title - Title of the toast. |
| 53 | + * @returns the Locator for the matching toast, or an empty locator if it |
| 54 | + * doesn't exist. |
| 55 | + */ |
| 56 | + public getToastIfExists(title: string): Locator { |
| 57 | + return this.page.locator(".mx_Toast_toast", { hasText: title }).first(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Accept a toast with the given title. Only works for the first toast in |
| 62 | + * the stack. |
| 63 | + * |
| 64 | + * @param title - Expected title of the toast |
| 65 | + */ |
| 66 | + public async acceptToast(title: string): Promise<void> { |
| 67 | + const toast = await this.getToast(title); |
| 68 | + await toast.locator('.mx_Toast_buttons button[data-kind="primary"]').click(); |
| 69 | + } |
| 70 | + /** |
| 71 | + * Accept a toast with the given title, if it exists. Only works for the |
| 72 | + * first toast in the stack. |
| 73 | + * |
| 74 | + * @param title - Title of the toast |
| 75 | + */ |
| 76 | + public async acceptToastIfExists(title: string): Promise<void> { |
| 77 | + const toast = this.getToastIfExists(title).locator('.mx_Toast_buttons button[data-kind="primary"]'); |
| 78 | + if ((await toast.count()) > 0) { |
| 79 | + await toast.click(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Reject a toast with the given title. Only works for the first toast in |
| 85 | + * the stack. |
| 86 | + * |
| 87 | + * @param title - Expected title of the toast |
| 88 | + */ |
| 89 | + public async rejectToast(title: string): Promise<void> { |
| 90 | + const toast = await this.getToast(title); |
| 91 | + await toast.locator('.mx_Toast_buttons button[data-kind="secondary"]').click(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Reject a toast with the given title, if it exists. Only works for the |
| 96 | + * first toast in the stack. |
| 97 | + * |
| 98 | + * @param title - Title of the toast |
| 99 | + */ |
| 100 | + public async rejectToastIfExists(title: string): Promise<void> { |
| 101 | + const toast = this.getToastIfExists(title).locator('.mx_Toast_buttons button[data-kind="secondary"]'); |
| 102 | + if ((await toast.count()) > 0) { |
| 103 | + await toast.click(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments