JavaScriptã®ãã¹ãã«ã¤ãã¦æ¬æ°åºãã¦èãã¦ã¿ã(2)
ååããã®ç¶ãã§ã
DOMã¨ãã¥ã¬ã¼ã·ã§ã³ã®æ¦ç¥
ä¸æ¹ã§ãæ¬ç©ã®ãã©ã¦ã¶ã使ããã«ä½ããã®JavaScriptå®è¡ç°å¢ã§DOMãã¨ãã¥ã¬ã¼ããã¦ããã®ä¸ã§ãã¹ããèµ°ãããããã¨ããæ¦ç¥ãããã
ãã®åéã®å¤§å¾¡æã¯Env.js(http://www.envjs.com/)ã¨ãããã¨ã«ãªã£ã¦ããã®ã ãã©ãEnv.jsã®ã¤ã¤ã³ãªã¨ããã¯å°å ¥ãããã©ãããæã§ãããä½ãããã©ãããã£ã¦ãantã§ãã«ãããªãã¡ããããªãããã¹ãã®ããã«ã©ã®ç¨åº¦ã®ç°å¢æ§ç¯ã³ã¹ããããããããã¯ç¶æ³ã«ããã¦éãã ããããä¾ãã°JSãã¡ã¤ã³ã§ãã£ã¦ããã¨ã³ã¸ãã¢ããã¡ãã£ã¨ãã¹ãç°å¢æ´ããããã£ã¦ããæã«antããå ¥ãã¦é å¼µãã ãããï¼Javaã®çµé¨ãããããããã«ããã¼ã«ã®å°å ¥/å©ç¨ã®æµãã«æ £ãã¦ãã人ã ã¨å ¨ç¶åé¡ãªãã¬ãã«ãªãã ãã©ã
ã¨ããããã§ãEnv.jsã¯çµæ§åãå ¥ãã¦éçºããããã®ã§ã¯ããã®ã ãããã©ãåã«ã¨ã£ã¦ã¯ããªããã´ãããããªããããã©ããããã¨ããæããæããªãã½ãªã¥ã¼ã·ã§ã³ãªã®ã§ããããã¡ããå®å ¨ã«ä¸»è¦³ã§ãã
ãã¨ããã¯ä½è«ã ãã©ãEnv.jsçºèµ·äººã®John Resigã¨ãã³ã¸ã§ãªã¹ãã "JavaScript Testing Does Not Scale" (http://ejohn.org/blog/javascript-testing-does-not-scale/) ã¨ããè¨äºã§ããjQueryã®ãã¹ãã«ã¯Rhino + Env.jsã使ã£ã¦ããããããããã©ãä»ã¯ãã使ã£ã¦ãªããTestSwarmã«ç§»è¡ãããHAHAHAãã¨ããæ¨ã®çºè¨ããã¦ããã2009å¹´ã®è¨äºã§ãã¤John Resigã¨ãã³ã¸ã§ãªã¹ãã®ãã¨ã§ãããã®ã§ãæ¢ã«æ¬¡ãªããã¼ã«ã®ã¨ãã³ã¸ã§ãªã¹ãã«ãªã£ã¦ããããã®ãããããªããåã¯ç¥ããªãããã jQueryã®ãã¹ãã«ä½¿ãããªããªã£ãã®ã¯ãããªãã«äºæ ã®ãããã¨ã§ããããã®ã§ãããããã£ã¦Env.jsã®ä¾¡å¤ã貶ããçç±ã«ã¯ãªããªãã念ã®ããã
Env.jsã¨ã¯å¥ã«ãæè¿åãè²ã 試ãã¦ã¿ã¦ããã®ãNode JSã®ã©ã¤ãã©ãªã¨ãã¦éçºããã¦ããjsdom (https://github.com/tmpvar/jsdom)ã§ãããããã¯Node JSã®ä¸ã§DOMãã¨ãã¥ã¬ã¼ãããããã®ã©ã¤ãã©ãªã§ãNode JSã¨npmãå ¥ã£ã¦ããã°npm install jsdomã§ä¸çºã§ã¤ã³ã¹ãã¼ã«ã§ããã®ãé åã§ããã(Node JSã¨npmå ¥ããæç¹ã§äººã«ãã£ã¦ã¯çµæ§ããã©ããããï¼ã£ã¦ããæè¦ã¯ãããããããªãï¼
jsdomã¯ä½¿ã£ã¦ã¿ãæãEventã¾ãããã¡ããã¨åãããnode-jquery (https://github.com/coolaj86/node-jquery) ã使ãã°jQueryãåããããããããæè¿ã§ã¯ãã¼ã«ã«ã®ã¹ã¯ãªãããªã©ãloadã§ããããã«ãªã£ããä¸è¨ãã¼ã¸ãè¦ãã¨
require('jsdom').defaultDocumentFeatures = { FetchExternalResources : ['script'], ProcessExternalResources : false, MutationEvents : false, QuerySelector : false }
ã®ããã«ã©ã®æ©è½ã使ããã®é¸æãã§ããï¼ã¾ã 試ãã¦ããªããï¼DOM Mutation EventãSelectors APIã®ãµãã¼ããå ¥ã£ã¦ããããã ã
jsdomããã¼ã¹ã«ãã¦ãã£ã¨é å¼µã£ã¦ã¨ãã¥ã¬ã¼ããã¡ããããï¼ã¨ããã®ã Zombie.js (http://zombie.labnotes.org/)ã§ãããã¯å¤é¨ã©ã¤ãã©ãªãå©ç¨ãã¦html5ã¾ããã®ãµãã¼ãããã¦ããããã¡ããã¨cookieãªã©ãé£ããä¸ã«xhrãæ±ãããJavaScriptãæ±ããMechanizeãã©ããã¨ããã¨PerlãRubyçéã®äººã«ã¯åããããããããããªããç¨éã«ãã£ã¦ã¯ä¸ã ã¤ã±ãããªæãã§ãããï¼ã¡ãã£ã¨ããçç±ã§ãåãç¾å¨æ¤è¨ãã¦ããJSã®ãã¹ãã«ä½¿ãã®ã¯è¦éã£ãï¼
é¦ããªããã©äººéã§ã
Headã®ãããã©ã¦ã¶ã使ã£ãèªååããã©ã¦ã¶ä»¥å¤ã§ã®ç°å¢ã§ã®ã¨ãã¥ã¬ã¼ã·ã§ã³ã®ä»ã«ããå¥ã«æ¬ç©ã®ãã©ã¦ã¶ãHeadlessã§ããããããã¨ããçºæ³ã®å ã«æè¿åºã¦ããã®ã Phantom JS (http://code.google.com/p/phantomjs/)ã§ãããã¯ãªãã¨QtWebkitã使ã£ã¦æ¬ç©ã®ãã©ã¦ã¶ï¼ãã ãã¬ã³ããªã³ã°ããç»é¢ã表示ããªãã®ã§GUIãå¿ è¦ãªãï¼ãCUIããåããã¦ãã¾ããã¨ãã試ã¿ã§ããã
Phantom JSã¯ã¬ãã§Headlessã«åãæ¬ç©ã®ãã©ã¦ã¶ãªã®ã§ãsessionå¨ããåé¡ãªãåããxhrãæãæ¾é¡ã§ããããã å®éã«åããã¦ã¿ãã¨åãããããã¼ã¸ãã¾ããã¦ä¿åã§ããç¶æ ãStringåãå ¥ããããphantom.stateã®ã¿ãªã®ã§ããã¼ã¸é·ç§»ãå«ããã¹ããæ¸ããã¨ããã¨ä¾ã«ããããã«æ¯åphantom.stateãè¦ããphantom.stateã«ä»£å ¥ããããè¦ã¯å°å³ãªç¶æ é·ç§»ãèªåã§ç®¡çããªããã°ãããªããæ½è±¡åã試ã¿ããã«ãã使ããã®ãphantom.stateã ããªã®ã§ä¸ã å³ããæãã§ãããã¡ãªã¿ã«ã³ãã³ãã©ã¤ã³ããé£ãããJSãã¡ã¤ã«ã¯ã©ãããã¡ã¤ã³ã«ã¼ãçãªãã®ã®ä¸ã«æ¾ããã¾ãã¦ããããã§ãç¶æ é·ç§»ã管çãã¤ã¤ã¡ããã¨æå¾ã«phantom.exit()ãå¼ãã§ãããªãã¨ãã¼ãããJSã®å®è¡ã¯ç¡éã«ã¼ãããã
ã¨ã¯ããHeadlessã§åãããã¤ã¨ãã¥ã¬ã¼ã·ã§ã³ã§ãªãæ¬ç©ã®ãã©ã¦ã¶ãªãã¦ä»ã¾ã§ã«ãªãã£ãã®ã§ãPhantom JSã¯ãDOMã®ã¨ãã¥ã¬ã¼ã·ã§ã³ï¼ä½ããï¼ããé å¼µãã¨ããããªããï¼ãã¨ããç¶æ³ãä½ãåºãå¯è½æ§ãç§ããé¢ç½ããã¼ã«ã ã¨æããAPIã®å å®/æ¹è¯ãå¾ ãããæãã§ãããæ¹è¯ãã¾ããè¦æã§ãæããããããã
ããããZombie.jsã¨ããPhantom JSã¨ãããä½ãããåç©ã£ã½ãååãã¤ãã¦ããã®ã¯æ¬§ç±³ã®äººã«ã¯"Headless"ã¨ããè¨èãããããã£ã人ãªããããã®ãé£æ³ããå¿åæ§ãããã®ã ãããï¼ãªããªãèå³æ·±ãã
(ã¤ã¥ã)