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?
-
Are you looking for a CLI or an API? If an API, what language?user246– user2462014年06月15日 22:33:10 +00:00Commented 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– Mike John2014年06月16日 06:31:24 +00:00Commented Jun 16, 2014 at 6:31
-
Should it be automated procedure of validating or you consider only manual testing?olyv– olyv2014年06月16日 06:38:44 +00:00Commented Jun 16, 2014 at 6:38
-
@olyv Automated.Mike John– Mike John2014年06月16日 19:38:53 +00:00Commented Jun 16, 2014 at 19:38
5 Answers 5
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
-
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– Mike John2014年06月17日 04:27:11 +00:00Commented Jun 17, 2014 at 4:27
-
1@Mike John stackoverflow.com/questions/2455980/…Niels van Reijmersdal– Niels van Reijmersdal2014年06月17日 06:23:52 +00:00Commented Jun 17, 2014 at 6:23
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.
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>
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).
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.