feat: let CheckboxTester and SwitchTester set a state, not just toggle - #185
Conversation
CheckboxTester and SwitchTester exposed only click(), so putting a checkbox or switch into a known state meant reading the component value first. Add CheckboxTester.setChecked(boolean)/isChecked() and SwitchTester.setOn(boolean), mirroring TestBench's CheckboxElement. Setting the state routes through click(), so the value change is seen as coming from the client, and is a no-op when the component already is in the requested state. The usability guard runs in either case, so acting on a disabled or read-only component still throws. Fixes #177
|
The change looks good, but |
setChecked(boolean)/setOn(boolean) read like a forced value set. Rename them to the gesture the user performs: CheckboxTester.check()/uncheck() and SwitchTester.switchOn()/switchOff(), matching the imperative naming the other testers already use (openDetails(), closeDetails(), select()). Behaviour is unchanged: each one clicks only when the component is not already in the target state, and the usability guard runs either way.
switchOff() promises to do nothing when the switch is already off, and uncheck() is declared on the tester so the generated locator picks it up - neither branch was exercised.
|
@mcollovati Agreed —
This also lines up with the imperative naming the other testers already use ( Behaviour is unchanged: each method clicks only when the component is not already in the target state, and the usability guard runs either way, so a disabled or read-only component still throws even on the no-op path. Javadoc now says why the no-op happens ("a user would not click it in that case") rather than describing it as setting a state. Full build is green. |
Summary
CheckboxTesterandSwitchTestercould onlyclick(), which flips the value. This addscheck()/uncheck()andswitchOn()/switchOff(), so a test can put the component into the state it needs without first asking what the state is.What changed
CheckboxTestergainscheck(),uncheck()andisChecked().SwitchTestergainsswitchOn()andswitchOff()(it already hadisOn()).click().IllegalStateException, even when the call would otherwise do nothing.CheckboxLocatorandSwitchLocator.This change is purely additive — no existing method changed its behaviour, so nothing breaks.
Use case
You test a signup view with an "I accept the terms" checkbox and a settings view with a "dark mode" switch. You want the test to say what the user did ("accept the terms"), and you do not want it to break if the default value of the field changes later — a plain
click()would then flip the value the wrong way.API Changes
com.vaadin.flow.component.checkbox.CheckboxTester
com.vaadin.flow.component.checkbox.SwitchTester
Test summary
check()/uncheck()reach the wanted state, fire exactly one value change withisFromClient() == true, and fire nothing when already in that stateswitchOn()/switchOff()behave the same way onSwitchIllegalStateException, even when the call is a no-opisChecked()matches the checkbox value before and after a clickcheck(),uncheck()andisChecked()work through the generatedCheckboxLocatorCheckboxTesterTest.checkAndUncheck_onlyClickWhenNeeded— row 1SwitchTesterTest.switchOnAndOff_onlyClickWhenNeeded— row 2CheckboxTesterTest.check_notUsableAndAlreadyChecked_throws,SwitchTesterTest.switchOn_notUsableAndAlreadyOn_throws— row 3CheckboxTesterTest.isChecked_reflectsValue— row 4LocatorApiTest.checkbox_checkUncheckAndIsChecked_reachableFromLocator— row 5Not tested on purpose:
switchOn()/switchOff()through the generatedSwitchLocator. It is the same generated delegation that row 5 already pins for the checkbox.