javascripttypescriptcheckboxplaywright

checkbox Playwright - check() vs setChecked(true)


According to Playwright Docs, there are two main methods for handling with checkbox elements.

I'm trying to understand what is the difference between these methods - locator.check and locator.setChecked when it set to true.

// check()
await page.getByRole('checkbox').check();

// VS

// setChecked(true)
await page.getByRole('checkbox').setChecked(true);

// (same for `uncheck()` vs `setChecked(false)`)

I've checked the docs and it looks like they're basically the same.

I've checked their functionality and it seems like they do the same.

Is there any difference? maybe a different purpose?


Solution

  • locator.check

    enter image description here

    locator.setCheck

    enter image description here

    So, judging by the documentation, locator.check will throw if the element is detached from DOM at any moment during action, whereas locator.setCheck will retry in that scenario.