This is an AMP version of the article, its original content can be found here.
PhantomJS as an HTML Validator
I created phandom.org a few months ago, but yesterday finally found the time to make some needed changes to it. So, now is a good time to explain how I'm using Phandom in some of my unit tests.
Before I get started, though, I should say a few words about
phantomjs, which is a
a user interface. WebKit is a C++ library that enables manipulation of
located code in
phantomjs from the command line with the following code:
PhantomJS creates a
page object (provided by
phantomjs), and then asks it to
open() a Web page. The object
communicates with WebKit and converts this call into DOM instructions. After
which, the page loads. The PhantomJS engine then terminates on line 6.
So far so good, and this is the traditional way of using PhantomJS. Now, on to giving you an idea of how Phandom (which stands for "PhantomJS DOM") works inside Java unit tests:
To test this, let's give
phantomjs an HTML page and ask him to render it. When
the page is ready, we'll ask
phantomjs to show us how this HTML looks in WebKit.
If we see the elements we need and desire,—we're good to go. Let's use
the following example:
When we use the above code, here is what happens. First, we get HTML
html as a
doc object, and then pass it to
as an argument. Then,
on line 13, we call the
Phandom.dom() method to get an instance
of the class
dom() produces a runtime
exception and the unit test fail. If HTML is clean and WebKit is able to
render it without problems, the test passes.
I'm using this mechanism in a few different projects,and it works quite well. Therefore, I highly recommend it.
Of course, you shouldn't forget that you must have
phantomjs installed on your
build machine. In order to avoid unit test failures when
phantomjs is not available or present, I've created the following supplementary method:
Enjoy and feel free to report any bugs or problems you encounter to: GitHub issues :)