In your day to day testing activity, you might have come across a few objects in the application under test whose properties are dynamic in nature and keep on changing every time. We can not use static object properties to identify such objects. The test script will fail in every iteration because it won’t be able to find the object with the required properties in the test application. Handling such objects are quite challenging. In such a situation regular expressions are quite instrumental. We can replace the hard-coded property using a regular expression to match the dynamic object properties. A regular expression is also known as regex or regexp represents a pattern that is used to match character combinations in strings.
In this article, you will learn how to use regular expression in LeanFT now known as UFT Developer to identify objects with the dynamic property. Here I have shown an example of identifying a web element that displays the date in the find flight search result page of the Mercury Tour demo application. The text property of the date object is a dynamic property as it will change every day.
Example of Using Regular Expression 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 |
import org.testng.annotations.Test; import com.hp.lft.report.ReportException; import com.hp.lft.sdk.GeneralLeanFtException; import com.hp.lft.sdk.RegExpProperty; import com.hp.lft.sdk.web.Browser; import unittesting.UnitTestClassBase; import com.hp.lft.sdk.web.*; public class REGEXPLeanFT extends UnitTestClassBase { Browser browser; @Test public void test() throws GeneralLeanFtException, ReportException { 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.setSecure("mercury"); Image signInImage = browser.describe(Image.class, new ImageDescription.Builder() .alt("Sign-In") .tagName("INPUT") .type(com.hp.lft.sdk.web.ImageType.BUTTON).build()); signInImage.click();//Click Sign-in button Image findFlightsImage = browser.describe(Image.class, new ImageDescription.Builder() .alt("") .tagName("INPUT") .type(com.hp.lft.sdk.web.ImageType.BUTTON).build()); findFlightsImage.click();//Click on Find Flight button //Using RegExpProperty class and passing inner text as regular expression WebElement webElement = browser.describe(WebElement.class, new WebElementDescription.Builder() .accessibilityName("") .className("") .index(0) .innerText(new RegExpProperty("[0-1][0-9]\\/[0-3][0-9]\\/20[0-9][0-9]")) .tagName("FONT").build()); if (webElement.exists()) { System.out.println(webElement.getInnerText()); } } } |
Code Explanation
In the above code innerText property of the Date, web element is being used to identify it, and rather than the hardcoded coded value of a date a regular expression pattern has been used that will match any valid date as per the date format being displayed in the web application.