ããã¸ã§ã¯ãå°ç¨ã®DSLã§ï¼å¹ççã«IEãèªåæä½ããï¼WSH/JScriptï¼
以ä¸ã®ã¨ã³ããªã§ã¯ï¼JScriptã§ãã©ã¦ã¶ã®ãªã¼ãã¡ã¼ã·ã§ã³ãè¡ãªãããã®åºç¤ãè¿°ã¹ãã
JScript / VBScript (WSH)ã§ï¼IEãèªåæä½ããã
http://d.hatena.ne.jp/language_and_engineering/20090713/p1
ãã®ç¶ç·¨ã¨ãã¦ï¼ããã¸ã§ã¯ãå
ã§ãªã¼ãã¡ã¼ã·ã§ã³ã¹ã¯ãªããã使ãããã®å®ç¨çãªãã¡ã¤ã«æ§æãè¿°ã¹ãã
ãããã¸ã§ã¯ãã«ç¹åãã便å©ã¡ã½ããï¼DSLï¼ãä½ã£ã¦ï¼æ¥½ã«ãã©ã¦ã¶ãåããããã¨ããã³ã³ã»ããã
æ¦è¦
ãã¡ã¤ã«æ§æï¼
- å®è¡.bat
- ie.wsfãèµ·åããããã®ãããã
- ie.wsf
- ããã¸ã§ã¯ãã®æ©è½ãå®è¡ãããã¡ã¤ã«ã
- eg. å ·ä½çãªã¦ã¼ã¶åãå©ç¨ãããã°ã¤ã³ãªã©ã
- lib_site.js
- ããã¸ã§ã¯ãã®æ©è½ãå®ç¾©ãããã¡ã¤ã«ã
- eg. ãã°ã¤ã³ãªã©ã
- lib_ie.js
- ãã©ã¦ã¶ã®æ©è½ãå®ç¾©ãããã¡ã¤ã«ã
- eg. DOMæä½ãªã©ã
ä¸ã®é層ãä¸ã®é層ãå¼ã³åºãã¦ããã
ï¼ã«ã¯ï¼IEãèªåæä½ããããã®ä¾¿å©ã¡ã½ãããããããè©°ãè¾¼ãã§ããã
æ®æ®µã¯ãã¾ãç·¨éããªãã
ï¼ã«ã¯ï¼å¯¾è±¡ããã¸ã§ã¯ãã®Webã¢ããªã±ã¼ã·ã§ã³ãæä½ããããã®ä¾¿å©ã¡ã½ãããæ¸ãã
ããã°ï¼ã¦ã¼ã¹ã±ã¼ã¹ã®éå½¢ã
ããã¸ã§ã¯ãã®æ©è½ãå©ç¨ããããã®DSLã¨ãè¨ããã
ï¼ã¯ï¼ï¼ã¨ï¼ãå¼ã³åºãã¦ããã
ï¼ã§ã¡ã½ãããå®ç¾©ãã¦ããã°ï¼ï¼ã§ã¯ï¼å¤æ°ã®å
·ä½çãªã¦ã¼ã¹ã±ã¼ã¹ãç°¡æ½ã«è¨è¿°ã§ããã
â»ã¦ã¼ã¹ã±ã¼ã¹ãç´æ¥wsfä¸ã«è¨è¿°ããã®ã§ã¯ãªãï¼å¤é¨jsãã¡ã¤ã«ã«åãåãã¦ãOKãwsfã¯ãããå¼ã³åºãã°ããã
ç¹ã«ï¼ã³ã¼ãéãå¢ãã¦ãããããããå¿
è¦ãããã
ï¼ã¯ï¼ï¼ãããã¯ããã ããwsfã®å®è¡æ¹æ³ãããããªãï¼ã¨ãã人ãããããã
å
·ä½çãªãã¡ã¤ã«å
容ã®ãµã³ãã«ã¯ä¸è¨ã®éãã
ï¼ï¼ï¼ããã¯ç¨ã®ããã
å®è¡.bat
cscript ie.wsf
ï¼ï¼ï¼ã¦ã¼ã¹ã±ã¼ã¹
ie.wsf
<job> <!-- IEèªåæä½ã®ããã®ã©ã¤ãã©ãª --> <script language="JavaScript" src="lib_ie.js"></script> <!-- ããã¸ã§ã¯ãã®æ©è½ãå©ç¨ããããã®ã©ã¤ãã©ãª --> <script language="JavaScript" src="lib_site.js"></script> <!-- ããã¸ã§ã¯ãã®æ©è½ã®å¼ã³åºãã以ä¸ã«è¨è¿°ãã¾ãã --> <script language="JavaScript"> var ie = new IE(); open_top_page(); replace_body_html(); // æ¤ç´¢å®è¡ search_by_keyword( "hoge" ); search_by_keyword( "fuga" ); search_by_keyword( "boo" ); ie.quit(); </script> </job>
ï¼ï¼ï¼ã¦ã¼ã¹ã±ã¼ã¹éå½¢
lib_site.js
// ããã¸ã§ã¯ãã«ç¹åããã¡ã½ãããè¨è¿°ãã¾ãã // ããããã¼ã¸ãéã function open_top_page() { ie.goto_url( "http://www.yahoo.co.jp" ); } // ç»é¢ä¸ã®è¡¨ç¤ºãå å·¥ãã function replace_body_html() { ie.exec_js(function(){ document.body.innerHTML = document.body.innerHTML.replace( /ã¼/g, "âââ(+A+)âââ" ); focus(); }); ie.sleep( 1000 ); } // ãã¼ã¯ã¼ãæ¤ç´¢ function search_by_keyword( kwd ) { open_top_page(); ie.type( "srchtxt", kwd ); ie.click_and_wait( "srchbtn" ); ie.sleep( 1000 ); }
å
·ä½çã«ã©ããããã¼ã¯ã¼ãã§æ¤ç´¢ããã®ãï¼ã¨ããæ
å ±ã¯ï¼ï¼ï¼ï¼ã§ã¯ãªãï¼ï¼ï¼ã«æ¸ãã
ï¼ï¼ï¼ãã©ã¦ã¶æä½ç¨ã®ã©ã¤ãã©ãª
lib_ie.js
// IEèªåæä½ã®ããã®ã¯ã©ã¹ var IE = function( obj ){ this._build( obj ); }; IE.prototype = { // ãã©ã¦ã¶ãªãã¸ã§ã¯ã _ie : null, // ---------- ã»ããã¢ããç³» ---------- // ååã»ããã¢ãã _build : function( obj ){ this._create_new_ie(); var default_url = "about:blank"; if( obj && obj[ "url" ] ) { default_url = obj[ "url" ]; } this.goto_url( default_url ); this.set_visible( true ); } , // æ°è¦IEãã»ãã _create_new_ie : function(){ this._ie = WScript.CreateObject( "InternetExplorer.Application" ); } , // å¯è¦ç¶æ set_visible : function( bool ){ this._ie.Visible = bool; } , // çµäº quit : function(){ this._ie.Quit(); this._ie = null; } , // ---------- ã·ã¹ãã å¾ æ©ç³» ---------- // ãã¸ã¼ç¶æ ã®éå¾ ã¤ wait_while_busy : function(){ var timeout_ms = 10000; var step_ms = 100; var total_waited_ms = 0; while( this.is_busy() ) { this.sleep( step_ms ); // ã¿ã¤ã ã¢ã¦ããï¼ total_waited_ms += step_ms; if( total_waited_ms >= timeout_ms ) { this.debug( "è¦åï¼ã¿ã¤ã ã¢ã¦ãã®ããï¼ãªãã¼ããã¾ãã(" + this.get_current_url() + ")" ); this.reload(); break; } } this.sleep( 500 ); } , // ãã¸ã¼ç¶æ ãå¤å® is_busy : function(){ return ( ( this._ie.Busy ) || ( this._ie.readystate != 4 ) ); } , // æå®ããªç§ã ãå¾ æ© sleep : function( ms ){ WScript.Sleep( ms ); } , // ---------- ã¢ãã¬ã¹ãã¼é¢é£ ---------- // ãã¼ã¸ã移å goto_url : function( url ){ this._ie.Navigate( url ); this.wait_while_busy(); } , // ãã¼ã¸ããªãã¼ã reload : function(){ this._ie.document.location.reload( true ); // http://www.microsoft.com/japan/technet/scriptcenter/resources/qanda/sept05/hey0927.mspx this.wait_while_busy(); } , // ç¾å¨è¡¨ç¤ºä¸ã®ãã¼ã¸ã®URL get_current_url : function() { return this._ie.LocationURL; // http://blog.livedoor.jp/programlog/archives/298228.html } , // åãåã£ãJavaScripté¢æ°ãªãã¸ã§ã¯ãããã©ã¦ã¶ä¸ã§å®è¡ãã¾ã exec_js : function( func ) { var str_address = "javascript:(" + func.toString() + ")();void(0);"; //this.debug( str_address ); // toSource()ã®ãããã«toString()ã // http://blog.livedoor.jp/dankogai/archives/50957994.html this._ie.Navigate( str_address ); } , // ---------- DOMæä½ ---------- // ã»ã¼ããªè¦ç´ åå¾ $ : function( dom_id ){ // 10ç§ã¾ã§ã¯å¾ ã£ã¦ããã this.wait_for_element_present( dom_id, 10000 ); return this.gid( dom_id ); } , // IDã§è¦ç´ åå¾ gid : function( dom_id ){ return this._ie.document.getElementById( dom_id ); } , // åå¨å¤å® is_element_present : function( dom_id ){ return ( this.gid( dom_id ) != null ); } , // åå¨å¾ ã¡ wait_for_element_present : function( dom_id, ms_timeout ) { var ms_spent = 0; while( true ) { // è¦ç´ ãç¾ãããï¼ if( this.is_element_present( dom_id ) ) { break; } else { ms_spent += 100; this.sleep( 100 ); } // ã¿ã¤ã ã¢ã¦ããï¼ if( ms_timeout <= ms_spent ) { this.debug( dom_id + "ãåå¨ãã¾ããã" ); break; } } return; } , // å ¥å type : function( dom_id, value ){ this.$( dom_id ).value = value; } , // ã¯ãªã㯠click : function( dom_id ){ this.$( dom_id ).click(); } , // ã¯ãªãã¯ãã¦å¾ æ© click_and_wait : function( dom_id ){ this.click( dom_id ); this.wait_while_busy(); } , // ã»ã¬ã¯ãããã¯ã¹ï¼æè¨ãã¼ã¹ï¼ select_by_label : function( dom_id, target_label ) { var opts = this.$( dom_id ).options; for( var i = 0; i < opts.length; i ++ ) { if( "" + opts[i].innerText == "" + target_label ) { opts[i].selected = true; } } this.$( dom_id ).fireEvent( "onchange" ); } , // ---------- ãããã°ç¨ ---------- debug : function( str ) { WScript.Echo( str ); } };
解説ï¼
- InternetExplorer.Applicationã¨ããActiveXãªãã¸ã§ã¯ããã®ãã®ãæ¡å¼µãããã¨ã¯ä¸å¯è½ãã ãããããã«ï¼ãã®ãªãã¸ã§ã¯ãããã£ã¼ã«ãã«æã¤ãããªã©ããã¼ã¯ã©ã¹ãå®ç¾©ãã¦ï¼ããããæ¾é¡ã«ã¡ã½ããã追å ãããã¤ã¾ãã³ã³ãã¸ã·ã§ã³ã
- exec_jsã¯ï¼ã¯ãã¼ã¸ã£ãåãåã£ã¦ãã©ã¦ã¶ä¸ã§ããã¯ãã¼ã¯ã¬ããã¨ãã¦å®è¡ãã¦ããããä¾ãã°ï¼wsfå´ã§ã¯ alert() ã¨ããé¢æ°ãå®ç¾©ããã¦ããªãã¦ãï¼wsfå ã®ã¯ãã¼ã¸ã£å ã« alert() ã¨æ¸ããã¨ãã§ãããã¯ãã¼ã¸ã£ã¯ãã°ãããã
- ãã©ã¦ã¶ã®èªåæä½ã«ã¤ããã®ã®åé¡ã¯ï¼ã¿ã¤ã ã¢ã¦ãã ããããï¼ãã®ã³ã¼ããªãï¼ã¿ã¤ã ã¢ã¦ãçºçãæ¤åºããæç¹ã§ãã¼ã¸ã¯èªåçã«ãªãã¼ãããï¼ãªã¼ãã¡ã¼ã·ã§ã³ã¯ä¸æããªãã
è£è¶³
Selenium RCã®ãã¹ãã¹ã¯ãªããã¨ä¼¼ãã¤ã¡ã¼ã¸ã
Part4 ãã¹ããèªååãã注ç®ã®ãã¼ã«Seleniumï¼ãã¾ããªãã¹ãããã°æ©æçºè¦ã®æ±ºãæã«
http://itpro.nikkeibp.co.jp/article/COLUMN/20071011/284284/?ST=develop&P=3ä¸çªä¸ã® testä¼å¡ç»é²() ã¨ããã¡ã½ãããåèã«
æ¬ç¨¿ã¯ï¼Webã¢ããªã±ã¼ã·ã§ã³ã¸ã®ãã¹ããã¼ã¿æå ¥ã®ãããªå®åæä½ãªã©ã«å©ç¨å¯ã
ã