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?

4
  • Are you looking for a CLI or an API? If an API, what language?
    – user246
    Commented Jun 15, 2014 at 22:33
  • I'm doing my testing in eclipse. If there is an API that I can use to test this java code I will take it. Again I am testing java code that makes HMTL code as a string.
    – Mike John
    Commented Jun 16, 2014 at 6:31
  • Should it be automated procedure of validating or you consider only manual testing?
    – olyv
    Commented Jun 16, 2014 at 6:38
  • @olyv Automated.
    – Mike John
    Commented Jun 16, 2014 at 19:38

5 Answers 5

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

2
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.

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>
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).

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.