Synchronization is a very important aspect of testing if you want to make your test scripts robust and efficient. The WaitUntil method in LeanFT now known as UFT Developer is the most effective way to wait for an object until a certain condition is met during execution rather than using hard wait. The following example illustrates how to use waitUntil method in LeanFT (UFT Developer) to dynamically wait for an expected condition to be met. In this example, the dynamic wait is being used to wait until the Sign-In image button exists and it becomes visible when the URL for the Home page is opened.
Example of WaitUntil Method in LeanFT |UFT Developer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import org.testng.annotations.Test; import com.hp.lft.report.ReportException; import com.hp.lft.report.Reporter; import com.hp.lft.report.Status; import com.hp.lft.sdk.*; import com.hp.lft.sdk.WaitUntilTestObjectState.WaitUntilEvaluator; import com.hp.lft.sdk.web.Browser; import com.hp.lft.sdk.web.BrowserFactory; import com.hp.lft.sdk.web.BrowserType; import com.hp.lft.sdk.web.EditField; import com.hp.lft.sdk.web.EditFieldDescription; import com.hp.lft.sdk.web.Image; import com.hp.lft.sdk.web.ImageDescription; import com.hp.lft.verifications.*; import unittesting.*; public class WaitUntilLeanFT extends UnitTestClassBase { Browser browser; @Test public void test() throws GeneralLeanFtException, ReportException { try { browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("http://newtours.demoaut.com/"); EditField userNameEditField = browser.describe(EditField.class, new EditFieldDescription.Builder() .name("userName") .tagName("INPUT") .type("text").build()); userNameEditField.setValue("mercury"); EditField passwordEditField = browser.describe(EditField.class, new EditFieldDescription.Builder() .name("password") .tagName("INPUT") .type("password").build()); passwordEditField.setValue("mercury"); Image signInImage = browser.describe(Image.class, new ImageDescription.Builder() .alt("Sign-In") .tagName("INPUT") .type(com.hp.lft.sdk.web.ImageType.BUTTON).build()); //Wait until Image sign-in button exists and is visible WaitUntilTestObjectState.waitUntil(signInImage,new WaitUntilEvaluator<Image>(){ public boolean evaluate(Image ImgButton){ try{ return ImgButton.exists() && ImgButton.isVisible(); } catch(Exception e){ return false; } } }); Verify.isTrue(signInImage.exists(), "Verify Signin image button", "Verify Signin image button exists."); Verify.isTrue(signInImage.isVisible(), "Verify Signin image button UserInput Control Is Visible", "Verify Signin image button is visible."); signInImage.click(); } catch(Error e){ Reporter.reportEvent("Test failed","Exception occured.Test Failed during execution",Status.Failed, e); throw e; } } } |
Code Explanation
First of all, I have created an object of the Image button for what I want to use dynamic wait.
1 2 3 4 |
Image signInImage = browser.describe(Image.class, new ImageDescription.Builder() .alt("Sign-In") .tagName("INPUT") .type(com.hp.lft.sdk.web.ImageType.BUTTON).build()); |
For the dynamic wait, we will have to use the waitUntil method of WaitUntilTestObjectState class. The waitUntil method requires two parameters, first one the object for which you want to wait. In this case, it is signInImage.For the next one follow the syntax as mention in the code and provide appropriate object type in all places. In this example, I have an Image type of object. If you want to waitUntil for another object type like web element, you can use the “WebElement” object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Wait until the Image sign-in button exists and is visible WaitUntilTestObjectState.waitUntil(signInImage,new WaitUntilEvaluator<Image>() { public boolean evaluate(Image ImgButton) { try { return ImgButton.exists() && ImgButton.isVisible(); } catch(Exception e) { return false; } } }); |
Recommended Posts
- How to Use Regular Expression in LeanFT | UFT Developer
- How To Use XPath in LeanFT | UFT Developer
- How to Send Keystrokes In LeanFT | UFT Developer
- How to Get Table Cell Value in LeanFT | UFT Developer
- Intuitive Way of MySQL Database Testing in Selenium | LeanFT
- Read and Write Excel File in Java Using Apache POI Lib