Below is an example using Playwright’s test hooks in plain JavaScript. In this example, any device added in a test is recorded in the deviceNames set. Then in an afterAll hook, any leftover devices are removed by calling the declarative devicesPage.deleteDevice(name) method.
const { test } = require('@playwright/test');
/* A Set to store the names of any devices that were added during tests */
const deviceNames = new Set();
/* Example page object for devices. Assume that methods such as
searchDevice, disableGridSearch, etc. are implemented accordingly.
*/
class DevicesPage {
constructor(page) {
this.page = page;
this.buttons = {
delete: page.locator('css=[data-test=delete-button]'),
confirmDelete: page.locator('css=[data-test=confirm-delete-button]')
};
}
async searchDevice(deviceName) {
// Implementation to search for the device by name.
await this.page.locator(`input[placeholder="Search devices"]`).fill(deviceName);
await this.page.keyboard.press('Enter');
await this.page.waitForLoadState('networkidle');
}
getDeviceNameConfirmInput(deviceName) {
// Returns the locator for the device name confirm input.
return this.page.locator(`input[placeholder="Type ${deviceName}"]`);
}
async disableGridSearch() {
// Implementation that optionally disables grid search.
// Could be used to reset filters after deletion.
}
/**
* Deletes a device by name.
* @param {string} deviceName - The name of the device to be deleted.
*/
async deleteDevice(deviceName) {
await this.searchDevice(deviceName);
await this.buttons.delete.click();
await this.getDeviceNameConfirmInput(deviceName).fill(deviceName);
await this.buttons.confirmDelete.click();
await waitForPageLoad(this.page);
await this.disableGridSearch();
}
}
async function waitForPageLoad(page) {
// Use Playwright's API to wait for the page to finish loading
await page.waitForLoadState('load');
}
/* Define the tests in a describe block */
test.describe('Device tests', () => {
// afterAll hook to cleanup any leftover devices regardless of test success or failure.
test.afterAll(async ({ page }) => {
const devicesPage = new DevicesPage(page);
for (const name of deviceNames) {
console.log(`Removing leftover device: ${name}`);
await devicesPage.deleteDevice(name);
}
});
test('Add a device and record its name for cleanup', async ({ page }) => {
const devicesPage = new DevicesPage(page);
const deviceName = 'Test Device 1';
// ... code to add a new device (e.g., fill out a form, click submit) ...
// Record the name for later cleanup.
deviceNames.add(deviceName);
// Expectation/assertions here.
// e.g., await expect(page.locator('css=[data-test=device-name]')).toHaveText(deviceName);
});
test('Another test that may add a device', async ({ page }) => {
const devicesPage = new DevicesPage(page);
const deviceName = 'Test Device 2';
// ... code to add a new device ...
// Record for cleanup.
deviceNames.add(deviceName);
// Additional assertions.
});
});
How It Works
-
Recording Devices:
Each test that adds a device also adds the device name to the deviceNames set. This way, even if a test fails before cleanup, the device name is still recorded.
-
Cleanup Using afterAll:
The test.afterAll hook runs after all tests in the block. It iterates over the deviceNames set and calls the declarative deleteDevice(deviceName) method from the DevicesPage object. This uses Playwright’s API (e.g., locators, waitForLoadState) and ensures that any leftover devices are removed.
-
Declarative Style & Playwright API:
The code is written in a declarative manner by describing what should be done (e.g., “after all tests, delete these devices”), relying on Playwright’s test hooks and its API rather than for example, raw Node.js file system or network calls.
This approach meets your requirements by using plain JavaScript (no TypeScript), the latest JavaScript features (e.g., async/await), and fully leveraging Playwright’s APIs and declarative test syntax.