### Question:
SelectFieldWithValidOptionRule extends AbstractRule { @Override protected void applyTo(Page page, Violations violations) { for (HtmlSelect select : page.findAllSelectTags()) { List<HtmlOption> options = select.getOptions(); if (options.isEmpty()) { violations.add(createViolation(select, page, RULE_MESSAGE_MINIMUM_ONE_OPTION)); } else { if (options.get(0).hasAttribute("disabled")) { violations.add(createViolation(select, page, RULE_MESSAGE_FIRST_OPTION_ENABLED)); } } } }  @Override String getName();  }### Answer:
@Test public void firstOptionMustNotBeDisabledAndUsedForInstructions() throws Exception { String content = "<html><body>" + " <select name=\"gender\">" + " <option disabled=\"disabled\">Please choose</option>" + " <option>male</option>" + " <option>female</option>" + " </select>" + "</body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }
@Test public void minimumOneOptionMustExist() throws Exception { String content = "<html><body>" + " <select name=\"gender\"></select>" + "</body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }
@Test public void validSelectFieldWithFirstOptionAvailableForSelection() throws Exception { String content = "<html><body>" + " <select name=\"gender\">" + " <option>male</option>" + " <option>female</option>" + " </select>" + "</body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertNoViolations(violations, rule); }