-ä»æ¥ã¯ãYandex.Directã®Webã€ã³ã¿ãŒãã§ã€ã¹ã§åäœãã¹ããäœæããæ¹æ³ã説æããŸãã äžè¬ã«ãã€ã³ã¿ãŒãã§ã€ã¹ãã¹ããä»ã®ãã¹ããšã©ã®ããã«ç°ãªããã確èªããŸãã ãã¹ããäœæããã«ã¯ãSeleniumã䜿çšããæ¹æ³ãšãããã¬ã¹ãã©ãŠã¶ãŒã䜿çšããæ¹æ³ã®2ã€ãæ€èšããŠãã ããã æåŸã«ãHeadless Chromeã§ãã¹ããå®è¡ããããã«Yandex.Directã§äœæããããŒã«ã玹ä»ããŸãã
ããã¯ãã€ã¬ã¯ã-åºåãªã©ã®ç®¡çããã«ã§ãã
ãããŠãããã¯æ€çŽ¢ã«æ¬¡ãã§Yandexã§2çªç®ã«å€§ããªãããžã§ã¯ãã§ãã ããšãã°ãããã³ããšã³ãããŒã ã«ã¯16人ãããŸãã 以åã®ä»äºã¯ãã¹ãŠæ倧4人ã§ããã ãªããããªã«äººãå€ãã®ã§ããïŒ ããªãã¯ã¡ããã©åºåãããŒãã¬ãŒãºãå ¥åããŸãã-ããããã¹ãŠã®äººã ã¯ããã§äœãããŠããŸããïŒ
Directã«ã¯éåžžã«è€éãªãµããžã§ã¯ããšãªã¢ããããŸãã ããã¯å žåçãªè±¡ã®è²©å£²åºåã§ãã è¿œå ã®ãªã³ã¯ãå«ãèµ€ã1ã€ã®å°ããªãããã¯ã匷調衚瀺ããŸããã
Yandex.Directã€ã³ã¿ãŒãã§ãŒã¹ã§ã¯ããããã¯èšå®ã®ãããã©ãŒã ã¯æ¬¡ã®ããã«ãªããŸãã
ã¢ããŠã³ã¹ã®ãã¬ãã¥ãŒããããåãªã³ã¯ã«ããã€ãã®ãã£ãŒã«ããããããµãŒããŒäžã®ç¹å¥ãªãã®ãå ¥åãããªã³ã¯ãã¿ããããéããŠããããšã確èªãããŸã§ããããã¯è€éãªæ€èšŒã§ãã³ã°ããŸãã ãŸã ä»ã«ãå€ãã®ããžãã¯ãããããã®åœ¢åŒã¯1ã€ã®å°ããªãããã¯çšã§ãããåºåã«ã¯å€ãã®ãããã¯ããããŸãã
ãããŠãããã¯åãªãåºåã®äžçš®ã§ãã-ããã¹ãåºåã ä»ã«ãå€ãã®ã¿ã€ãããããŸãã ããã«ããã£ã¹ãã¬ã€èšå®ã«ã€ããŠã話ããŸããã ãã®åºåããã€èª°ã«è¡šç€ºãããã決å®ããä»ã®å€ãã®èšå®ããããŸãã
äžè¬çã«ããã®å³ã¯å€æããŸãã
ãµã€ããŸãã¯æ€çŽ¢ã§è¡šç€ºãããåºåã«ã¯ãå€ãã®èšå®ãšãäœæã«äœ¿çšãããéåžžã«è€éãªã€ã³ã¿ãŒãã§ã€ã¹ããããŸãã ãã®è€éãªã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããããã«ãå€ãã®ã³ãŒããèšè¿°ãããŠããŸãã Direct BEMã¹ã¿ãã¯ã§äœ¿çšãããŸãã ãããžã§ã¯ãã¯ãããã¯ã«åå²ãããŠããããããžã§ã¯ãã«ã¯ãããã®ãããã¯ãçŽ800åãããŸããReactã§æžããŠããå Žåã800åã®Reactã³ã³ããŒãã³ãããããããžã§ã¯ããæ³åããŠãã ããã ããã¯éåžžã«å€§ããªãããžã§ã¯ãã§ãïŒ Web Stormã§ãã¡ã€ã«ã®ãªã¹ãã枬å®ããããšãããšããã20ç»é¢ã®é«ããåŸãããŸããã ã»ããããã¯ããããã®ã³ãŒãã§ãã
ãããžã§ã¯ãã¯çµ¶ããå€åããŠããããããã®16人ã®éçºè ã¯æ¯æ¥æ°å件ã®ã³ããããè¡ããåžžã«æ°ããæ©èœãè¿œå ããŠããŸãã ãããŠããããžã§ã¯ãã«æ°ããæ©èœãè¿œå ããŠãããããäœãå£ããªãããšãäœããã®åœ¢ã§ç¢ºèªããå¿ èŠããããŸãã ãããžã§ã¯ããå°ããå Žåãæåãã¹ã¿ãŒã®å©ããåããŠãããè¡ãããšãã§ããŸããã圌ãã¯æ©èœãè¿œå ããŸãã-圌ãã¯ãã¹ããããå®çšŒåäžã§ãã ãŸãããããžã§ã¯ãã¯éåžžã«å€§ãããããæ¯åãã¹ãããäœè£ã¯ãããŸããã
ãã¹ãŠã«å¯ŸããŠèªåãã¹ããäœæããŸãã ãããžã§ã¯ãã«ã¯çŽ7000ã®ãŠããããã¹ãããããã³ãããããšã«å®è¡ãããäœãå£ããŠããªããšããèªä¿¡ãäžããŸãã
éåžžã«å€§ããªãããžã§ã¯ããããå Žåãèªåãã¹ãã¯åãªãåªããè¿œå æ©èœã§ã¯ãªãããšã©ãŒã®çºèŠã«åœ¹ç«ã€å ŽåããããŸãã ããã¯å¿ èŠãªäœæ¥ããŒã«ã§ããããããªãã§ã¯äœæ¥ã§ããŸããã ã³ãŒãã«çŸãããã°ã®æ°ã«drããŸãã
ããã¯ãããžãã¯ãã¯ã©ã¹ããŸãã¯é¢æ°ããã¹ããããšãã®ãã¹ãã®äŸã§ãã
æåã«æºåã¢ã¯ã·ã§ã³ãå®è¡ãã次ã«ç¢ºèªããå¿ èŠãããã¢ã¯ã·ã§ã³ãå®è¡ããŸããæåŸã«ããã®ã¢ã¯ã·ã§ã³ã®çµæãäºæ³ãããã¢ã¯ã·ã§ã³ãšæ¯èŒããŸãã æåŸ ã©ããã§ãªãå Žåããã¹ãã¯å€±æããŸãã
ããããçš®é¡ã®ãã¿ã³ã«ã€ããŠãã€ã³ã¿ãŒãã§ãŒã¹ã®ãã¹ããæžãæ¹æ³ã¯ïŒ åæ§ã«ïŒ
æåã«ããŒãžã«äœããã¬ã³ããªã³ã°ããŠããããã§ãã¯ããå¿ èŠãããã¢ã¯ã·ã§ã³ãå®è¡ããŸããããšãã°ãå€ãå ¥åããŠãã¿ã³ãã¯ãªãã¯ããŸããæåŸã«ãããšãã°ãæ£ãããã©ã¡ãŒã¿ãŒã§ãªã¯ãšã¹ãããµãŒããŒã«éä¿¡ããããããã©ãŒã ã®é©åãªå Žæã«ãããã確èªããŸããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸããã ã¢ã¯ã·ã§ã³ã®ã·ãŒã±ã³ã¹ã¯ãããžãã¯ã®å®æçãªãã¹ããäœæãããšããšåãã§ãã
ãã®åè·¯ã®åé¡ã¯äœã§ããïŒ ãããã®ãã¹ãã«ã¯ãã¹ãŠãã©ãŠã¶ãå¿ èŠã§ãã ããã¯ããŠãŒã¶ãŒãããŒããŒãããäœããå ¥åãããã¿ã³ãã¯ãªãã¯ããã°ã©ãã£ã«ã«ã€ã³ã¿ãŒãã§ã€ã¹ãåããããã°ã©ã ã§ãããã©ãŠã¶ãŒã®çµæã¯ãç»é¢ã«æç»ããããã¯ã»ã«ã§ãã èªåãã¹ããããã人éã®ä»å ¥ãªãã§èªåçã«å®è¡ãããã ã€ã³ã¿ãŒãã§ã€ã¹ã§ã®ãã¹ãã«ã¯ãã©ãŠã¶ãå¿ èŠã§ããããã¯ç§ãã¡ã«ã¯é©ããŠããŸããã
äœãã§ããŸããïŒ ãã®åé¡ã解決ããã«ã¯2ã€ã®æ¹æ³ããããŸãã 1ã€ã¯ãããçš®ã®ãã©ãŠã¶ç®¡çããŒã«ã䜿çšããããšã§ãã Seleniumã¯ããã©ãŠã¶ãŒã§ãŠãŒã¶ãŒãšåãã¢ã¯ã·ã§ã³ãå®è¡ãããã®ã§ããããããã®ã¢ã¯ã·ã§ã³ãããã°ã©ã ã§å¶åŸ¡ã§ããèªåçã«å®è¡ããŸãã 2çªç®ã®æ¹æ³ã¯ããããã¬ã¹ãã©ãŠã¶ãŒã䜿çšããããšã§ãã éåžžã«äººæ°-PhantomJSã 圌ã¯æ»ãã ããããã§ãéåžžã«äººæ°ããã£ãã
SeleniumãŸãã¯å¥ã®åæ§ã®ããŒã«ã䜿çšããŠãã©ãŠã¶ãŒãå¶åŸ¡ãããšãã¹ããŒã ã¯æ¬¡ã®ããã«ãªããŸãã
ãã®ãã§ãã¯ããŒã¯ä»ãã®ã¡ã¢åž³ã¯ãã¹ãã©ã³ããŒã§ãããç»é¢ã§èŠãã³ãŒãã®ãããªãã®ãå転ããããSelenium Webãã©ã€ããŒããšåŒã°ããç¹å¥ãªãã®ã«ã³ãã³ããæäŸããŸãã ããã¯ããã©ãŠã¶ã管çããããã®APIãæäŸãããœãããŠã§ã¢ã©ã€ãã©ãªã§ãã ãããŒãžãéãããããã¿ã³ãã¯ãªãã¯ãããããå ¥åãã£ãŒã«ãã«ããã¹ããå ¥åããããªã©ã®ã³ãã³ãããããŸãã
Selenium Webãã©ã€ããŒã¯ããã®ãã·ã³ãŸãã¯ãªã¢ãŒãã®å®éã®ãã©ãŠã¶ãŒã®ã©ããã§èµ·åããããã«ãŠã£ã³ããŠãæç»ããããã¹ãã§å®è¡ãããã¢ã¯ã·ã§ã³ãèªåçã«è¡ãããŸãã ãã®å ŽåããŠãŒã¶ãŒã¯ãã©ãŠã¶ã䜿çšã§ããŸããã ãã©ãŠã¶ã¯èªååããããœãããŠã§ã¢ã«ãã£ãŠå¶åŸ¡ãããŠããããã®ãŠã£ã³ããŠã§ã¯äœãã§ããªããšããã¡ãã»ãŒãžã衚瀺ãããŸãã ãã®ã¢ãããŒãã¯ããã¹ã¿ãŒã«ââããéåžžã®ãã¹ãã«äŒŒãŠããŸãããã¹ãã¯äººã«ãã£ãŠã§ã¯ãªãããã¹ãã«ãã£ãŠè¡ãããŸãã ããã®ãã¹ãã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
ããã«ã¯ãã©ãŠã¶å€æ°ããããŸãã ç°ãªããã¬ãŒã ã¯ãŒã¯ã§ã¯ç°ãªãæ¹æ³ã§åŒã³åºãããšãã§ããŸãããæ¬è³ªã¯åãã§ããWebãã©ã€ããŒã®Selenium APIã«ã¢ã¯ã»ã¹ãããªããžã§ã¯ãããããŸãã æºåãæ€èšŒå¯èœãªã¢ã¯ã·ã§ã³ãæ€èšŒ-é¢æ°/ã¯ã©ã¹ã®ãã¹ãã«é¢ããã¹ã©ã€ãã«ãã£ããã®ãšåãã¢ã¯ã·ã§ã³ããã¹ãŠãããŸãã ãã©ãŠã¶ã«äœããã¹ãããäŒããã ãã§ãã
ãã®ã¢ãããŒãã®å©ç¹ã¯äœã§ããïŒ ãŸããSelenium Webãã©ã€ããŒã¯ãããŸããŸãªãã©ãŠã¶ãŒã管çããããã®åãæ¹æ³ãæäŸããŸãã ããšãã°ãFirefoxãšChromeã§ãã¹ãããã¹ãããããã«äœ¿çšããŸãã ãã®åŸãIEãŸãã¯ã¢ãã€ã«Safariã§ãã¹ããå®è¡ããå¿ èŠããããšèšãããŸããã IE8ãèšå®ã«è¿œå ãããšãIEã§ãã¹ãã®å®è¡ãéå§ãããŸãã ãã®ããã«ãã¹ãã³ãŒããç·šéããå¿ èŠã¯ãããŸããã åäžãªãã©ãŠã¶ç®¡çã¯å€§ããªãã©ã¹ã§ãã
2çªç®ã®ãã©ã¹ã¯ãã¹ã±ãŒã©ããªãã£ã§ãã ç§ã瀺ããå³ã§ã¯ã3çªç®ã®ã³ã³ããŒãã³ãã¯ãã©ãŠã¶ãŒã§ãã ãã¹ããå®è¡ããŠããã³ã³ãã¥ãŒã¿ãŒã ãã§ãªããå¥ã®ã³ã³ãã¥ãŒã¿ãŒã«ãé 眮ã§ããŸãã ããã«ããã©ãŠã¶ãŒãæèŒããã³ã³ãã¥ãŒã¿ãŒããã®ã¹ããŒã ã«è¿œå ãããããã®ã³ã³ãã¥ãŒã¿ãŒã§å€ãã®ãã¹ãã䞊è¡ããŠããã«å®è¡ã§ããŸãã ããã¯ãæ°çŸãŸãã¯æ°åã®ãã¹ãããããããšãã°1å°ã®ã³ã³ãã¥ãŒã¿ãŒã§1æéåã远跡ãã10å°ã®ã³ã³ãã¥ãŒã¿ãŒãè¿œå ãããã¯ã©ãŠãã§å®è¡ãã10åã§åæ Œããå Žåã«éåžžã«äŸ¿å©ã§ãã ããã¯ãã¹ããé«éåããéåžžã«è¯ãæ¹æ³ã§ãããéåžžã«ç°¡åã«è¡ãããšãã§ããŸãã ãã®åè·¯ã¯éåžžã«ããã¹ã±ãŒãªã³ã°ããŸãã
ãã®ã¢ãããŒãã¯ããŸãæ©èœããããŸããŸãªãã©ãŠã¶ã§å€ãã®ãªãŒãã£ãšã³ã¹ããããããæ€çŽ¢ã§ç©æ¥µçã«äœ¿çšãããŠããŸãã
Seleniumã䜿çšããŠãã¹ããå®è¡ããæ¹æ³ã¯ïŒ
Searchã®ã¡ã³ããŒã¯ãç¹å¥ãªããŒã«HermioneãäœæããŸãã ã ããã¯åãªããã¹ãã©ã³ãã£ãŒã§ã¯ãªããå€ãã®æ©èœããããŸãã ãããããŸã第äžã«ãããã¯å€ãã®ãã©ãŠã¶ãŒã§Seleniumã§ãã¹ããå®è¡ããããŒã«ã§ãã
ã¹ã©ã€ãã«ã¯npmããã±ãŒãžããããŸãã Nightwatchã¯Hermioneãšã»ãŒåãããšãå®è¡ã§ããŸãããæ©èœã¯ãããã«ç°ãªããŸããããã2ã€ã®ããšãèŠãŠã奜ããªãã®ãéžæã§ããŸãã
Seleniumã§ã®ãã¹ãã¯åªããã¢ãããŒãã§ãããæ©èœããå€ãã®äººã䜿çšããŠããŸãã ãããã圌ã«ã¯1ã€ã®éèŠãªå¶éããããŸãã
åäœãã¹ããæžãããšã¯ã§ããŸããã çµ±åã®ã¿ã åäœãã¹ããšçµ±åãã¹ãã®éãã¯äœã§ããïŒ
çµ±åãã¹ãã¯ãã³ã¬ã¯ã·ã§ã³å ã®ãã¹ãŠãæ€èšŒããŸãã ãã¹ãããã€ã³ã¿ãŒãã§ã€ã¹èŠçŽ ã¯ãå éšã§ä»ã®ã€ã³ã¿ãŒãã§ã€ã¹èŠçŽ ã䜿çšã§ããŸããããšãã°ããã©ãŒã ããã¹ãããŠããå Žåããã¿ã³ãå ¥åãã£ãŒã«ããããçš®ã®ãã¹ãããããã©ãŒã ã䜿çšããŸãã ãŸããäŸåé¢ä¿ã«å€éšAPIãå«ããããšãã§ããŸããããšãã°ããããã¯ãŒã¯ããäœããããŒãããããã¹ãã¬ãŒãžããäœããããŒããããã§ããŸããã·ã¹ãã æéãå€éšäŸåé¢ä¿ã§ãã çµ±åãã¹ãã§ã¯ããã¹ãŠããŸãšããŠãã¹ãããŸããããŒãžäžã®ãã©ãŒã ããã¹ãŠã®éšåã§ã¬ã³ããªã³ã°ããããã§ã¢ã¯ã·ã§ã³ãå®è¡ããŸãã
åäœãã¹ãã§ã¯ãèŠçŽ ãäŸåé¢ä¿ããåãé¢ããŠãã¹ãããŸãã å³ã®åç¢å°ã«ã¹ã¿ããé 眮ãããã¹ãäžã«äŸåé¢ä¿ã³ãŒãã¯å®è¡ãããŸããã 代ããã«ãã³ãŒãã¯å°ããªã¹ã¿ãã§ãã 圌ã¯ããã¹ããããã¯ã®ãã§ãã¯å¯Ÿè±¡ã®ããŒã¿ãææ¡ããŸãã
åã¢ãããŒãã¯ãç¬èªã®åé¡ã解決ããŸãã åäœãã¹ãã¯ãDirectã«ãšã£ãŠéèŠã§ãã éåžžã«è€éãªããžãã¯ãæã€éåžžã«è€éãªãããã¯ããããããããçµ±åãã¹ãã§ç¢ºèªããã®ã¯éåžžã«é£ããããã§ãã 倧ããªããŒã¿ã·ãŒããå ¥åã«éä¿¡ãããã¹ãŠã®ãããã¯äŸåé¢ä¿ã®ããŒã¿ãæºåããå¿ èŠããããŸãã å éšã§äœããå£ããå Žåãã©ãã§å£ããã®ããç解ããã®ã¯å°é£ã§ãã ãããŠçµ±åãã¹ãã¯é ãã§ãã
çµ±åãã¹ãã¯ãããŸããããŠããããã¹ããäœæãããã®ããŒã«ãªãã§å¥ã®ãããã¯ããã¹ãããå¿ èŠãããå€ãã®å ŽæããããŸãã
Seleniumã§äœãèµ·ãã£ãŠããŸããïŒ ä»¥äžã¯ãåè·¯ã§å®è¡ãããã³ãŒããå«ããã¡ã€ã«ã§ãã ãã¹ããå®è¡ãããå·ŠåŽã§ãã¹ãã®ã³ãŒããå®è¡ããããã©ãŠã¶ã§å®è¡ãããå³åŽã§ãã¹ãããŒãžã®ã³ãŒããšãã¹ãããã³ãŒããå®è¡ãããŸãã ãããã®éã«ã¯Seleniumã®åœ¢åŒã®äžéå±€ãããããã¹ãã³ãŒãã¯ãã¹ã察象ã®ã³ãŒãã«çŽæ¥ã¢ã¯ã»ã¹ã§ããŸããã ãã¹ãã³ãŒãã¯ããã¹ãŠã®ã¹ã¿ããé 眮ããŠããã¹ãã³ãŒããäŸåé¢ä¿ããåé¢ããããšã¯ã§ããŸããã Selenium APIãšã®ã¿å¯Ÿè©±ã§ããŸããäœããã¯ãªãã¯ããŸãã äœããã®JSãå®è¡ã§ããŸãããããã§ãçŽæ¥ã¢ã¯ã»ã¹ããããšã¯ã§ããŸããã ãã®ãããåäœãã¹ãã¯èšè¿°ã§ããŸããã
ãŸãã¯ããã®ãã¹ãã³ãŒãããã©ãŠã¶ãŒåŽã«ã転éãããã¹ãã©ã³ããŒã«ãã¹ããã©ã®ããã«é²ããã«é¢ããããŒã¿ãã¬ããŒãããŠããã§ãã¯ããŒã¯ã衚瀺ããŠããã§ã¯ãã¹ããããšãã§ããŸãã ãã ãããã®APIã¯äžæ¹åã§ãããããSeleniumã§ãããè¡ãããšã¯ã§ããŸããã ãã¹ãããäœãããã©ãŠã¶ã«èŠæ±ããããšã¯ã§ããŸããããã©ãŠã¶ãããããå¶åŸ¡ããããã°ã©ã ã«äœããéä¿¡ããããšã¯ã§ããŸããã
çµè«ïŒYandex.Directã§ã¯åäœãã¹ããå¿ èŠã§ãããSeleniumã§ã®ã¢ãããŒããåäœãã¹ãã«äœ¿çšããããšã¯ã§ããŸãããçµ±åãã¹ãã«ã®ã¿äœ¿çšã§ããŸãã
2çªç®ã®ã¢ãããŒã-ãããã¬ã¹ãã©ãŠã¶ãŒã§ã®ãã¹ããèŠãŠã¿ãŸãããã
ãããã¬ã¹ãã©ãŠã¶ã¯éåžžã®ãã©ãŠã¶ãšåãã§ãããäœæ¥äžã¯ç»é¢ã«äœã衚瀺ãããŸããã ãããã¬ã¹ãã©ãŠã¶ãã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ãšããŠèµ·åã§ããŸãã ããã§ãããŒãžãéããããã¹ãŠã®CSSãJSãå®è¡ãããŸãããç»é¢ã«ã¯äœã衚瀺ãããŸããã
ãã®å Žåããã©ãŠã¶ã¯ããçš®ã®APIãä»ããŠã®ã¿å¶åŸ¡ã§ããŸããããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã¯ãããŸããã ããã¯ãSeleniumãš1ã€ã®ããã«ã®ãã©ãŠã¶ã«äŒŒãŠããŸãããAPIã¯ãã©ãŠã¶èªäœã®å éšã«å®è£ ãããŠãããå¥åã®å€éšã³ã³ããŒãã³ãã§ã¯ãããŸããã
APIããã©ãŠã¶ãŒã®å èã«ã¢ã¯ã»ã¹ã§ããããšãããããŸããã ååãšããŠããããã¬ã¹ãã©ãŠã¶ãŒAPIã¯ãSeleniumãããã¯ããã«å€ãã®æ©èœã»ãããæäŸããŸãã ç¹ã«ãããŒãžã®èªã¿èŸŒã¿ãããŒãžãšã©ãŒãªã©ã®ãã©ãŠã¶ãŒã€ãã³ãããªãã¹ã³ãããããããã¯ãŒã¯ãžã®èŠæ±ãã€ã³ã¿ãŒã»ããããããã³ã³ãœãŒã«ãžã®åºåãã€ã³ã¿ãŒã»ãããããã§ããŸãã ãããã®ããšã«ãããç§ã話ããŠãããªãã·ã§ã³ãå¯èœã«ãªããŸãããã¹ãã³ãŒãããã©ãŠã¶åŽã«è»¢éããŠããã¹ãã©ã³ããŒã«ãã¹ãã®å®è¡æ¹æ³ã«é¢ããæ å ±ãéä¿¡ããŸãã ãã®ã¢ãããŒããéåžžã«äººæ°ããããå€ãã®äººã ãåäœãã¹ããå¿ èŠãšããŸãã çŽ3幎é䜿çšããŠããŸãã
æè¿ãŸã§ãæ£åžžã«æ©èœãããã©ãŠã¶ã¯PhantomJSã ãã§ããã
æ¬ é¥ããããŸãã-ã¡ã¢ãªãæµããŠããããããã¬ããã¹ãããããã°ããããã«åå ããããšã¯å°é£ã§ã-ããããååãšããŠãããã¯æ£åžžã«åäœãããã©ãŠã¶ã§ãã 圌ã¯å éšã«Webkitãæã£ãŠããŸããããã¯ãã¹ãŠæ©èœãã2幎é䜿çšããŸããã
ããã¯Googleã°ã«ãŒãã®ã¹ã¯ãªãŒã³ã·ã§ããã§ããæåŸã®PhantomJSéçºè ã§ããVitaliy Slobodinã次ã®ããã«èšã£ãŠããŸãã ãŸããChromeã調ã¹å§ããããã«åãæ¿ããŸããã
ããã¯ããããã¬ã¹ã¢ãŒãã§Chromeãå¶åŸ¡ããããã®PuppeteerãšããããŒã«ã®ããŽã§ãã æã人圢垫ïŒãæã人圢垫ããšèš³ãããŠããŸãïŒã¯Chrome Dev ToolsããŒã ãéçºããŠããŸããããµããŒããæ念ããªããšãã確信ããããŸãã ããã¯NodeJSããã±ãŒãžã§ãããJS APIãåããŠããŸããæãã¯ãŒã«ãªããšã¯ãChromeãäŸåé¢ä¿ãšããŠé 眮ããããšã§ãã ãã©ãŠã¶ã§äœããå®è¡ããããã«ãã©ãŠã¶ãåå¥ã«ã€ã³ã¹ããŒã«ããå¿ èŠã¯ãããŸããã npm installãæžããŠããã«åäœããŸããã
ç§ãã¡ã¯åœŒã®æ¹åãèŠãŠã¿ãŸãã å¯äžã®åé¡ã¯ããã¹ããšHeadless Chromeã暪æããããŒã«ããªãã£ãããšã§ãã PhantomJSã«ã¯ããã®ãããªããŒã«ããããŸãããé·ãéååšããŠããããããã¬ã¹ã¯ããŒã ãç»å Žããã°ããã§ãããŒã«ã¯ãããŸããã§ããã
mocha-headless-chromeããŒã«ãäœæããŸããã
æ€çŽ¢ããã空æ³ãå°ãªãïŒåœŒãã®æ¥œåšã¯HermioneãšåŒã°ããç§ãã¡ã®æ¥œåšã¯mocha-headless-chromeãšåŒã°ããŠããŸãã 6ãæé䜿çšããŸãããåäœããŸãã äŸãšããŠå°ããªãããžã§ã¯ãã䜿çšããŠããããã©ã®ããã«èµ·ãããã瀺ããŸãã ïŒã¬ããŒãããã®ãã¢ã¯ãã¡ãã§ã -ããããšããïŒ
ãã¹ããããžã§ã¯ãã§ã¯ã1ã€ã®ãã¡ã€ã«ã¯test-form.jsã§ãã ããã¯æ€çŽ¢ãã©ãŒã ã§ãããå ¥åãšãã¿ã³ããããšæšæž¬ããã®ã¯ç°¡åã§ãã SearchFormã¯ã©ã¹ã«ã¯ãã»ãŒReactã®ãããªrenderã¡ãœããããããããŒãžã«ãã©ãŒã ãå ¥åããã¿ã³ãè¿œå ããŸãã ããã«ã圌ã¯ãã¿ã³ã®ã¯ãªãã¯ã«ãµãã¹ã¯ã©ã€ããããŠãŒã¶ãŒããã¿ã³ãã¯ãªãã¯ãããšãAjaxãªã¯ãšã¹ããäœæãããã©ãŒã ã®ã³ã³ãã³ããexample.comã«éä¿¡ãããã®åŸãã©ãŒã ãã¯ãªãŒãã³ã°ããŸãã
class SearchForm { onClick(e) { e.preventDefault(); let xhr = new XMLHttpRequest(); xhr.open("GET", "http://example.com"); xhr.send(new FormData(this.form)); this.form.reset(); } render(parent) { this.form = document.createElement('form'); this.form.innerHTML = `<input type="text" name="query" /> <input type="button" value="" />`; this.input = this.form.querySelector('input[type=text]'); this.button = this.form.querySelector('input[type=button]'); this.button.addEventListener('click', this.onClick.bind(this)); parent.appendChild(this.form); } destroy() { this.form.remove(); } }
ãã®ããã®ç°¡åãªãã¹ããæžããŸãããã testsãã©ã«ããŒããã®äžã®ãã¡ã€ã«test.jsã ããã«ã¯ãã¹ãã¯ãããŸããããåãã¹ãã®ååŸã«å®è¡ããå¿ èŠãããã¢ã¯ã·ã§ã³ãšã¢ã¯ã·ã§ã³ã®ã¿ã説æããŸãã
const assert = chai.assert; describe(' ', function() { let searchForm, server; beforeEach(function() { searchForm = new SearchForm(); searchForm.render(document.body); server = sinon.createFakeServer({ respondImmediately: true }); }); afterEach(function () { searchForm.destroy(); server.restore(); }); });
åãã¹ãã®åã«ããã©ãŒã ãããŒãžã«è¿œå ããåãã¹ãã®åŸã«ãããåé€ããŸãã
ãŸããåãã¹ãã®åã«ãAjaxãªã¯ãšã¹ãã®ã¹ã¿ããäœæããŠããã¹ãã§ã©ã®ãªã¯ãšã¹ããè¡ããããã確èªã§ããããã«ããŸãã ãããŠãåãã¹ãã®åŸããã®ã¹ã¿ãããªã»ããããŸã-èªåã§ã¯ãªãŒã³ã¢ããããŸãã
ãã¹ããã¡ã€ã«JSãäœæããŸãããããŸã ãã¹ãã¯ãããŸããããŸããªãäœæããŸãã
ããããæåã«ããã©ãŠã¶ã§éããã¹ãããŒãžãäœæããŸãããã
testsãã©ã«ããŒã§ãtest.htmlãã¡ã€ã«ãäœæããŸãã ããã§ã¯è€éãªããšã¯äœããããŸããã3ã€ã®ã©ã€ãã©ãªãæ¥ç¶ããŸããMochaã¯ãã¹ããã¬ãŒã ã¯ãŒã¯ã§ããã誰ããããã«æ £ããŠãããšæããŸãã Sinonã¯ãããããçš®é¡ã®ã¹ã¿ããèªåçã«äœæããŠããããã¯ãäŸåé¢ä¿ããåé¢ã§ããã©ã€ãã©ãªã§ãã ãããŠãããããçš®é¡ã®ã¢ãµãŒã·ã§ã³ãåããchaiã©ã€ãã©ãªãŒã¯ãããŸããŸãªãã¹ããã§ãã¯çšã®APIãæäŸããŸãã
<html> <head> <meta charset="utf-8"> <link href="../node_modules/mocha/mocha.css" rel="stylesheet" /> <script src="../node_modules/mocha/mocha.js"></script> <script src="../node_modules/sinon/pkg/sinon.js"></script> <script src="../node_modules/chai/chai.js"></script> <script>mocha.setup('bdd');</script> </head> <body> <div id="mocha"></div> <script src="../src/search-form.js"></script> <script src="../tests/test.js"></script> <script>mocha.run();</script> </body> </html>
ããã3ã€ã®ã©ã€ãã©ãªãæ¥ç¶ãããã©ãŒã ã®ã³ãŒããsearch-formãæ¥ç¶ããäœæãããã¡ã€ã«ããã¹ãã«æ¥ç¶ããŸããã æåŸã«ããã¹ããå®è¡ããããã«mocha-runã³ãã³ããåŒã³åºããŸããã
ãã©ãŠã¶ãŒã§ããŒãžãèµ·åãããã¹ãŠãæ£åžžã§ããããšã確èªããŸãã ããŒãžãéããããã¹ãããŒãã§ããããšãäºæ³ãããŸãã ããã€ãã®ãã¹ããæžããŸãããã ãã¹ãã§ã¯ããã¿ã³ãã¯ãªãã¯ãããšãæ£ããã¢ãã¬ã¹ãžã®Ajaxãªã¯ãšã¹ãããµãŒããŒã«éä¿¡ãããããšã確èªããŸãã ããã¯ãæåã«äœæããsearchFormã§ãã ãã®ãã¹ãã§ã¯ããã©ãŒã ã«ããŒã¿ãå ¥åãããã¿ã³ãã¯ãªãã¯ããŠããããµãŒããŒå€æ°ã®ã¹ã¿ãã䜿çšããŠãæåŸã®ãªã¯ãšã¹ãã«æ£ããURLãå«ãŸããŠããããšã確èªããŸãã
it(' ', function() { // searchForm.input.value = ''; // searchForm.button.click(); // assert.equal(server.lastRequest.url, 'http://example.com'); });
ãã©ãŠã¶ã§ããŒãžãèŠãŠã¿ãŸããããããŒãžãæŽæ°ããã1ã€ã®ãã¹ãã«åæ ŒããããšãããããŸãã ãã©ãŠã¶ã«Ajaxãªã¯ãšã¹ããè¡ãã³ãŒãããããŸãã ãã¹ãäžã«ãã®èŠæ±ãè¡ããªãããã«ã¹ã¿ããé 眮ããèŠæ±ãæ£ãããã©ã¡ãŒã¿ãŒã§è¡ãããããšã確èªããŸããã
ãã¹ãŠãHeadless Chromeã§å®è¡ããŸãããã
npm install mocha-headless-chrome
æ¯åæžã蟌ãŸãªãããã«ããã¹ãã³ãã³ããpackage.jsonã«è¿œå ããŸãã mocha-headless-chromeããã±ãŒãžãã€ã³ã¹ããŒã«ãããšãåãååã®ãŠãŒãã£ãªãã£ãè¿œå ãããŸãã 圌女ã¯-fãªãã·ã§ã³ïŒãã©ãŠã¶ã§éãããã¹ãããŒãžãžã®ãã¹ïŒãæž¡ãå¿ èŠããããŸãã
{ ... "scripts": { "test": "mocha-headless-chrome -f tests/test.html" }, ... }
ä»ãnpm testãå®è¡ãããšããã¹ãŠãæ©èœããã¯ãã§ãã
äŸåé¢ä¿ãäœæããChromeãç¬èªã«ããŠã³ããŒãããnode_modulesãã©ã«ããŒã«ããŒã«ã«ã«é 眮ããŸããã 次ã«ãã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ãšããŠååã§åŒã³åºãã ãã§ãfãã©ã¡ãŒã¿ãŒãä»ããŠãã¹ãããŒãžãæž¡ããŸãã ãã¹ãã«åæ ŒããŸãããããã¯ç§ãã¡ãæžãããã¹ãã§ãã
Ajaxãªã¯ãšã¹ããéä¿¡ããåŸãæ€çŽ¢ãã©ãŒã ã®å€ãã¯ãªã¢ããããšã確èªããå¥ã®ãã¹ããè¿œå ããŠã¿ãŸãããã
it(' ', function() { // searchForm.input.value = ''; // searchForm.button.click(); // assert.equal(searchForm.input.value, ''); });
ãã©ãŠã¶ã§ã衚瀺ãããããšã確èªããŸãã 次ã«ãã¿ãŒããã«ã§å®è¡ããŸãã 2ã€ã®ãã¹ãããããããããå®è¡ããããšãããã©ãŠã¶ãŠã£ã³ããŠã¯è¡šç€ºãããŸããã§ããã ãããå§ãã人ã¯åœŒã®æã§äœãããŸããã§ããããã¹ãŠãå®å šã«èªåçã«è¡ããŸãã
ãã®ããŒã«ã¯çŽ6ãæé䜿çšãããŠããŸãã PhantomJSã§ä»¥åã«æ©èœããŠãã7000ãã¹ãã¯ãHeadless Chromeã§ãåé¡ãªãæ©èœãå§ããŸããã ãã¹ãã®å®è¡ã30ïŒ å éãããŸããã ãã®äºã¯å€éšã®npmã§å©çšå¯èœã§ããããªãã¯ãããå©çšããããšãã§ããŸãã ãã§ã«æã«5,000åã®ããŠã³ããŒãããããŸããã€ãŸããYandexã®å€éšã§ããã䜿çšããŠãã人ãããŸãã