Java Selenium: Custom Assert Message for Multiple Checkbox



  • I'm working with an automated script for multiple checkboxes that has dependent permissions.

    Given there are multiple checkboxes

    • Checkbox A
    • Checkbox B
    • Checkbox C
    • Checkbox D
    • Checkbox E

    When I select Checkbox A Then Checkbox C and Checkbox E is selected

    I've handled this using a switch statement because there are multiple combinations but its kind of messy because certain checkbox(es) are not only selected but disabled as well due to the dependent permissions:

    e.g.

        public boolean multipleCheckboxVerification(String roleName) {
        switch (roleName) {
            case CheckboxA:
                return permissionPage.isSelectedChkA() && permissionPage.isSelectedChkC()
                        && permissionPage.isSelectedChkE() && !permissionPage.isEnabledChkC() && !permissionPage.isEnabledChk();}
    

    May I kindly know if there is a cleaner way to return which checkbox isn't as expected?

    I'm currently just using assertTrue from TestNG but the output seems to be vague of which checkbox is actually failing.

    I was looking for an output where the error message would display "Checkbox E is enabled where it should be disabled"



  • You can Replace Conditional with Polymorphism.

    class Checkboxes {
      // ...
      Boolean multipleCheckboxVerification() {
        switch (roleName) {
          case CheckboxA:
            return permissionPage.isSelectedChkA() && permissionPage.isSelectedChkC()
                    && permissionPage.isSelectedChkE() && !permissionPage.isEnabledChkC() && !permissionPage.isEnabledChk();
          case CheckboxB:
            return !permissionPage.isSelectedChkA() && permissionPage.isSelectedChkC()
                    && permissionPage.isSelectedChkE() && !permissionPage.isEnabledChkC() && !permissionPage.isEnabledChk();
           ....
        }
      }
    }
    

    Refactoring...

    abstract class Checkbox {
      // ...
      abstract Boolean isValid(Page permissionPage);
      abstract String getErrorMessage();
    }
    
    class CheckboxA extends Checkbox {
      Boolean isValid(Page permissionpage) {
        return permissionPage.isSelectedChkA() && permissionPage.isSelectedChkC()
                        && permissionPage.isSelectedChkE() && !permissionPage.isEnabledChkC() && !permissionPage.isEnabledChk();
      }
      String getErrorMessage() { return "Checkbox A is enabled where it should be disabled"; }
    }
    class CheckboxB extends Checkbox {
      Boolean isValid(Page permissionpage) {
        return !permissionPage.isSelectedChkA() && permissionPage.isSelectedChkC()
                        && permissionPage.isSelectedChkE() && !permissionPage.isEnabledChkC() && !permissionPage.isEnabledChk();
      }
      String getErrorMessage() { return "Checkbox B is enabled where it should be disabled"; }
    }
    

    This way, you can keep your swtich statements only as new CheckboxA().isValid() or new CheckboxA().getErrorMessage(), e.g.

    If you need to create a new checkbox, you just need to create a new class and add it to switch statement.

    If you need to create further specific cases of CheckboxA, e.g., you can just create a new class and override the methods.



Suggested Topics