Checkboxes | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Mastering UI Elements
Instructor: Artem Bondar
Lesson Summary
In this lesson, we will explore how to automate checkboxes using Playwright, including how to select, unselect, and perform assertions on checkbox selections. Key Concepts Checkbox Interaction: Learn to interact with checkboxes using Playwright's methods. Locator Selection: Use locators to find checkboxes by their roles and names. Methods: check : Checks the checkbox if it is not already checked. uncheck : Unchecks the checkbox if it is checked. click : Performs a click action without checking the current status. Example Implementation To interact with a checkbox: const checkbox = page.getByRole('checkbox', { name: 'Checkbox Name' }); await checkbox.check(); // Check the checkbox await checkbox.uncheck(); // Uncheck the checkbox Selecting All Checkboxes To select or unselect all checkboxes on a page: const allBoxes = page.getByRole('checkbox'); const boxesArray = await allBoxes.all(); // Convert to array for (const box of boxesArray) { await box.check(); // Check each checkbox expect(await box.isChecked()).toBeTruthy(); // Validate checked status } Summary Use check and uncheck methods for reliable checkbox status management, while click performs a simple action without status validation. Always convert locators to an array for looping through multiple elements. In conclusion, understanding these methods will enhance your ability to automate checkbox interactions effectively in your tests.