Selenium ä¸ç´è ã«ãªãã ï¼å¤æ°ï¼XPathï¼JavaScriptãï¼ãã¹ãã±ã¼ã¹ä¸ã§å©ç¨ããæ¹æ³ï¼
å帰ãã¹ããã¼ã«ãSeleniumãã®ä¸ç´ Tipsã
åç´ã®ä½¿ãæ¹ã«ã¤ãã¦ã¯
ä»ããï¼å㧠selenium ã®ä½¿ãæ¹ã身ã«ä»ããã(å帰ãã¹ãèªåå)
http://language-and-engineering.hatenablog.jp/entry/20081016/1224080409
seleniumã主ãªã³ãã³ãä¸è¦§
http://language-and-engineering.hatenablog.jp/entry/20081016/1224123118
ã§å ¥éã®ãã¨ã
Seleniumã®ãã便å©ãªä½¿ãæ¹ã¨ãã¦ï¼ä¸è¨ã§
- ï¼ï¼ï¼å¤æ°ã®ä½¿ãæ¹
- ï¼ï¼ï¼XPathã®ä½¿ãæ¹
- ï¼ï¼ï¼ãã¹ãã±ã¼ã¹ä¸ã¸ã®javascriptã®åãè¾¼ã¿
- ï¼ï¼ï¼Ajaxã¢ããªã®ãã¹ãæ¹æ³
ãå¦ã¶ã
ã¾ãï¼ã¾ã¨ããæ²è¼ããã®ãã¨ã§ï¼å®éã®ãã¹ãã§ã©ãå½¹ç«ã¤ã®ã詳ãã解説ããã
ã¾ã¨ã
è¦ç´ ã®æå®æ¹æ³ã¾ã¨ã
ElementLocatorï¼è¦ç´ ãæå®ããããã®æååï¼ã«ã¯DOM IDï¼xpath, document.ãï¼javascript{"DOM"+"ID";}, ${varname}ã使ããã
- verifyElementPresent fuga
- verifyElementPresent //input[@id="fuga"]
- verifyElementPresent document.getElementById("fuga")
- verifyElementPresent document.getElementsByTagName("input")[0]
- verifyElementPresent javascript{ "fu" + "ga"; }
- store fuga elem_id
verifyElementPresent ${elem_id}- store fuga elem_id
verifyElementPresent javascript{ storedVars["elem_id"]; }
å¤æ°ã®ã¾ã¨ã
æ ¼ç´
- store å¤ å¤æ°å
- storeValue è¦ç´ å¤æ°å
- getEval storedVars["å¤æ°å"] = å¤;
åç §
- ${å¤æ°å}
- javascript{ storedVars["å¤æ°å"] }
JSã®ã¾ã¨ã
åãè¾¼ã¿
- javascript{ ã³ã¼ã }
- getEval ã³ã¼ã
- verifyEval ã³ã¼ã å¤
JSã³ã¼ãä¸ã§å©ç¨ã§ããç¹æ®ãªãªãã¸ã§ã¯ã
- this : seleniumå ã®å°ç¨ç°å¢ãæã
- this.page().getDocument() : ãã¹ãä¸ã®ãã¼ã¸ã®document
- this.page().getCurrentWindow() : ãã¹ãä¸ã®ãã¼ã¸ã®window
- storedVars : å¤æ°æ ¼ç´ç¨ã®ããã·ã¥
- selenium : seleniumã³ã¢ã®ããããæä½ã¨ãã§ãããªãã¸ã§ã¯ã
以ä¸è§£èª¬ã
ï¼ï¼ï¼Seleniumã§å¤æ°ã使ã
ä¸è¨ã§ã¯ãããªHTMLãæ³å®ã
ã¦ã¼ã¶åï¼<input type="text" id="input1"> ãããã¤ï¼<input type="text" id="input2">
å¤æ°ããã®ã¾ã¾ä½¿ã
storeç³»ã®ã³ãã³ãã§ä¿åã${}ã§å¤æ°å¼ã³åºãã
ãinput1ãã¨ããIDã®DOMè¦ç´ ã«ï¼ãuser1ãã¨å ¥åãããã
# å ¥åå 容ã®æååã¨ãã¦ï¼str_usernameã¨ããå¤æ°ã«æ ¼ç´ããuser1ã¨ããå¤ãå©ç¨ store user1 str_username type input1 ${str_username} # å ¥å対象ã®è¦ç´ IDã¨ãã¦ï¼input_usernameã¨ããå¤æ°ã«æ ¼ç´ããinput1ã¨ããå¤ãå©ç¨ store input1 input_username type ${input_username} user1
å
·ä½çãªãã¹ããã¼ã¿ãè¦ç´ IDã¯ï¼ãã¹ãã±ã¼ã¹ã®å
é ã§å¤æ°ã¨ãã¦å®£è¨ãã¦ããã°ï¼ããã¨ããæã«å¤æ´ã¯ä¸ç®æã§ããã
ã¤ã¾ãï¼ãã¹ãã±ã¼ã¹ã®ä¿å®ã容æã«ãªãã
ãªãï¼${varname}ã®é¨åã¯javascript{ storedVars["varname"]; }ã¨ãã¦ãåãã
storedVarsã¯ï¼å¤æ°ãæ ¼ç´ããããã®é£æ³é
åã
å¤æ°ãJavaScriptã§å å·¥ãã¦ä½¿ã
ãinput1ãã¨ããè¦ç´ ã®valueã«ï¼ååãå ¥ã£ã¦ããã¨ããã
ããããHello, ã!ãã®ããã«å å·¥ãã¦ããï¼input2ã®valueã«å ¥åãããã
storeValue input1 str_username type input2 javascript{ "Hello, "+storedVars["str_username"]+"!"; }
JavaScriptããå¤æ°ãåç §ããã°ï¼ä¿ç®¡ããå¤ã好ããªããã«å å·¥ã§ããã
ãä»æ¥ã®æ¥ä»ãã¨ããä¹±æ°ãã¨ããæ±ãå ´åï¼ãã¹ãã±ã¼ã¹ä¸ã§åºå®å¤ãæ¸ããã¨ã¯ä¸å¯è½ï¼å¤ãäºæ¸¬ãããã¨ãã§ããªãï¼ã
ã ãããã®ããã«
- Webãã¼ã¸ä¸ã«ãã®æç¹ã§è¡¨ç¤ºããã¦ããå¤ãè¦ã¦ï¼
- ãããå¤æ°ã«æ ¼ç´ãã¦ï¼
- ä»ã®å¤ã¨æ¯è¼
ã¨ãã風ã«ãã¦ï¼æè»ã«å¯¾å¦ã§ããã
ï¼ï¼ï¼Seleniumã§XPathã使ã
ã·ã³ãã«ãªHTMLãªãã°ï¼DOM IDã ãã§ãã¹ãå¯è½ã ã
ã§ãï¼åçã«å¤åãããããªWebãã¼ã¸ã®å ´åï¼åè¦ç´ ã«IDãæ¯ãã¥ãããããã«
- ç¹å®ã®CSSã¯ã©ã¹ã§ãããã㪠n çªç®ã®divè¦ç´
- IDããhoge_ãã§å§ã¾ãå ¨è¦ç´
ã¿ãããªæå®ã¯ã§ããªãã
ãããï¼XPathãªãå¯è½ã
jQueryã®CSSã»ã¬ã¯ã¿ã®ããã«ï¼å¼·åã«è¦ç´ é¸æã§ããã
âãªã®ã§
- Webãã¼ã¸ãè¤éãªæ§é ã§ããã¹ããæããªã
- ãã¹ãã®ããã«å ¨é¨IDãæ¯ããªããã¦ããï¼ã¿ãããªäºãéçºè ã»ãã¶ã¤ãã»ãã¹ã¿ã¼éã§èª¿æ´ãã¦åããªãã¦æ¸ãã
ã¨ããã¡ãªãããããããã ï¼è¨æ³ãè¦ããå¿
è¦ããããã
XPathã®åºç¤ç¥è
ç¬ç¿ã«å½¹ç«ã¤ãããªWebãã¼ã¸ã¯é常ã«å°ãªãã以ä¸åèURL
XMLãã¹è¨èª (XPath) Version 1.0
http://www.doraneko.org/xml/xpath10/1...
ä»æ§æ¸ãããã ãèªãã§ç解ããã®ã¯ä¸å¯è½
JavaScript-XPath ããªãªã¼ã¹ãã¾ããï¼ãããããªãã XPath ã使ããï¼ï¼è§£èª¬ä»ãï¼
http://d.hatena.ne.jp/amachang/200711...
ãã¼ãã·ã¼ãæ
XPathå ¥éãå®ç¨ä¾
http://d.hatena.ne.jp/javascripter/20...
- javascripteræ°ã«ãããµã³ãã«ã®åæ
XPathã®å¦ç¿æ¹æ³ã¨ãã¦ã¯ï¼FireBugã®ã³ã³ã½ã¼ã«ã§ãªã¢ã«ã¿ã¤ã ã§ç¢ºããã¦ã¿ãï¼ã¨ããã®ãæãè¯ãæ¹æ³ã ããã
FireBugãéãï¼ã³ã³ã½ã¼ã«ã®ä¸çªä¸ã®ãï¼ï¼ï¼ãã¨æ¸ãã¦ããè¡ã«
$x("//*")
ã¨å ¥åãï¼ã¨ã³ã¿ã¼ãã¼ãæ¼ä¸ãããã¨ï¼è¡¨ç¤ºä¸ã®ãã¼ã¸ã®å ¨è¦ç´ ãåæãããã
ä»åº¦ã¯
$x("//td/a[@href]")
ã¨ããã°ï¼tdè¦ç´ ã®åè¦ç´ ã§ãããããªaãªã³ã¯è¦ç´ ããã¹ã¦åæãããã
$x( XPathè¨æ³ )ãã®ããã«ãã¦ï¼
- ãã®è¨æ³ãå®éã«ã©ã®è¦ç´ ãç¾ãã
- æ£ããè¨æ³ã
確ãããããã¨ããããã ã
çç¥è¨æ³ãè¦ããã°ï¼è¦ç´ æå®ã¯çããªãã
ä¾ãã°ï¼
- //table[@id="hoge"]/tbody/tr[1] ã¨æ¸ã代ããã«ï¼
- //table[@id="hoge"]//tr[1] ã¨æ¸ãã¦ããã
â»//ã¯ãããèªèº«ãï¼ã¾ãã¯åå«å ¨ä½ã®ä¸ããé¸ã¶ãã¨ããæå³ã
注æç¹ã¨ãã¦ï¼ãã¼ãã»ããï¼ãã¼ãã®é åï¼ã®æå®ã¯ééããããã
ãç¾å¨ã®ãã¼ã¸å ã«åå¨ããï¼ã¤ãã®ãªã³ã¯ããæå®ãããå ´åï¼
$x("//a[@href][2]")
ã§ã¯ã ãããä½ãã®è¦ªè¦ç´ ã®ï¼çªç®ã®åè¦ç´ ã¨ãã¦åå¨ãã¦ããaè¦ç´ ãããã¹ã¦åæããã¦ãã¾ãã
ãããã«
$x("descendant::a[@href][2]")
ã¨ããã°OKã
XPathã«ããã//*ã¨descendant::*ã®éã
http://d.hatena.ne.jp/os0x/20080711/1...
Seleniumãã¹ãã±ã¼ã¹å ã§ï¼Xpathã§è¦ç´ ãæå®ãã
- //
- xpath=
ã®ããããã§å§ããã¨ï¼xpathè¨æ³ã¨ãã¦è§£éãããã
Seleniumã§ã¯ï¼è¦ç´ ã®åå¨å¤å®ã®ã¨ãã«XPathã便å©ã
å¤å®ãããè¦ç´ ã«ç¹å®ã®classãæããã¦ããã¦ï¼ããããã¯ã©ã¹ã®è¦ç´ ãåå¨ãããã©ãã確ãããã°ããã
#hogeã¨ããclassã®spanè¦ç´ ãåå¨ãããã¨ãç¢ºèª assertElementPresent //span[@class="hoge"] #hogeã¨ããclassã®spanè¦ç´ ããï¼åãåå¨ãããã¨ãç¢ºèª verifyElementPresent xpath=descendant::span[@class="hoge"][1] verifyElementPresent xpath=descendant::span[@class="hoge"][2]
ã¤ã¾ãï¼XPathãå©ç¨ãã¦ï¼getElementsByClassNameãã¦ããã
ã¡ãã»ã¼ã¸ãæè¨ã®è¡¨ç¤ºç¢ºèªãªã©ã§ä¾¿å©ã
ï¼ï¼ï¼è¦ç´ ãå¤ã®æå®ä»¥å¤ã«ãï¼JavaScriptãå©ç¨ãã¦ã¿ãã
ï¼ï¼ï¼ã§æ¢ã«è¦ãã¨ããï¼javascript{ã}ã«ãã£ã¦è¦ç´ IDã¨ãå¤ã¨ããæå®ã§ããã
ãã以å¤ã®æ¹æ³ã§ãï¼JSã³ã¼ããåãè¾¼ã¿å¯è½ã
Seleniumã®ãã¹ãã±ã¼ã¹ä¸ã§ï¼ä»»æã®JavaScriptã³ã¼ããå®è¡ããã
# hogeã¨ã¢ã©ã¼ã表示 getEval alert("hoge"); # ãã¼ã¸å ä»»æè¦ç´ ã®innerHTMLãã¢ã©ã¼ã表示 getEval alert( this.page().getDocument().getElementById("div_content").innerHTML );
ç¹ã«å¾è
ã¯ï¼ãã¹ãã®éä¸ã§ãã¼ã¸å
ã®ç¶æ³ã確ããããã¨ãã§ãã¦ä¾¿å©ã
Seleniumå JavaScriptå®è¡ç¨ã³ãã³ããgetEvalã
http://colo-ri.jp/develop/2008/04/sel...
ãªãï¼
- this.page().getDocument()ã¯ï¼ãã¹ã対象ã®ãã¼ã¸ã®documentãæãã
- åãªããdocumentãã¯ï¼getEvalã®ãããªJSã³ã¼ãä¸ã§ã¯ï¼Seleniumã®ãã¹ããã¼ã«ã®ããç»é¢ãåå¾ããã¦ãã¾ãã
windowã«ã¤ãã¦ãåãã§ï¼
- this.page().getCurrentWindow()ã¯ï¼ãã¹ã対象ã®ãã¼ã¸ã®windowãæãã
- åãªããwindowãã¯ï¼Seleniumã®ãã¹ããã¼ã«ã®ããç»é¢ãæãã¦ãã¾ãã
ãã£ã¦ï¼ãããã¹ã対象ã®ãã¼ã¸å ã§$()ã¨ããé¢æ°ãå®ç¾©ããã¦ããã°ï¼this.page().getCurrentWindow().$()ã«ãã£ã¦å¼ã³åºããã
ã¾ãï¼user-extensions.jsä¸ã«é¢æ°ãå®ç¾©ãã¦ããã°ï¼ãã¹ãã±ã¼ã¹å ã§ãã®é¢æ°ãå¼ã³åºããã¨ãã§ããã
Seleniumã«å¤é¨JavaScriptãã¡ã¤ã«(.js)ãèªã¿è¾¼ãã§é¢æ°ãå®è¡ãã
http://colo-ri.jp/develop/2008/04/sel...
ãããï¼ããã¯ãã¾ãå¾çã§ã¯ãªãã ããããã¹ããã¼ã«ã«æãå ãããï¼ãã¹ãã®ãã¹ããããªãããªããªãã®ã§ã»ã»ã»
å¤ã®é«åº¦ãªæ¯è¼
verifyEvalã¨ãassertEvalã使ãã°ï¼JavaScriptãä»ãã¦è¤éãªAssertionãå®ç¾ã§ããã
ãã¨ãã°ä¸è¨ã®ãããªHTMLãæ³å®ï¼
ãããã®æ¥ä»ã¯<span id="hoge">2009å¹´1æ1æ¥</span> <span id="fuga">2009/1/1</span>ã®è¡åäºå®ï¼ãã
ãã©ã¼ãããã¯ç°ãªããã©ãï¼åãå¤ãå
¥ã£ã¦ãã¦ã»ããã
ãããï¼ãã¹ããå®è¡ããæ¥ä»ãã ãã§ï¼å®éã«ç»é¢ä¸ã«è¡¨ç¤ºãããæ¥ä»ãå¤ãã£ã¦ãã¾ãã
ã¨ãããããªå ´åã
# ã¾ãspanã®ä¸èº«ãå¤æ°ã«æ ¼ç´ã㦠storeText hoge str_date1 storeText fuga str_date2 # äºãã«æ¯è¼ verifyEval storedVars["str_date1"].match(/(.*)å¹´(.*)æ(.*)æ¥/);RegExp.$1+"/"+RegExp.$2+"/"+RegExp.$3; ${str_date2}
hogeã®ã»ããå å·¥ãã¦ï¼fugaã«çãããªããã©ããããã§ãã¯ãã¦ããã
ãªãï¼ã³ã¼ããããã¯å ã§æå¾ã«è©ä¾¡ããå¤(ããã§ã¯RegExp.$1+"/"+RegExp.$2+"/"+RegExp.$3)ãï¼æ¯è¼ã®ããã«å©ç¨ããããï¼Rubyã£ã½ãï¼
ããã¡ãã£ã¨ç°¡åãªãµã³ãã«ï¼
# ãã¼ã¸ã®ã¿ã¤ãã«ãå¤æ°ã«ä¿åãã¦ãã store javascript{this.page().getDocument().title} fuga # å¤æ°ã¨ãã¼ã¸ã®ã¿ã¤ãã«ãæ¯è¼ãï¼å½ç¶ãªããäºãã«çããï¼ verifyEval this.page().getDocument().title ${fuga}
æ¸ãæ¹ã«ã¯ããããå¹ ãããã
ããå¤æ°ã®ä¸èº«ã«ã¤ãã¦ï¼æååã®é·ãã確ããããå ´åã¯
# æ°å¤ã¨æ¯è¼ verifyEval storedVars["hoge"].length 5 # çå½å¤ã¨æ¯è¼ verifyEval storedVars["hoge"].length==5 true
ã®ããã«ï¼ï¼éãã®æ¸ãæ¹ãã§ããã
ãã®ããã«Seleniumã®ä¸çã«JavaScriptãæã¡è¾¼ããã¨ãã§ããã°ï¼ãããã¨ã¯ããããæ¾é¡ã«ãã¹ãã§ããã¯ãã
ï¼ï¼ï¼Ajaxã¢ããªã®ãã¹ã
div_ajaxã¨ããè¦ç´ å ã«ï¼Ajaxã§HTMLãèªã¿è¾¼ãã¨ãããã
ãã®èªã¿è¾¼ã¿ãçµäºããã¾ã§å¾ ã¤ã«ã¯ï¼waitForããã³ãã³ãã使ãã
# è¦ç´ ã®ä¸ã«ãèªã¿è¾¼ã¿å®äºãã¨ããæååãå«ã¾ããç¶æ³ã«ãªãã®ãå¾ ã¤ã10ç§ã§ã¿ã¤ã ã¢ã¦ãã waitForCondition var val=selenium.getText("div_ajax");val.match(/èªã¿è¾¼ã¿å®äº/)!=null; 10000
ç¹å®ã®è¦ç´ ãèªã¿è¾¼ã¿ã«ãã£ã¦åºç¾ããã®ãå¾
ã£ã¦ãè¯ãã
# èªã¿è¾¼ã¿ã«ãã£ã¦h3è¦ç´ ãç¾ããã®ãå¾
ã¤
waitForElementPresent xpath=//h3[@class="content_loaded"] 10000
ãããã¯ï¼ãã¼ãã£ã³ã°ä¸ã®ã¯ã«ã¯ã«åãç»åãæ¶ããã®ãï¼waitForElementNotPresent ã§æ¤åºãã¦ãOKã
Seleniumã§Ajaxã¢ããªã±ã¼ã·ã§ã³ããã¹ããã
http://www.infoq.com/jp/articles/test...
ä½ãããã§ãã¯ããã®verifyXxxxãassertXxxxããããªããéåæå¹æããã¹ãããããã®waitForXxxxãå¿ ãåå¨ãã¾ãã
以ä¸ã®ãã¨ãç¥ã£ã¦ããã°ï¼ãããããã¨ã¯ã»ã¼Seleniumã§å®ç¾ã§ããã ããã
é¢é£ããè¨äºï¼
"Excelenium"ï¼ã¨ã¯ã»ã¬ãã¦ã ï¼ã§ï¼å¿«é©ãªèªåå帰ãã¹ããããï¼Seleniumã®ãã¹ãã¹ã¯ãªããã¨ãã¹ãä»æ§æ¸ãèªåçæï¼
http://language-and-engineering.hatenablog.jp/entry/20090524/p1
Androidã¢ããªã®èªåãã¹ããã¼ã«ã§æãææã - ãNativeDriverãï¼Google製ãWebDriverãã®æ¡å¼µãï¼å
¬å¼ã®AndroidçSeleniumï¼
http://language-and-engineering.hatenablog.jp/entry/20110930/p1
IE AutoTester ã§ï¼UIã®å帰ãã¹ããå®å
¨èªåå
http://language-and-engineering.hatenablog.jp/entry/20090922/p1
ã