2

I am testing a class that takes 1 string and generates an HTML page around that string.

So I pass it the string Math and it returns to me a whole HTML page as a string containing that Word and with matching <td></td> a header <h3></h3> and a form.

How do I validate this as a valid HTML code?

Niels van Reijmersdal
  • 32,520
  • 4
  • 57
  • 124
Mike John
  • 135
  • 2
  • 6

5 Answers5

8

Yes, it is important to validate HTML. There are W3C standards against which you can validate it.

You can use various tools like the W3C Html validator, or Web Developer addon of FireFox.

There are several such paid an unpaid tools that can help you validate your HTML. It helps in page optimization and SEO.

Lesly Tavis
  • 341
  • 2
  • 2
3

I would create a jUnit test using a HTML validator like jTidy API, like suggested in https://stackoverflow.com/questions/4217801/a-html-validator-in-java

Also see running tests like this in Eclipse: http://help.eclipse.org/juno/topic/org.eclipse.jdt.doc.user/gettingStarted/qs-junit.htm

Niels van Reijmersdal
  • 32,520
  • 4
  • 57
  • 124
  • This tool fixes the problems with the HTML but it doesn't tell me what are the errors. I just need to know if an HTML is valid or not. I don't need to fix the errors. – Mike John Jun 17 '14 at 04:27
  • 1
    @Mike John http://stackoverflow.com/questions/2455980/how-can-i-get-the-error-warning-messages-out-of-the-parsed-html-using-jtidy – Niels van Reijmersdal Jun 17 '14 at 06:23
2

Once the HTML page is generated try validating it with W3C HTML validator!

Its maven info is:

<dependency>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-w3c</artifactId>
    <version>1.1</version>
</dependency>
IAmMilinPatel
  • 9,026
  • 7
  • 41
  • 67
1

Agree with suggestion to use validator. One more approach is just to parse xml using xpath. Test will fail if document structure is broken. As a regression approach you can create some html files and compare those pattern files with newly generated. For example by using bash (or smth. similar in Windows OS).

olyv
  • 530
  • 3
  • 15
1

Apache JMeter provides HTML Assertion which uses JTidy HTML syntax checker.

You can use JMeter to test your generated pages as per How to Use JMeter Assertions in 3 Easy Steps. It can be launched as command-line application, integrated into Ant, Maven, Jenkins, build or launched from Java code.

If you don't need the superpower of JMeter you can just extract HTML Assertion related code as it's free and open source product.

Dmitri T
  • 13,472
  • 1
  • 15
  • 19