Phantom.js+QUnitã§javascriptã®èªåãã¹ãç°å¢ãä½ããã
Phantom.jsã£ã¦ä½?
PhantomJS is a headless WebKit with JavaScript API.
- [PhantomJS: Headless WebKit with JavaScript API ](http://phantomjs.org/)
headless?
ã¹ã¯ãªã¼ã³è¡¨ç¤ºã®ç¡ããCUIã§åä½ãããã©ã¦ã¶
ä»ã«ç¹å¾´ã¯?
è²ããªãã¹ããã¬ã¼ã ã¯ã¼ã¯ã«å¯¾å¿
jasmine, capybara, QUnit, mocha, webdriver, yui test, busterjs, funcunit, robot framework
ä»ã®CUIãã©ã¦ã¶ã¨æ¯ã¹ã¦è»½ã
ãã©ã¦ã¶ã®åä½ãã¨ãã¥ã¬ã¼ããã¦ãã訳ã§ã¯ãªãçº
å°å ¥
èªåã®ç°å¢ã¯ãMacPortsãªã®ã§ãæåã§ã¤ã³ã¹ãã¼ã«
$ wget http://phantomjs.googlecode.com/files/phantomjs-1.7.0-macosx.zip $ unzip phantomjs-1.7.0-macosx.zip $ cd phantomjs-1.7.0-macosx $ bin/phantomjs -v 1.7.0
ç°¡åã
jsã使ã£ã¦åãã
ã¾ãã¯ãhello world
console.log('hello, world!'); // ã¡ã¤ã³ã¹ã¬ãããçµãã£ã¦ãçµäºããªãã®ã§ã // æ示çã«çµããããå¿ è¦ããã phantom.exit();
$ bin/phantomjs test.js Hello, world!
ãã¼ã¸ãèªã¿è¾¼ã
var page = require('webpage').create(); page.open('http://google.com', function () { // ãã¼ã¸ã®ãã£ãã㣠page.render('google.png'); phantom.exit(); });
æ¨æºã ã¨ã594x442px ã§ã¬ã³ããªã³ã°ããã¦ã
page.viewportSizeã§ãµã¤ãºã®å¤æ´ãå¯è½
page.viewportSize = { width: 480, height: 800 };
å é¨ã§javascriptãåãã®?
onloadã§ãdiv id="test" ã«ãæååã表示ãããhtmlãç¨æãã¦å®é¨
<html> <head> <meta charset="utf-8"> <title>title</title> <script> window.onload = function(){ document.getelementbyid("test").innerhtml='test'; } </script> </head> <body> <div id="test1"></div> </body> </html>
test.js ã«ã¦ãhtmlãèªã¿è¾¼ãã§ãonloadã§è¡¨ç¤ºãããdiv id="test"ã®ä¸ã®æååãåå¾ãã¦è¡¨ç¤ºãã
var page = require('webpage').create(); page.open('01.html', function () { // ãã¼ã¸ã¢ã¯ã»ã¹ setTimeout(function(){ console.log( // ãã¼ã¸å ã§JSãåããã¡ã½ãã page.evaluate(function () { return document.getElementById("test").innerHTML; }) ); phantom.exit(); }, 1000); });
$ bin/phantomjs test.js test
ãããã!
ã¤ãã³ãéåä¿¡
ä»åº¦ã¯ãããªHTMLãç¨æ
div id="test" ãã¯ãªãã¯ããã¨ãconfirmãåºã¦åå²
// ã ä¸ç¥ ã window.onload = function(){ document.getElementById("test").addEventListener("click", function(){ if(confirm("click?")){ alert("yes"); }else{ alert("no"); } }); } </script> </head> <body> <div id="test">click me!</div> </body> </html>
var page = require('webpage').create(); page.open('01.html', function () { // ãã¼ã¸ã¢ã¯ã»ã¹ page.onConfirm = function(msg){ // confirmçºçæã«ããªã¬ console.log("confirm : "+msg); return true; // çå½å¤ã§OKããã£ã³ã»ã«ã決ãã } page.onAlert = function(msg){ // ã¢ã©ã¼ãçºçæã«ããªã¬ console.log("alert : "+msg); } page.includeJs("jquery.min.js", function(){ // jQueryãinclude var offset = page.evaluate(function(){ // ãªã³ã¯ã®offsetãåå¾ return $("* test").offset(); }); page.sendEvent('click', offset.left+1, offset.top+1); // ã¯ãªã㯠}); });
$ bin/phantomjs test.js alert : jquery.min.js * includeJså®äºæã«ãalertã¤ãã³ããé£ãã§ãã£ã½ã confirm : click? alert : yes
ã¤ãã³ããåãåã£ã¦ãconfirmã§yesãé¸æãã¦ãã
ã¡ãªã¿ã«ãconfirmãåãåããªãå ´åãããã©ã«ãã§ã¯ããããããé¸æãã¦ããæ§å
QUnité£æº
- javascriptã®unitãã¹ããã¬ã¼ã ã¯ã¼ã¯
- QunitããPhantom.js ã§å®è¡ããããã¨ãå¯è½
- å ¨ã¦CUIã§å®è¡ã§ããã®ã§ãCIç°å¢ã«æã¡è¾¼ãã®ãç°¡å
é£æºã«ã¯ãexamples/run-QUnit.js ã使ã
試ãã«ãQUnitã®ãã¹ããPhantom.jsã§å®è¡ãã¦ã¿ã
$ examples/run-QUnit.js ../jquery-QUnit-4e03a4b/test/index.html; echo $1 'waitfor()' finished in 1901ms. tests completed in 1771 milliseconds. 596 tests of 596 passed, 0 failed.bin/phantomjs 0
- æåã®å ´åãæ»ãå¤ã¯0
- 失æã®å ´åã¯ã1ãè¿ã
ã¾ã¨ã
- å é¨çã«ã¯ãQUnitãopenãã¦ãçµæãåºåããDOMãã¦ã©ãããã¦ããã ããªã®ã§ãã¡ãã£ã¨ãããã°ãã¨ã©ã¼ãçºçãããã¹ããåºåããã¨ãã§ããã
- æ»ãå¤ãè¿ãã®ã§ãJenkinsã¸ã®çµã¿è¾¼ã¿ãç°¡åã«ã§ããã
- å ¨ãã©ã¦ã¶ã®ãã¹ãã¯ç¡çã ãã©ãCIã«çµã¿è¾¼ããã®ãè¯ãã¨æã£ã
- ãã¶ã¤ããããjavascriptæ¸ãã¦ããã¹ããQUnitã§æ¸ãã¨ããç¶æ³ãªãã°ãPhantom.js ã¨çµã¿åããã¦ãCIã«çµã¿è¾¼ãã®ããã¿ã¼?