+++ title = "phantomjs" draft = false tags = [ "code", "javascript", "testing", "phantomjs" ] date = "2013-03-12" +++ # Test automatisatie: PhantomJS Een experiment om mijn `JasmineTestRunner` uit te breiden (of volledig op te splitsen) van **Rhino + Envjs 1.2** naar **PhantomJS 1.4** (liefst 1.5 maar dit moet ik nog compilerend krijgen onder Windows). ### Phantom.js test code #### Start script ```javascript console.log("loading"); var page = new WebPage(); // Is nodig omdat anders in uw testpagina console.log() niets doet. page.onConsoleMessage = function(msg) { console.log("debug from page: " + msg); }; /** * Phantom JS 1.4 tests voor EnvJS "fixes" die ik heb moeten erin hacken. * Alles werkt blijkbaar native, hoera! * * TODO: * ----- * # Waarom werken relatieve paden niet met openen van de pagina (crash Phantom) * # Waarom werken script includes niet, lokaal op relatief pad? Absoluut = access denied * -> Een HTTP pad opgeven werkt wel, zoals jQuery.com include. * -> Zolang dit niet goed werkt kunnen we niet naar de jasmine HTML suite gaan! * # testen van set/clearTimeout en set/clearInterval * # Heb ik Rhino dan nog wel nodig? Hoe kan ik phantomjs output evalueren en dan in een TestRunner knallen? * * Jasmine integratie * ------------------ * Custom XML reporters, zie * - http://code.google.com/p/phantomjs/wiki/ExternalArticles * - http://code.google.com/p/phantomjs/wiki/TestFrameworkIntegration * - https://github.com/detro/phantomjs-jasminexml-example **/ page.open('D:

Profiles

BVERBEKE

Desktop

phantomjs-1.4.1-win32-dynamic

test.html', function(status) { console.log("should have printed 'yoo' AND 'sup?', include from test.js??"); console.log(""); var testHTML = page.evaluate(function() { return document.getElementById('test').innerHTML; }); var isKnopHidden = page.evaluate(function() { return $("#hiddenKnop").is(":hidden"); }); var whichIsFocussed = page.evaluate(function() { return document.activeElement.id; }); var divHeightCSSGetterTest = page.evaluate(function() { return $("#test").css("height"); }); var jQueryCheckedSelectorTest = page.evaluate(function() { $("#check").click(); return $("#check").is(":checked"); }); console.log("got testHTML " + testHTML); console.log("is knop hidden? (should be) --> " + isKnopHidden); console.log("which is focussed? (should be textarea) --> #" + whichIsFocussed); console.log("div height? (should be 100px) --> " + divHeightCSSGetterTest); console.log("checkbox checked? (should be true) --> " + jQueryCheckedSelectorTest); phantom.exit(); }); ``` #### Test.js script ```javascript console.log("supp??"); ``` #### Test.html resources ```html
bla
``` #### Console Output bovenstaande ``` D:
Profiles
BVERBEKE
Desktop
phantomjs-1.4.1-win32-dynamic>phantomjs start.js loading debug from page: yoo debug from page: is knop hidden? (should be) --> true should have printed 'yoo' AND 'sup?', include from test.js?? got testHTML jquery nieuwe blabla is knop hidden? (should be) --> true which is focussed? (should be textarea) --> #text div height? (should be 100px) --> 100px checkbox checked? (should be true) --> true ``` ### Phantom.js uitvoeren Downloaden (1.4 voor windows...) op http://code.google.com/p/phantomjs/downloads/list, zit Qt4 libs in. Dan met: -> `phantomjs start.js` Die dan met `page.open()` een webpagina inlaadt. Blijkbaar werkt `