In my past professions whenever the word “testing” came up, there were usually one of two follow-up actions to take on my part as a developer.
– Get with a group of testers and draft a Q&A (quality assurance) specification.
– I would be the one stuck with the task of programming test scripts in some language that would take quite a bit of time (I was using Borland Silk Test version 6 and I’m quite sure the latest is a dramatic improvement).
As I spend most of my time in Javascript, it would be nice to create standard automated regression tests in the same realm of my skill set (i.e. web technologies). And that’s where PhantomJS comes in. For those web developers who have used the Console window inside their browser cranking out console.log statements will be right at home with PhantomJS.
What is it? It’s a headless (something you can’t see) web browser based on WebKit that is “scriptable”. Imagine a console terminal application that can be programmed to go to a web site which performs the following:
– Calculates the time it takes to load the page.
– Performs some validation. When the script enters a letter instead of a integer (inside a input box), does it error like it’s suppose to?
– Automated action. Fill in the input fields and click on the submit button.
If you’re comfortable using jQuery, you can simply load it in the PhantomJS environment and off you go. Here’s a taste of a PhantomJS script that goes to www.omnispear.com to print out all the blog headings:
var page = require('webpage').create(); page.onConsoleMessage = function(msg) { console.log(msg); }; page.open("http://www.omnispear.com", function(status) { if (status === "success") { page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js", function() { page.evaluate(function() { var blogs = $('#blog_feed_text'); $(blogs).find('.wp-title a').each(function(i) { console.log($(this).html()); }); }); phantom.exit(); }); } });
I’ve been following the development of PhantomJS since it first came out in 2011 and I am excited about where the project is heading. If testing websites is something that is in the back of your mind, you definitely should check it out.