00001
00002
00003
00004
00005
00006
00007
00008 function IeDhtml(){
00009
00010 this.comWin = null;
00011
00012
00013 this.doc = null;
00014
00015
00016 this.win = null;
00017
00018
00019 this.submitPause = IeDhtml_submitPause;
00020
00021
00022 this.findTimeout = IeDhtml_findTimeout;
00023
00024
00025 this.findScope = null;
00026
00027
00028 this.defaultFrame = -1;
00029
00030
00031 this._sleep = function _sleep(milliseconds) {
00032 WScript.Sleep(milliseconds);
00033 };
00034
00035
00036
00037
00038
00039
00040 this.openWindowAsync = function openWindowAsync(url) {
00041 this.comWin = WScript.CreateObject("InternetExplorer.Application");
00042 if (this.ieWindowVisible!=false) {
00043 this.ieWindowVisible=true;
00044 }
00045 this.comWin.visible = this.ieWindowVisible;
00046 if ( (url.substr(0,5)!="http:") && (url.substr(0, 6)!="https:") && (url.charAt(1)!=':') ) {
00047
00048
00049 if ( url.substr(0, 6) != "about:" ) {
00050 var fso = new ActiveXObject("Scripting.FileSystemObject");
00051 var f = fso.GetFile(url);
00052 url = f.Path;
00053 }
00054 }
00055 this.comWin.navigate(url);
00056 return this;
00057 };
00058
00059
00060
00061
00062
00063
00064
00065 this.openWindow = function openWindow(url) {
00066 this.openWindowAsync(url);
00067 this.checkSubmit();
00068 return this;
00069 };
00070
00071
00072 this.navigateTo = function navigateTo(url) {
00073 this.comWin.navigate(url);
00074 this.checkSubmit();
00075 };
00076
00077
00078
00079
00080
00081 this.navigateToAsyn = function navigateToAsyn(url) {
00082 this.comWin.navigate(url);
00083 };
00084
00085
00086
00087
00088
00089
00090
00091 this.closeWindow = function closeWindow() {
00092 this.comWin.quit();
00093 this._sleep(300);
00094 this.comWin = null;
00095 this.win = null;
00096 this.doc = null;
00097 return this;
00098 };
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 this.seekWindow = function seekWindow(titlePattern, returnFirst) {
00111 var doc = null;
00112 var win = null;
00113
00114 for(var t=0; t<=this.findTimeout; t+=100) {
00115 this._sleep(200);
00116 var wins = (WScript.CreateObject("Shell.Application")).Windows();
00117 for( var i=0; i < wins.Count; i++) {
00118 try {
00119 doc = wins.item(i).document;
00120 if ( this._contains(doc.title, titlePattern) ) {
00121 win = wins.item(i);
00122 if (returnFirst) break;
00123 }
00124 } catch(e) {}
00125 }
00126 if (win != null ) return win;
00127 }
00128
00129 throw new Error(1100, "Can't find window containing title " + titlePattern);
00130 };
00131
00132
00133
00134
00135
00136
00137
00138 this.seekAndSetWindow = function(titlePattern, returnFirst) {
00139 var newWin=this.seekWindow(titlePattern, returnFirst);
00140 this.setWindow(newWin);
00141 return newWin;
00142 };
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 this.setFrame = function setFrame(idx_or_id) {
00159 this.defaultFrame = idx_or_id;
00160 if ( this.comWin != null ) {
00161 this.checkSubmit();
00162 }
00163 return this;
00164 };
00165
00166
00167
00168
00169 this.setFindScopeToFrame = function setFindScopeToFrame(idx) {
00170 if ( (this.doc.frames != undefined ) && (this.doc.frames.length>idx) ) {
00171 this.doc = this.doc.frames(idx).document;
00172 }
00173 this.findScope = this.doc;
00174 return this;
00175 };
00176
00177
00178
00179
00180 this.setWindow = function setWindow( win ) {
00181 this.comWin = win;
00182 this.checkSubmit();
00183 return this;
00184 };
00185
00186
00187
00188
00189
00190
00191
00192
00193 this.setTime = function setTime(pause, timeout) {
00194 if (pause > 0) {
00195 this.submitPause = pause;
00196 }
00197
00198 if (timeout>0) {
00199 this.findTimeout = timeout;
00200 }
00201 return this;
00202 };
00203
00204
00205
00206
00207
00208
00209 this.setFindScope = function setFindScope( scope ) {
00210 this.findScope = scope;
00211 return this;
00212 };
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 this.findLink = function findLink(str_or_reg, index) {
00228 if ( index == undefined) index = 0;
00229
00230 var list = this.findScope.all.tags("A");
00231 for (var i=0; i<list.length; i++) {
00232 var label = list[i].innerText;
00233 var match = ( typeof(str_or_reg) == "string" )
00234 ? this._contains(label, str_or_reg) : str_or_reg.test(label);
00235 if ( match ) {
00236 index--;
00237 if ( index < 0 ) {
00238 return list[i];
00239 }
00240 }
00241 }
00242 return null;
00243 };
00244
00245
00246
00247
00248 this.findImage = function findImage(src, index) {
00249 if ( index == undefined) index = 0;
00250 var list = this.findScope.all.tags("IMG");
00251 for (var i=0; i<list.length; i++) {
00252 if ( this._contains(list[i].src, src) ) {
00253 index--;
00254 if ( index < 0 ) {
00255 return list[i];
00256 }
00257 }
00258 }
00259 return null;
00260 };
00261
00262
00263
00264
00265
00266
00267
00268
00269 this.findObjById = function findObjById(id_or_name, tagName, type) {
00270 var obj = this.doc.all(id_or_name);
00271 if ( obj == null ) return null;
00272
00273 if ( obj.tagName != undefined ) {
00274 if ( tagName != undefined ) {
00275 if ( tagName != obj.tagName ) {
00276 return null;
00277 } else {
00278 if ( (type != undefined ) && ( obj.type != type ) ) {
00279 return null;
00280 }
00281 }
00282 }
00283 return obj;
00284 } else {
00285 for (var i=0; i<obj.length; i++) {
00286 var obj_i = obj[i];
00287 if ( tagName != undefined ) {
00288 if ( tagName != obj_i.tagName ) {
00289 continue;
00290 } else {
00291 if ( (type != undefined ) && ( obj_i.type != type ) ) {
00292 continue ;
00293 }
00294 }
00295 }
00296 return obj_i;
00297 }
00298 return null;
00299 }
00300 };
00301
00302
00303
00304
00305
00306
00307 this.findButton = function findButton(str, index) {
00308 if ( index == undefined) index = 0;
00309
00310 var list = this.findScope.all.tags("INPUT");
00311 for (var i=0; i<list.length; i++) {
00312 var e = list[i];
00313 if ( (e.type=="submit") || (e.type=="button") ) {
00314 if ( this._contains(e.value, str) ) index--;
00315 } else if ( e.type == "image" ) {
00316 if ( this._contains(e.alt, str) ) index--;
00317 if ( this._contains(e.src, str) ) index--;
00318 }
00319 if ( index < 0 ) {
00320 return e;
00321 }
00322 }
00323
00324 list = this.findScope.all.tags("BUTTON");
00325 for (var j=0; j < list.length; j++) {
00326 var e = list[j];
00327 if (this._contains(e.innerText, str) ) {
00328 index--;
00329 }
00330 if ( index < 0 ) {
00331 return e;
00332 }
00333 }
00334 return null;
00335 };
00336
00337
00338
00339
00340 this.findField = function findField(idx_or_id) {
00341 if ( typeof( idx_or_id ) != "number" ) {
00342 var e = this.findObjById(idx_or_id, "INPUT");
00343 if ( e == null ) return null;
00344 if ( (e.type == "password")|| (e.type=="text") ) {
00345 return e;
00346 }
00347 return null;
00348 }
00349
00350 var list = this.findScope.all.tags("INPUT");
00351 for (var i=0; i<list.length; i++) {
00352 var e = list[i];
00353 if ( (e.type=="password") || (e.type=="text") ) idx_or_id--;
00354 if ( idx_or_id<0 ) {
00355 return e;
00356 }
00357 }
00358 return null;
00359 };
00360
00361
00362
00363
00364 this.findCheckBox = function findCheckBox(idx_or_id) {
00365 if ( typeof(idx_or_id) == "number" ) {
00366 var list = this.findScope.all.tags("INPUT");
00367 for (var i=0; i<list.length; i++) {
00368 var e = list[i];
00369 if ( (e.type=="checkbox")) idx_or_id--;
00370 if ( idx_or_id<0 ) return e;
00371 }
00372 return null;
00373 } else {
00374 return this.findObjById(idx_or_id, "INPUT", "checkbox");
00375 }
00376 };
00377
00378
00379
00380
00381 this.findRadioButton = function findRadioButton(idx_or_id) {
00382 if ( typeof(idx_or_id) == "number" ) {
00383 var list = this.findScope.all.tags("INPUT");
00384 for (var i=0; i<list.length; i++) {
00385 var e = list[i];
00386 if ( e.type=="radio" ) idx_or_id--;
00387 if ( idx_or_id<0 ) return e;
00388 }
00389 } else {
00390 return this.findObjById(idx_or_id, "INPUT", "radio");
00391 }
00392 return null;
00393 };
00394
00395
00396
00397
00398
00399
00400 this.findSelect = function findSelect(idx_or_id) {
00401 if ( typeof(idx_or_id) == "number" ) {
00402 var list = this.findScope.all.tags("SELECT");
00403 return ( idx_or_id<list.length) ? list(idx_or_id) : null;
00404 } else {
00405 return this.findObjById(idx_or_id, "SELECT");
00406 }
00407 };
00408
00409
00410
00411
00412
00413 this.findByText = function findByText(text, idx) {
00414 if (idx == undefined) idx = 0;
00415 var rng = this.doc.body.createTextRange();
00416 if ( rng==null ) return null;
00417
00418 while(true) {
00419 if ( rng.findText(text) ) {
00420 if (idx==0) {
00421 return rng.parentElement();
00422 } else {
00423 idx--;
00424 rng.collapse(false);
00425 }
00426 } else {
00427 return null;
00428 }
00429 }
00430 };
00431
00432
00433
00434
00435 this.findParent = function findParent(obj, tag) {
00436 if ( obj==null ) return null;
00437
00438 var e;
00439 while ( true ) {
00440 e = obj.parentElement;
00441 if ( e==null ) return null;
00442 if ( e.tagName==tag.toUpperCase() ) {
00443 return e;
00444 }
00445 obj = e;
00446 }
00447 };
00448
00449
00450
00451
00452
00453
00454 this.findByTag = function findByTag(tag, idx) {
00455 var list = ( tag == "" ) ? this.findScope.all : this.findScope.all.tags(tag);
00456 if ( idx == undefined ) idx = 0;
00457 return list[idx];
00458 };
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 this.findByTagAndAttr = function findByTagAndAttr(tag, attrFilter, idx) {
00472 var list = ( tag == "" ) ? this.findScope.all : this.findScope.all.tags(tag);
00473 var attrName = attrFilter.substring(0, attrFilter.indexOf("~"));
00474 var attrValue = attrFilter.substring(attrFilter.indexOf("~") + 1);
00475
00476
00477 if ( idx == undefined ) idx = 0;
00478
00479 if ( attrName=="class" ) attrName = "className";
00480
00481 for (var i=0; i<list.length; i++) {
00482
00483 if ( attrName.length == 0 ) {
00484 if (idx-- <= 0) return list[i];
00485 } else {
00486 var aValue = list[i].getAttribute(attrName);
00487 if ( (aValue != undefined) && ( aValue != null)
00488 && this._contains(aValue, attrValue) ) {
00489 if ( idx-- <= 0 ) return list[i];
00490 }
00491 }
00492 }
00493 };
00494
00495
00496
00497
00498
00499 this.findTableCell = function findTableCell(id_or_idx, rowIdx, columnIdx) {
00500 var table;
00501 if ( typeof(id_or_idx) == "number") {
00502 if ( this.findScope.all.tags("TABLE").length <= id_or_idx ){
00503 return null;
00504 }
00505 table = this.findScope.all.tags("TABLE")[id_or_idx];
00506 } else {
00507 table = this.findScope.all[id_or_idx];
00508 }
00509 if ( table == null ) {
00510 return null;
00511 }
00512
00513 if ( table.rows.length <= rowIdx ) return null;
00514 var row = table.rows[rowIdx];
00515
00516 if ( row.cells.length <= columnIdx) return null;
00517 return row.cells[columnIdx];
00518 };
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 this.clickObj = function clickObj(obj) {
00532 if ( obj == null ) {
00533 throw new Error(1108, "Can't click null object");
00534 }
00535
00536 obj.fireEvent("onmouseover");
00537 obj.click();
00538 this.checkSubmit();
00539 return this;
00540 };
00541
00542
00543
00544
00545
00546
00547 this.clickLink = function clickLink(str_or_reg, index) {
00548 var e = this.findLink(str_or_reg, index);
00549 if ( e == null ) {
00550 throw new Error(1105, "Can't find link whose text contains or matchs " + str_or_reg);
00551 }
00552
00553 return this.clickObj( e );
00554 };
00555
00556
00557
00558
00559
00560
00561 this.clickLinkRange = function clickLinkRange() {
00562 for (var x=0;x<arguments.length; x++) {
00563 this.clickLink(arguments[x]);
00564 }
00565 };
00566
00567
00568
00569
00570
00571
00572 this.clickButton = function clickButton(text, index) {
00573 var b = this.findButton(text, index);
00574 if ( b == null ) {
00575 throw new Error(1104, "Can't find button whose label contains '" + text + "'");
00576 } else {
00577 return this.clickObj( b );
00578 }
00579 };
00580
00581
00582
00583
00584
00585 this.clickImage = function clickImage(src_sub_str, index) {
00586 var img = this.findImage(src_sub_str, index);
00587 if ( img == null ) {
00588 throw new Error(1109, "Can't find image whose src attributes contains '" + src_sub_str + "'");
00589 } else {
00590 return this.clickObj( img );
00591 }
00592 };
00593
00594
00595
00596
00597 this.clickObjById = function clickById(id_or_name) {
00598 var e = this.findObjById(id_or_name);
00599 if ( e == null ) {
00600 throw new Error(1104, "Can't find object with id " + id_or_name);
00601 }
00602 return this.clickObj(e);
00603 };
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 this.checkSubmit = function checkSubmit() {
00616 this._sleep(this.submitPause);
00617 var frmChain = null;
00618
00619 if (this.defaultFrame != -1) {
00620 if ( typeof( this.defaultFrame ) == "string" ) {
00621 frmChain = this.defaultFrame.split("/");
00622 } else {
00623 frmChain = new Array();
00624 frmChain[0] = this.defaultFrame;
00625 }
00626 }
00627
00628 function findNestedDoc(rootDoc, frmC) {
00629 var theDoc = rootDoc;
00630 try {
00631 while( true ) {
00632 var id_or_idx = frmC.shift();
00633 if ( id_or_idx == null ) break;
00634 if ( (typeof(id_or_idx)=="string") && id_or_idx.match(/^\d+$/) ) {
00635 id_or_idx = id_or_idx-0;
00636 }
00637 theDoc = theDoc.frames(id_or_idx).document;
00638 }
00639 } catch (e) {theDoc = null}
00640 return theDoc;
00641 };
00642
00643
00644 for(var t=0; t<=this.findTimeout; t+=100) {
00645 this._sleep(100);
00646 try {
00647 this.doc = this.comWin.document;
00648 if (this.doc.readyState == "complete") {
00649 if ( frmChain != null ) {
00650 var nestedDoc = findNestedDoc(this.comWin.document, frmChain);
00651 if ( nestedDoc == null ) continue;
00652
00653
00654 try {
00655 if( nestedDoc.readyState != "complete" ) {
00656 continue;
00657 }
00658 this.findScope = nestedDoc;
00659 this.doc = nestedDoc;
00660 } catch(ex) {
00661
00662 this.findScope = this.doc;
00663 }
00664 } else {
00665 this.findScope = this.doc;
00666 }
00667
00668
00669 this._sleep(this.submitPause*0.2);
00670 this.win = this.doc.parentWindow;
00671 return this;
00672 }
00673 } catch (ex) {}
00674 }
00675
00676 throw new Error(1101, "Waiting for HTTP response timed out");
00677 };
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 this.setSelectOption = function setSelectOption(idx_or_id, substr_or_idx) {
00689 var s = this.findSelect(idx_or_id);
00690 if ( s == null ) {
00691 throw new Error(1106, "Can't find SELECT element with index or id '" + idx_or_id + "'");
00692 }
00693 var opt = null;
00694
00695 if ( typeof(substr_or_idx) == "number" ) {
00696 if ( substr_or_idx < s.options.length ) {
00697 opt = s.options[substr_or_idx];
00698 }
00699 } else {
00700 for(var i=0; i<s.options.length; i++) {
00701 if ( this._contains(s.options[i].innerText, substr_or_idx) ) {
00702 opt = s.options[i];
00703 break;
00704 }
00705 }
00706 }
00707
00708 if ( opt != null ) {
00709 opt.selected = true;
00710 s.fireEvent("onchange");
00711 this.checkSubmit();
00712 } else {
00713 throw new Error(1106, "Can't find SELECT option " + value);
00714 }
00715 };
00716
00717
00718
00719
00720 this.setCheckBox = function setCheckBox(idx, checked) {
00721 var b = this.findCheckBox(idx);
00722 if ( b == null ) {
00723 throw new Error(1107, "Can't " + idx + "-th check box");
00724 }
00725 if ( b.checked != checked ) {
00726 this.clickObj(b);
00727 }
00728 return this;
00729 };
00730
00731
00732 this.checkRadioButton = function checkRadioButton(idx) {
00733 var b = this.findRadioButton(idx);
00734 if ( b == null ) {
00735 throw new Error(1108, "Can't " + idx + "-th radio button");
00736 }
00737 this.clickObj(b);
00738 return this;
00739 };
00740
00741
00742 this.setField = function setField(idx_or_id, value) {
00743 var e = this.findField(idx_or_id);
00744 if ( e != null ) {
00745 e.value = value;
00746 return this;
00747 } else {
00748 throw new Error(1103, "Can't find field with index or id " + idx_or_id);
00749 }
00750 };
00751
00752
00753
00754
00755
00756
00757
00758
00759 this._contains = function _contains(str, pattern) {
00760 if ( (pattern == null) || (pattern=="") || ( str.indexOf(pattern)>=0) ) {
00761 return true;
00762 } else
00763 return false;
00764 };
00765
00766
00767
00768
00769 this.findRowByText = function findRowByText(txt) {
00770 return this.findParent(this.findByText(txt), "TR");
00771 };
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781 this.assertSelectHasOption = function assertSelectHasOption(idx_or_id, optPattern, msg) {
00782 var s = this.findSelect(idx_or_id);
00783 for( var i=0; i<s.options.length; i++) {
00784 if ( this._contains(s.options[i].innerText, optPattern) )
00785 return;
00786 }
00787
00788 var txt = "assertSelectHasOption failed: no option containing " + optPattern;
00789 if ( msg != undefined) txt += ": " + msg;
00790 throw new IeError(1011, txt);
00791 };
00792
00793
00794
00795
00796
00797
00798 this.assertPageHasText = function assertPageHasText(str, msg) {
00799 var pageText = this.doc.documentElement.innerText;
00800 if ( pageText.indexOf(str) < 0 ) {
00801 var txt = "assertPageHasText failed: the pages doesn't contain '" + str + "'";
00802 if ( msg != undefined ) txt += ": " + msg;
00803 throw new IeError(1009, txt);
00804 }
00805 };
00806
00807
00808
00809
00810
00811 this.assertTagHasText = function assertTagHasText(tag, idx, txt) {
00812 var e = this.findByTag(tag, idx);
00813 if ( e == null ) {
00814 throw new IeError(1014, "Can't find " + idx + "-th element with tag " + tag );
00815 } else {
00816 if ( e.innerText.indexOf(txt) < 0 ) {
00817 throw new IeError(1014,
00818 idx + "-th element with tag " + tag + " doesn't contain + \"" + txt + "\"" );
00819 }
00820 }
00821 };
00822
00823
00824
00825
00826
00827
00828 this.assertTextContains = function(e, str, msg) {
00829 var innerText = e.innerText;
00830 if ( (innerText == null) || ( innerText.indexOf(str)<0 ) ) {
00831 var txt = "assertTextCoontains failed: element's innerText doesn't contain '" + str + "'";
00832 if ( msg != undefined ) txt += ": " + msg;
00833 throw new IeError(1013, txt);
00834 }
00835 };
00836 }
00837