Name: hamcrest-html-matchers
Owner: Wikimedia Deutschland e.V.
Description: Set of Hamcrest matchers for HTML assertrions
Created: 2017-01-24 12:00:03.0
Updated: 2018-04-19 10:30:13.0
Pushed: 2018-05-14 08:46:23.0
Homepage: null
Size: 94
Language: PHP
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Hamcrest allows you to create pretty complicated and flexible assertions. Just remember:
“You can” does not mean “you should”.
The following example shows how we can ensure that there is an HTML form and password entered in it is not weak:
l = '<div>
<form>
<input name="password" value="strong password"/>
</form>
</div>';
rtThat($html, is(htmlPiece(
havingChild(
both(withTagName('form'))
->andAlso(
havingDirectChild(
allOf(
withTagName('input'),
withAttribute('name')->havingValue('password'),
withAttribute('value')->havingValue(not('weak password')))))))));
Usage limitations:
htmlPiece()
call (is()
can be used to improve readability)havingRootElement()
, havingDirectChild()
or havingChild()
needed to be passed as an argument to htmlPiece()
Information about general Hamcrest usage can be found at Hamcrest GitHub repository.
htmlPiece()
- checks that string is a valid HTML, parses it and passes control to given matcher if one present
rtThat('<p></p>', is(htmlPiece())); // Just checking that string is a valid piece of HTML
havingRootElement
- checks given constraint against root element of HTML. NOTE: Can be passed only to htmlPiece()
rtThat('<p></p>', htmlPiece(havingRootElement(withTagName('p'))));
havingDirectChild
- checks given constraint against direct children
rtThat('<p><b></b></p>', htmlPiece(havingRootElement(havingDirectChild(withTagName('b')))));
havingChild
- checks given constraint against all children
rtThat('<p><b></b></p>', htmlPiece(havingChild(withTagName('b'))));
withTagName
- checks given constraint against tag name
rtThat('<p><b></b></p>', htmlPiece(havingChild(withTagName(
either(equalTo('i'))->orElse(equalTo('b'))
;
withAttribute
- checks given constraint against elements attributes comparing names and values
rtThat('<p><input required></p>', htmlPiece(havingChild(withAttribute('required'))));
rtThat('<p><input data-value="some data"></p>', htmlPiece(havingChild(
withAttribute(startsWith('data-'))->havingValue('some data'))));
withClass
- checks given constraint against element's class list
rtThat('<p class="class1 class2 class3"></p>', htmlPiece(havingChild(
withClass('class2'))));
havingTextContents
- checks given constraint against elements text contents
rtThat('<div><p>this is Some Text</p></div>', htmlPiece(havingChild(
havingTextContents(containsString('some text')->ignoringCase()))));
tagMatchingOutline
- tolerantly checks that tag matches given outline (outline - tag representation in HTML format)\
That means:
class
is treated in a different manner (see further).This will pass:
rtThat('<form><input id="id-pass" name="password" class="pretty green" required="required"></form>',
mlPiece(havingChild(
tagMatchingOutline('<input name="password" class="green" required>')