2017 © Pedro Peláez
 

library hamcrest-html-matchers

Set of Hamcrest matchers for HTML assertions

image

wmde/hamcrest-html-matchers

Set of Hamcrest matchers for HTML assertions

  • Thursday, July 19, 2018
  • by wmde
  • Repository
  • 18 Watchers
  • 0 Stars
  • 58,221 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 11 Versions
  • 17 % Grown

The README.md

This is the set of Hamcrest matchers for HTML assertions

Build Status Scrutinizer Code Quality Code Coverage, (*1)

Usage examples

Hamcrest allows you to create pretty complicated and flexible assertions. Just remember:, (*2)

"You can" does not mean "you should"., (*3)

The following example shows how we can ensure that there is an HTML form and password entered in it is not weak:, (*4)

$html = '

'; assertThat($html, is(htmlPiece( havingChild( both(withTagName('form')) ->andAlso( havingDirectChild( allOf( withTagName('input'), withAttribute('name')->havingValue('password'), withAttribute('value')->havingValue(not('weak password')))))))));

Usage limitations: * Each HTML assertion starts with htmlPiece() call (is() can be used to improve readability) * One of havingRootElement(), havingDirectChild() or havingChild() needed to be passed as an argument to htmlPiece(), (*5)

Documentation

Information about general Hamcrest usage can be found at Hamcrest GitHub repository., (*6)

Available Matchers

  • htmlPiece() - checks that string is a valid HTML, parses it and passes control to given matcher if one present php assertThat('<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(), (*7)

    assertThat('<p></p>', htmlPiece(havingRootElement(withTagName('p'))));
    
  • havingDirectChild - checks given constraint against direct children, (*8)

    assertThat('<p><b></b></p>', htmlPiece(havingRootElement(havingDirectChild(withTagName('b')))));
    
  • havingChild - checks given constraint against all children, (*9)

    assertThat('<p><b></b></p>', htmlPiece(havingChild(withTagName('b'))));
    
  • withTagName - checks given constraint against tag name, (*10)

    assertThat('<p><b></b></p>', htmlPiece(havingChild(withTagName(
        either(equalTo('i'))->orElse(equalTo('b'))
    ))));
    
  • withAttribute - checks given constraint against elements attributes comparing names and values, (*11)

    assertThat('<p><input required></p>', htmlPiece(havingChild(withAttribute('required'))));
    

    php assertThat('<p><input data-value="some data"></p>', htmlPiece(havingChild( withAttribute(startsWith('data-'))->havingValue('some data'))));, (*12)

  • withClass - checks given constraint against element's class list, (*13)

    assertThat('<p class="class1 class2 class3"></p>', htmlPiece(havingChild(
        withClass('class2'))));
    
  • havingTextContents - checks given constraint against elements text contents, (*14)

    assertThat('<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), (*15)

    That means:, (*16)

    • Element's tag name is equal to outline's tag name
    • Element has all the attributes that outline has with the same values. If element has more attributes than outline it still matches.
      • NOTE: Attribute class is treated in a different manner (see further).
      • NOTE: If attribute outlined is boolean, then its value in element won't be checked, just presence.
    • Element has all html classes that outline has.

    This will pass:, (*17)

    assertThat('<form><input id="id-pass" name="password" class="pretty green" required="required"></form>', 
      htmlPiece(havingChild(
          tagMatchingOutline('<input name="password" class="green" required>')
    )));
    

The Versions

19/07 2018

dev-clear-xml-errors

dev-clear-xml-errors

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

14/05 2018

dev-denormalization

dev-denormalization

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

14/05 2018

dev-contains-string-ignoring-white-space

dev-contains-string-ignoring-white-space

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

19/04 2018

dev-master

9999999-dev

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

19/04 2018

dev-minorPerf

dev-minorPerf

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

18/04 2018

dev-phpCsFixups105

dev-phpCsFixups105

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

17/04 2018

dev-phpDocTags

dev-phpDocTags

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

16/04 2018

dev-spaceyParenthesis

dev-spaceyParenthesis

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

11/07 2017

dev-mwCodeFormatting1

dev-mwCodeFormatting1

Set of Hamcrest matchers for HTML assertions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

05/07 2017

v0.1.1

0.1.1.0

Set of Hamcrest matchers for HTML assertrions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov

09/02 2017

v0.1.0

0.1.0.0

Set of Hamcrest matchers for HTML assertrions

  Sources   Download

LGPL-2.1

The Requires

 

The Development Requires

by Aleksey Bekh-Ivanov