← Back to library
Playwrightformsbeginnerv1.20+

Check and uncheck a checkbox

Toggle a checkbox on and off, asserting state after each interaction.

1test('checkbox toggles correctly', async ({ page }) => {
2  await page.goto('/preferences');
3
4  const checkbox = page.getByRole('checkbox', { name: 'Email notifications' });
5
6  await expect(checkbox).not.toBeChecked();
7
8  await checkbox.check();
9  await expect(checkbox).toBeChecked();
10
11  await checkbox.uncheck();
12  await expect(checkbox).not.toBeChecked();
13});
#checkbox#check#uncheck#isChecked