Main Page | File List | File Members

IeDhtml.js

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Implements services to interact with the DHTML model within
00004  * Internet Explorer windows.
00005  */  
00006 
00007 /** Construct an IeDhtml object. */
00008 function IeDhtml(){
00009      /** Reference to IE COM object. */
00010     this.comWin = null;
00011     
00012     /** The underlaying DHTML document object. */
00013     this.doc = null;
00014 
00015     /** Reference to document's window object.*/
00016     this.win = null;
00017 
00018     /** The pause in milliseconds after submission. */
00019     this.submitPause = IeDhtml_submitPause;
00020 
00021     /** Timeout value in milliseconds for find-operations. */
00022     this.findTimeout = IeDhtml_findTimeout;
00023 
00024     /** Search scope object for most find-operations. */
00025     this.findScope   = null;   
00026 
00027     /** The index, name or id of the default frame for the doc object. */
00028     this.defaultFrame = -1;         
00029         
00030     /** Sleeps for given number of milliseconds. */
00031     this._sleep = function _sleep(milliseconds) {
00032         WScript.Sleep(milliseconds);
00033     };
00034 
00035     /**
00036      * Start IE and nevigate it to given URL. 
00037      * Don't wait for the document changes to completed status.
00038      * @see openWindow
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             // url is probably a local relative file path, we convert to
00048             // absolute path here
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      * Start IE and nevigate it to given URL.
00061      * @see closeWindow
00062      * @see seekWindow
00063      * @see setWindow
00064      */
00065     this.openWindow = function openWindow(url) {
00066         this.openWindowAsync(url);
00067         this.checkSubmit();
00068         return this;
00069     };
00070 
00071     /** Navigate current IE browser to a new URL. */
00072     this.navigateTo = function navigateTo(url) {
00073         this.comWin.navigate(url);
00074         this.checkSubmit();
00075     };
00076     
00077     /** 
00078      * Navigate IE to a new URL and waits till the document's 
00079      * status becomes completed 
00080      */
00081     this.navigateToAsyn = function navigateToAsyn(url) {
00082         this.comWin.navigate(url);
00083     };
00084 
00085     /**
00086      * Close the current IE window.
00087      * @see openWindow
00088      * @see seekWindow
00089      * @see setWindow
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      * Seek a window with given pattern in its titles. If there more than one such windows,
00102      * the last one will be returned, unless returnFirst is set to true in which
00103      * case the first window found will be returned.
00104      *
00105      * Together with method setWindow() you can attach an IeDhtml to 
00106      * any open Internet Explorer window. 
00107      *
00108      * @see setWindow
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      * Find IE window whose title matches titlePattern and set the found window
00134      * as the window object of current IeDhtml object. If the second argument is provided
00135      * and is the boolean true, the first matching window will be set, otherwise
00136      * the last one.
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     // Some set methods.
00146     //================================================================
00147 
00148     /**
00149      * Change the default frame index for window with frames. After each
00150      * submit action, the document object of the frame with the default
00151      * index/id/name will become the default document object this.doc. 
00152      * The argument may be the index, the name or the id of a frame.
00153      * If the frames are nested throught multiple document the argument
00154      * must be a string of syntax "idx_or_id1/idx_or_id2/...".
00155      *
00156      * @see setFindScopeToFrame
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      * Set the find scope to the document object of frame with give index.
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      * Set given IE window object as current window object of this IeDhtml object.
00179      */
00180     this.setWindow = function setWindow( win ) {
00181         this.comWin = win;
00182         this.checkSubmit();
00183         return this;
00184     };
00185 
00186     /**
00187      * Set the timeout values. 'pause' specifies the minimum time in milliseconds
00188      * a submit action will wait before returning. 'timeout'
00189      * specifies the maximum time in milliseconds that a find operation
00190      * will wait for completion. If no object can be found, an exception will normally
00191      * be thrown.
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      * Set the find scope to a DHTML object. The default find scope is the whole 
00206      * document object of the default frame.  Using this method we can restrict 
00207      * the searching scopes. Most find operations search within the find scope.
00208      */
00209     this.setFindScope = function setFindScope( scope ) {
00210         this.findScope = scope;
00211         return this;
00212     };
00213 
00214     //================================================================
00215     // Methods to localize objects. All find methods should return 
00216     // the object found, or null if the object is not found. No HTTP
00217     // requested should be submitted.
00218     //================================================================
00219 
00220     /** 
00221      * Find an anchor object whose description contains given string.
00222      * If the index paramter is present, the index-th anchor object
00223      * containing given string will be returned.
00224      * If the parameter str_or_reg is regular expression object, e.g. /Hello.*Word/i, 
00225      * the regular expression will be used to find matching text.
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      * Find an image object whose src attribute contains given string
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      * Find an object with given id or name. If the optional tagName and type are 
00264      * given, the object must have given tag name and
00265      * type. If there are multiple object with the same id or name which satify 
00266      * the tagName and type condition, the first one found will be returned.
00267      * Notice: In DHTML element id is case-incensitive.
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      * Find a button object whose label contains given string, or whose
00304      * id equals given string. If the index parameter is given, index-th button
00305      * with given label will be returned.
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      * Find an INPUT object of type text or password with given index, id or name.
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      * Find an check box with given index, id or name.
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      * Find an radio box with given index, id or name.
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      * Find a SELECT object with given index, id or name. If there are multiple 
00398      * SELECT object with the same id or name the first will be returned.
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      * Find the idx-th appearence of given text within the whole document object, 
00411      * then return the closest object containing the text.
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      * Find the first parent of given object with given tag name.
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      * Find a HTML element with given tag name. If the
00451      * idx argument is specified, the idx-th such element will be returned.  
00452      * If the tag parameter is the empty string all tags are searched.
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      * Find a HTML element with given tag name and attribute sub-string. If the
00462      * idx argument is specified, the idx-th such element will be returned. 
00463      * The syntax for the attrFilter is [attrName]~[SubString]. For instance
00464      * findByTagAndAttr("INPUT", "value~abc", 2) finds the 2-th INPUT object
00465      * whose value contains the substring abc.
00466      *
00467      * If the tag parameter is the empty string all tags are searched.
00468      * If the attrFilter parameter is empty all this method just returns
00469      * the idx-th object with the given tag name.
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"; // fix the problem with getAttribute().
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      * Find a cell in a table. The table is identified by given id or index.
00497      * The cell is indentified by row and column index within the table.
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     // Methods for various submit operations.
00522     //================================================================
00523 
00524     /**
00525      * Trigger a click event on an DHTML object, then wait till the document's status
00526      * becomes 'complete' which normally means that the response from the Web server has been
00527      * loaded completely into the document object.
00528      *
00529      * @see checkSubmit
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      * Click on an anchor object whose target contains specific text. If the
00544      * the index argument is specified, the index-th such anchor 
00545      * object will be clicked.
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      * Click on a range of anchor objects whose target contains 
00558      * specific text as defined in argument list. Notice this 
00559      * method accepts variable number of arguments.
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      * Click on a button object whose label contains given text or whose id equals
00569      * the given text. If the optional index argument is given this function returns
00570      * the index-th button with given label.
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      * Click on an image object whose src attributes contains given string. 
00583      * If an index argument is given, the index-th such image object will be clicked. 
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      * Click on an object with given id or name.
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      * Check the submit response.
00607      * This method is called internally by many methods which are expected to trigger
00608      * submit operation. This method waits till the document's status becomes 'complete',
00609      * then set this.doc to the document object of the default frame if the
00610      * window has frames. This method uses the members submitPause and findTimeout
00611      * to control the minimum and maximal waiting time.
00612      *
00613      * @see setTime
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         // Wait till the page is in ready state or timeouted
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                         // wait till the default frame become ready.
00654                         try {
00655                             if( nestedDoc.readyState != "complete" ) {
00656                                 continue;
00657                             }
00658                             this.findScope = nestedDoc;
00659                             this.doc = nestedDoc; 
00660                         } catch(ex) {
00661                             // The frame probably hosts foreign domain document. 
00662                             this.findScope = this.doc; 
00663                         }
00664                     } else {
00665                         this.findScope = this.doc;
00666                     }
00667                     
00668                     // wait a little while for possible JavaScript code.
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     // Methods to enter input values triggering submission.
00681     //================================================================
00682 
00683     /**
00684      * Select an option of a SELELT element. The argument
00685      * idx_or_id is either the index or the id of the SELECT object.
00686      * opt_str_or_idx is a substring the option or the index of the option.
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      * Set the state of the idx-th CHECKBOX object to checked which must be a boolean value.
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     /** Toggle the state of the idx-th radio box. */
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     /** Set the value of an text input object.  */
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     // General purpose functions and methods
00754     //================================================================
00755 
00756     /**
00757      * Check whether a string contains another string.
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      * Finds a TR object with specific text.
00768      */
00769     this.findRowByText = function findRowByText(txt) {
00770         return this.findParent(this.findByText(txt), "TR");
00771     };
00772     
00773     //================================================================
00774     // DHtml specifical assert functions
00775     //================================================================
00776     /**
00777      * Assert that a SELECT object has a OPTION object with given substring.
00778      * Optional message argument will be appended to the exception message 
00779      * in case of failure.
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      * Assert that the response page's text contains given string.
00795      * The optional error message will be appended the exception message
00796      * in case of failure.
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      * Assert that an element with given tag name and index contains
00809      * given text
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      * Assert that an element's innerText contains given text.
00825      * The optional error message will be appended to the exception 
00826      * message in case of failure.
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 

Generated on Thu Sep 29 09:05:40 2005 for IeUnit by  doxygen 1.3.9.1