In actual testing, it is inevitable to wait until the control is fully ready to perform related operations. In this case, it is necessary to dynamically judge a certain condition in a small step, wait a few seconds instead of simply waiting. Related functions can be encapsulated in a seleniumutils class for you to call:
Import COM. thoughtworks. selenium. selenium; <br/> Import COM. thoughtworks. selenium. selenesetestcase; </P> <p> public class seleniumutils extends selenesetestcase {</P> <p> private selenium; <br/> Public seleniumutils (selenium) {<br/> This. selenium = selenium; <br/>}</P> <p> Public selenium getselenium () {<br/> return selenium; <br/>}</P> <p> Public void pause (INT millisec) {<br/> try {<br/> thread. Sleep (millisec); <br/>} catch (interruptedexception e) {<br/>; <br/>}</P> <p> Public Boolean waitforelementeditable (string locator, int seconds) {<br/> Boolean elementeditable = false; <br/> for (INT second = 0; second ++) {<br/> If (second> seconds) {<br/> break; <br/>}< br/> try {<br/> If (selenium. iseditable (Locator) {<br/> elementeditable = true; <br/> break; <br/>}< br/>} catch (exception E) {<br/>;< br/>}< B R/> pause (1000); <br/>}</P> <p> return elementeditable; <br/>}</P> <p> Public Boolean waitforelementpresent (string locator, int seconds) {<br/> Boolean elementpresent = false; <br/> for (INT second = 0; second ++) {<br/> If (second> seconds) {<br/> break; <br/>}< br/> try {<br/> If (selenium. iselementpresent (Locator) {<br/> elementpresent = true; <br/> break; <br/>}< br/>} catch (exception E) {<br/>;< br/>}< br/> pause (10 00); <br/>}< br/> return elementpresent; <br/>}</P> <p> Public Boolean waitfortextpresent (string locator, int seconds) {<br/> Boolean textpresent = false; <br/> for (INT second = 0; second ++) {<br/> If (second> seconds) {<br/> break; <br/>}< br/> try {<br/> If (selenium. istextpresent (Locator) {<br/> textpresent = true; <br/> break; <br/>}< br/>} catch (exception E) {<br/>;< br/>}< br/> pause (1000); <br/>}< br/> return te Xtpresent; <br/>}</P> <p> Public void waitforpagetoload (string MIS) {<br/> Boolean isloaded = false; <br/> int COUNT = 0; <br/> do {<br/> If (count ++> 3) {<br/> break; <br/>}< br/> try {<br/> selenium. waitforpagetoload (MIS); <br/> isloaded = true; <br/>}catch (exception ex) {<br/> continue; <br/>}< br/>}while (! Isloaded); <br/>}< br/>}