hasElement function

Matcher hasElement(
  1. String selector, {
  2. String? withText,
  3. String? withAttribute,
  4. String? attributeValue,
  5. int? count,
})

Matches if rendered HTML contains element(s) matching the CSS selector.

Optional parameters refine the match:

  • withText: element's text content must contain this string
  • withAttribute: the element must have this attribute
  • attributeValue: combined with withAttribute, the attribute must equal this value
  • count: exactly this many elements must match
expect(html, hasElement('h1', withText: 'Hello'));
expect(html, hasElement('.item', count: 3));
expect(html, hasElement('a[href="/about"]'));
expect(html, hasElement('input', withAttribute: 'required'));
expect(html, hasElement('input', withAttribute: 'type', attributeValue: 'email'));

Implementation

Matcher hasElement(String selector, {String? withText, String? withAttribute, String? attributeValue, int? count}) =>
    _HasElementMatcher(
      selector,
      withText: withText,
      withAttribute: withAttribute,
      attributeValue: attributeValue,
      count: count,
    );