The Long and Short of Waits in Web Ap­pli­ca­tions Us­ing Se­le­nium

The time taken to load aWeb page im­pacts the user ex­pe­ri­ence in a big way. Of­ten, a Web page car­ries many Web el­e­ments which take time to load, and if this is too long, users tend to shift to an­other Web page, re­sult­ing in the loss of valu­able busi­ness op

OpenSource For You - - Developers - By: Ath­meeya N.R. The au­thor works as a Se­le­nium au­to­ma­tion de­vel­oper at Del­lEmc (EMC2) and has six years of ex­pe­ri­ence in au­to­ma­tion test­ing.

Most Web ap­pli­ca­tions we deal with are de­vel­oped us­ing Ajax and JavaScript. In such cases, when a page is loaded by the browser, the Web el­e­ments will take some time to load and the load­ing time can be dif­fer­ent in each run. This leads to dif­fer­ent ex­cep­tions, re­sult­ing in a re­port with the wrong results even though the Web app is work­ing cor­rectly.

Han­dling the de­lays with ac­cu­racy

On notic­ing a de­lay in the ap­pear­ance of a UI el­e­ment on the page, let’s sup­pose we use Thread.sleep() to han­dle it. The next time we run the test, the UI el­e­ment loads in­stantly. But what about Thread.sleep(), which we in­tro­duced into our code? It still halts the ex­e­cu­tion, thereby in­duc­ing the ex­tra wait time even though it’s not re­quired. Fi­nally, ex­e­cut­ing our en­tire test suite takes its own sweet time to fin­ish. Though the results are ac­cu­rate and re­li­able, in a prac­ti­cal world, this ap­proach makes no sense when man­ual in­ter­ven­tion test­ing of the ap­pli­ca­tion gets the job done in one minute, com­pared to our au­to­mated script, which takes three min­utes due to the ex­tra over­head de­lay of sleep() that we in­tro­duced.

So to make au­to­ma­tion ro­bust and ac­cu­rate, as well as to syn­chro­nise our test tool with the ap­pli­ca­tion un­der test by han­dling these de­lays dy­nam­i­cally, we use waits pro­vided by Se­le­nium.

Types of waits

Im­plicit wait: An im­plicit wait is to in­struct We­bDriver to poll the DOM for a cer­tain amount of time when try­ing to find an el­e­ment or el­e­ments, if these are not im­me­di­ately avail­able. The de­fault set­ting is 0. Once set, the im­plicit wait is set for the life of the We­bDriver ob­ject in­stance.

Dec­la­ra­tion:

driver.man­age().time­outs().im­plic­it­lyWait(10, TimeUnit. SEC­ONDS);

When to use: Not rec­om­mended, but bet­ter than us­ing Thread.sleep().

PageLoad­Time­out wait: This wait is fo­cused on the time a Web page needs to be loaded – the pageLoad­Time­out lim­its the time that the script al­lots for a Web page to be dis­played be­fore pro­ceed­ing with other op­er­a­tions on the Web el­e­ments present on the page. If the page loads within the time, then the script con­tin­ues. If the page does not load within the time­out, the script will be stopped by a Time­outEx­cep­tion.

Dec­la­ra­tion:

driver.man­age().time­outs().pageLoad­Time­out(50, TimeUnit. SEC­ONDS);

When to use: When­ever you need to en­sure a page is loaded in the op­ti­mum time be­fore pro­ceed­ing to per­form an op­er­a­tion on We­bEle­ments.

SetScrip­tTime­out: This wait is ap­pli­ca­ble only for Javascrip­tEx­ecu­tor, which pro­vides us two meth­ods for script ex­e­cu­tion — ex­e­cuteScript and ex­e­cuteAsyncScript. The ex­e­cuteScript method will block the ex­e­cu­tion of the next line of code till the ex­e­cu­tion of this method is com­pleted, and then it moves to the next line of code. The ex­e­cuteAsyncScript method does not block the ex­e­cu­tion of the next line of code till the ex­e­cu­tion of this method is com­pleted.

Since the ex­e­cuteAsyncScript method doesn’t block the ex­e­cu­tion of the next line of code, we use driver.man­age(). time­outs().setScrip­tTime­out(5,SEC­ONDS); to spec­ify the amount of time for an asyn­chro­nous script to fin­ish the ex­e­cu­tion be­fore throw­ing an er­ror.

Dec­la­ra­tion:

driver.man­age().time­outs().setScrip­tTime­out(5, TimeUnit. SEC­ONDS);

When to use: Used to spec­ify the amount of time for an asyn­chro­nous script to fin­ish ex­e­cu­tion, or else it will take its own time to ex­e­cute and may al­low a bug to slip away. Flu­en­tWait: Each Flu­en­tWait in­stance de­fines the max­i­mum amount of time to wait for a con­di­tion, as well as the fre­quency with which to check the con­di­tion. The user may con­fig­ure the wait to ig­nore spe­cific types of ex­cep­tions while wait­ing, such as NoSuchEle­men­tEx­cep­tions when search­ing for an el­e­ment on the page, and to ig­nore the ex­cep­tion to con­tinue with script ex­e­cu­tion in case the el­e­ment is not found.

Dec­la­ra­tion:

Wait<We­bDriver> wait = new Flu­en­tWait<We­bDriver>(driver) .with­Time­out(30, TimeUnit.SEC­ONDS).pollingEvery(5, TimeUnit.SEC­ONDS).ig­nor­ing(NoSuchEle­men­tEx­cep­tion.class);

We­bEle­ment click­se­le­ni­um­link = wait.un­til(new Func­tion<We­bDriv er, We­bEle­ment>() {pub­lic We­bEle­ment ap­ply(We­bDriver driver) {

re­turn driver.find­Ele­ment(By.xpath(“//*[@id=’login­but­ton’]”));

} });

When to use: Used when the el­e­ment takes a dif­fer­ent load­ing time to ap­pear each time we run the test.

Explicit wait: This al­lows the user to di­rect the driver to wait for cer­tain con­di­tions to oc­cur be­fore pro­ceed­ing fur­ther in the code. We­bDriverWait by de­fault calls the Ex­pect­edCon­di­tion ev­ery 500 mil­lisec­onds un­til it re­turns suc­cess­fully.

Dec­la­ra­tion:

We­bDriverWait wait = new We­bDriverWait(driver, 10); wait.un­til(Ex­pect­edCon­di­tions.el­e­men­tToBeClick­able(By. xpath(“//*[@id=’lo­gin- but­ton’]”))).click();

When to use: When the el­e­ment takes a long time to load and also when there is a need to check the prop­erty of an el­e­ment (e.g., el­e­ment is click­able).

Ef­fec­tive use of waits in a real world

Us­ing a com­bi­na­tion of pageLoad­Time­out + SetScrip­tTime­out and Ex­plic­itWait in your frame­work en­sures that the page is loaded at the op­ti­mal time, and that all asyn­chro­nous scripts are ex­e­cuted in a stip­u­lated time. Ex­plic­itWait helps to check the var­i­ous prop­er­ties of dif­fer­ent el­e­ments based on the type of op­er­a­tion that needs to be per­formed. If there are any fail­ures, it’s easy to find the root cause for them. Us­ing this com­bi­na­tion of waits in your frame­work can de­liver the most ro­bust and ac­cu­rate results.

Ref­er­ence

http://www.se­le­ni­umhq.org/docs/

Fig­ure 1: Types of waits

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.