### Question:
LabelWithoutFormElementRule extends AbstractRule { @Override protected void applyTo(Page page, Violations violations) { List<HtmlLabel> labels = page.findAllLabelTags(); for (HtmlLabel label : labels) { if (!associatedFormElementExists(page, label.getForAttribute())) { violations.add(createViolation(label, page, RULE_MESSAGE)); } } }  @Override String getName();  }### Answer:
@Test public void labelWithoutFormElementPresent() throws Exception { String content = "<html><body><form><label for=\"missingId\">Some text</label></form></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }
@Test public void labelWithFormElementPresent() throws Exception { String content = "<html><body><form><label for=\"validId\">Some text</label><input id=\"validId\" /></form></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertNoViolations(violations, rule); }
@Test public void labelWithNonFormElementPresent() throws Exception { String content = "<html><body><form><label for=\"wrongElement\">Some Text</label><p id=\"wrongElement\">Not a form element</p></body></html>"; Violations violations = rule.applyTo(toHtmlPage(content)); assertViolations(violations, rule, 1); }