Selenium study notes 5: encapsulate several common functions

Source: Internet
Author: User

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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.