- assert.dom()
- DOMAssertions
- exists
- doesNotExist
- isChecked
- isNotChecked
- isFocused
- isNotFocused
- isRequired
- isNotRequired
- isValid
- isNotValid
- isVisible
- isNotVisible
- hasAttribute
- doesNotHaveAttribute
- hasAria
- doesNotHaveAria
- hasProperty
- isDisabled
- isNotDisabled
- hasClass
- doesNotHaveClass
- hasStyle
- hasPseudoElementStyle
- doesNotHaveStyle
- doesNotHavePseudoElementStyle
- hasText
- hasAnyText
- hasNoText
- includesText
- doesNotIncludeText
- hasValue
- includesValue
- doesNotIncludeValue
- hasAnyValue
- hasNoValue
- matchesSelector
- doesNotMatchSelector
- hasTagName
- doesNotHaveTagName
- hasHtml
- doesNotHaveHtml
- includesHtml
- doesNotIncludeHtml
Once installed the DOM element assertions are available at assert.dom(...).*
:
Parameters
target
(string | HTMLElement) A CSS selector that can be used to find elements usingquerySelector()
, or an [HTMLElement][] (Not all assertions support both target types.) (optional, defaultrootElement
ordocument
)rootElement
(HTMLElement | Document | ShadowRoot | null)? The root element of the DOM in which to search for thetarget
(optional, defaultsdocument
whennull
or not provided)
Examples
test('the title exists', function(assert) {
assert.dom('#title').exists();
});
- See: #doesNotExist
Assert an HTMLElement (or multiple) matching the selector
exists.
assert.dom('#title').exists();
assert.dom('.choice').exists({ count: 4 });
- See: #exists
Assert an HTMLElement matching the selector
does not exists.
message
string?
assert.dom('.should-not-exist').doesNotExist();
- See: #isNotChecked
Assert that the HTMLElement or an HTMLElement matching the
selector
is currently checked.
Note: This also supports aria-checked="true/false"
.
message
string?
assert.dom('input.active').isChecked();
- See: #isChecked
Assert that the HTMLElement or an HTMLElement matching the
selector
is currently unchecked.
Note: This also supports aria-checked="true/false"
.
message
string?
assert.dom('input.active').isNotChecked();
- See: #isNotFocused
Assert that the HTMLElement or an HTMLElement matching the
selector
is currently focused.
message
string?
assert.dom('input.email').isFocused();
- See: #isFocused
Assert that the HTMLElement or an HTMLElement matching the
selector
is not currently focused.
message
string?
assert.dom('input[type="password"]').isNotFocused();
- See: #isNotRequired
Assert that the HTMLElement or an HTMLElement matching the
selector
is currently required.
message
string?
assert.dom('input[type="text"]').isRequired();
- See: #isRequired
Assert that the HTMLElement or an HTMLElement matching the
selector
is currently not required.
message
string?
assert.dom('input[type="text"]').isNotRequired();
- See: #isValid
Assert that the HTMLElement passes validation
Validity is determined by asserting that:
element.reportValidity() === true
message
string?
assert.dom('.input').isValid();
- See: #isValid
Assert that the HTMLElement does not pass validation
Validity is determined by asserting that:
element.reportValidity() === true
message
string?
assert.dom('.input').isNotValid();
- See: #isNotVisible
Assert that the HTMLElement or an HTMLElement matching the
selector
exists and is visible.
Visibility is determined by asserting that:
- the element's offsetWidth and offsetHeight are non-zero
- any of the element's DOMRect objects have a non-zero size
Additionally, visibility in this case means that the element is visible on the page, but not necessarily in the viewport.
assert.dom('#title').isVisible();
assert.dom('.choice').isVisible({ count: 4 });
- See: #isVisible
Assert that the HTMLElement or an HTMLElement matching the
selector
does not exist or is not visible on the page.
Visibility is determined by asserting that:
- the element's offsetWidth or offsetHeight are zero
- all of the element's DOMRect objects have a size of zero
Additionally, visibility in this case means that the element is visible on the page, but not necessarily in the viewport.
message
string?
assert.dom('.foo').isNotVisible();
Assert that the HTMLElement has an attribute with the provided name
and optionally checks if the attribute value
matches the provided text
or regular expression.
assert.dom('input.password-input').hasAttribute('type', 'password');
- See: #hasAttribute
Assert that the HTMLElement has no attribute with the provided name
.
Aliases: hasNoAttribute
, lacksAttribute
assert.dom('input.username').doesNotHaveAttribute('disabled');
- See: #doesNotHaveAria
Assert that the HTMLElement has an ARIA attribute with the provided
name
and optionally checks if the attribute value
matches the provided
text or regular expression.
assert.dom('button').hasAria('pressed', 'true');
- See: #hasAria
Assert that the HTMLElement has no ARIA attribute with the
provided name
.
Aliases: hasNoAria
, lacksAria
assert.dom('button').doesNotHaveAria('pressed');
- See: #doesNotHaveProperty
Assert that the HTMLElement has a property with the provided name
and checks if the property value
matches the provided text or regular
expression.
assert.dom('input.password-input').hasProperty('type', 'password');
- See: #isNotDisabled
Assert that the HTMLElement or an HTMLElement matching the
selector
is disabled.
message
string?
assert.dom('.foo').isDisabled();
- See: #isDisabled
Assert that the HTMLElement or an HTMLElement matching the
selector
is not disabled.
Aliases: isEnabled
message
string?
assert.dom('.foo').isNotDisabled();
- See: #doesNotHaveClass
Assert that the HTMLElement has the expected
CSS class using
classList
.
expected
can also be a regular expression, and the assertion will return
true if any of the element's CSS classes match.
assert.dom('input[type="password"]').hasClass('secret-password-input');
assert.dom('input[type="password"]').hasClass(/.*password-input/);
- See: #hasClass
Assert that the HTMLElement does not have the expected
CSS class using
classList
.
expected
can also be a regular expression, and the assertion will return
true if none of the element's CSS classes match.
Aliases: hasNoClass
, lacksClass
assert.dom('input[type="password"]').doesNotHaveClass('username-input');
assert.dom('input[type="password"]').doesNotHaveClass(/username-.*-input/);
- See: #hasClass
Assert that the [HTMLElement][] has the expected
style declarations using
window.getComputedStyle
.
assert.dom('.progress-bar').hasStyle({
opacity: 1,
display: 'block'
});
- See: #hasClass
Assert that the pseudo element for selector
of the [HTMLElement][] has the expected
style declarations using
window.getComputedStyle
.
assert.dom('.progress-bar').hasPseudoElementStyle(':after', {
content: '";"',
});
- See: #hasClass
Assert that the [HTMLElement][] does not have the expected
style declarations using
window.getComputedStyle
.
assert.dom('.progress-bar').doesNotHaveStyle({
opacity: 1,
display: 'block'
});
- See: #hasClass
Assert that the pseudo element for selector
of the [HTMLElement][] does not have the expected
style declarations using
window.getComputedStyle
.
assert.dom('.progress-bar').doesNotHavePseudoElementStyle(':after', {
content: '";"',
});
- See: #includesText
Assert that the text of the HTMLElement or an HTMLElement
matching the selector
matches the expected
text, using the
textContent
attribute and stripping/collapsing whitespace.
expected
can also be a regular expression.
Note: This assertion will collapse whitespace if the type you pass in is a string. If you are testing specifically for whitespace integrity, pass your expected text in as a RegEx pattern.
Aliases: matchesText
// <h2 id="title">
// Welcome to <b>QUnit</b>
// </h2>
assert.dom('#title').hasText('Welcome to QUnit');
assert.dom('.foo').hasText(/[12]\d{3}/);
- See: #hasText
Assert that the textContent
property of an HTMLElement is not empty.
message
string?
assert.dom('button.share').hasAnyText();
- See: #hasNoText
Assert that the textContent
property of an HTMLElement is empty.
message
string?
assert.dom('div').hasNoText();
- See: #hasText
Assert that the text of the HTMLElement or an HTMLElement
matching the selector
contains the given text
, using the
textContent
attribute.
Note: This assertion will collapse whitespace in
textContent
before searching. If you would like to assert on a string that should contain line breaks, tabs, more than one space in a row, or starting/ending whitespace, use the #hasText selector and pass your expected text in as a RegEx pattern.
Aliases: containsText
, hasTextContaining
assert.dom('#title').includesText('Welcome');
Assert that the text of the HTMLElement or an HTMLElement
matching the selector
does not include the given text
, using the
textContent
attribute.
Aliases: doesNotContainText
, doesNotHaveTextContaining
assert.dom('#title').doesNotIncludeText('Welcome');
- See: #includesValue
- See: #hasAnyValue
- See: #hasNoValue
Assert that the value
property of an HTMLInputElement matches
the expected
text or regular expression.
If no expected
value is provided, the assertion will fail if the
value
is an empty string.
assert.dom('input.username').hasValue('HSimpson');
- See: #doesNotIncludeValue
Assert that the value
property of an HTMLInputElement includes
the expected
text.
Aliases: containsValue
, hasValueContaining
assert.dom('textarea.description').includesValue('https://example.com');
- See: #includesValue
Assert that the value
property of an HTMLInputElement does not include
the expected
text.
Aliases: doesNotContainValue
, doesNotHaveValueContaining
assert.dom('textarea.description').doesNotIncludeValue('https://example.com');
- See: #hasValue
- See: #hasNoValue
Assert that the value
property of an HTMLInputElement is not empty.
message
string?
assert.dom('input.username').hasAnyValue();
- See: #hasValue
- See: #hasAnyValue
Assert that the value
property of an HTMLInputElement is empty.
Aliases: lacksValue
message
string?
assert.dom('input.username').hasNoValue();
Assert that the target selector selects only Elements that are also selected by compareSelector.
assert.dom('p.red').matchesSelector('div.wrapper p:last-child')
Assert that the target selector selects only Elements that are not also selected by compareSelector.
assert.dom('input').doesNotMatchSelector('input[disabled]')
Assert that the tagName of the HTMLElement or an HTMLElement
matching the selector
matches the expected
tagName, using the
tagName
property of the HTMLElement.
// <h1 id="title">
// Title
// </h1>
assert.dom('#title').hasTagName('h1');
Assert that the tagName of the HTMLElement or an HTMLElement
matching the selector
does not match the expected
tagName, using the
tagName
property of the HTMLElement.
// <section id="block">
// Title
// </section>
assert.dom('section#block').doesNotHaveTagName('div');
Assert that the html of the HTMLElement or an HTMLElement
matching the selector
matches the expected
html, using the
innerHTML
property of the HTMLElement.
expected
can also be a regular expression.
Note: This assertion will collapse whitespace if the type you pass in is a string. If you are testing specifically for whitespace integrity, pass your expected html in as a RegEx pattern.
// <h1>
// A <b>great</b> thing
// </h1>
assert.dom('h1').hasHtml('A <b>great</b> thing');
assert.dom('h1').hasHtml(/.*\s<b>great.+/);
Assert that the html of the HTMLElement or an HTMLElement
matching the selector
does not match the expected
html, using the
innerHTML
property of the HTMLElement.
// <section>
// a <b>great</b> thing
// </section>
assert.dom('section').doesNotHaveHtml('<b>fantastic</b>');
- See: #hasHtml
Assert that the html of the HTMLElement or an HTMLElement
matching the selector
contains the given html
, using the
innerHTML
property.
Note: This assertion will collapse whitespace in
innerHTML
before searching. If you would like to assert on a string that should contain line breaks, tabs, more than one space in a row, or starting/ending whitespace, use the #hasText selector and pass your expected html in as a RegEx pattern.
Aliases: containsHtml
, hasHtmlContaining
assert.dom('#title').includesHtml('<b>nice</b>');
Assert that the html of the HTMLElement or an HTMLElement
matching the selector
does not include the given expected
html, using the
innerHTML
attribute.
Aliases: doesNotContainHtml
, doesNotHaveHtmlContaining
assert.dom('#title').doesNotIncludeHtml('<i>nope</i>');