In this tutorial, I will show you how to use XPath in LeanFT.LeanFT has been officially rebranded as UFT Developer by Miro Focus. It has a very powerful mechanism to identify objects in a test application. It can identify objects by using object properties, CSS Selection, and XPath.In Open source tools, XPath is widely used to identify web objects.LeanFT also supports XPath to identify all kinds of web objects. To know more about XPath expressions and how to create them, please refer to my post on XPath in Selenium WebDriver with Example which is a generic article on XPath and is not specific to a specific automation tool. The core concepts of XPath are the same across all automation tools.
Example of Using XPath in LeanFT | UFT Developer
LeanFT is capable enough to uniquely identify objects using its property. However, there might be some scenarios wherein you would like to use XPath to identify a test object.LeanFT gives us the option to use the XPath of the object along with other properties. Nevertheless, you can also use the only XPath to identify an object as a valid XPath alone is sufficient to identify an object. The following example illustrates how to use XPath in LeanFT.In the following test, the script enters the user credential and presses the login button using XPath of the respective objects.
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 |
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.GeneralLeanFtException; 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.Image; import com.hp.lft.sdk.web.XPathDescription; import unittesting.UnitTestClassBase; public class XPATHLeanFTTest extends UnitTestClassBase { @Test public void testMercury() throws GeneralLeanFtException, ReportException { try { //Open the Chrome browser. Browser browser = BrowserFactory.launch(BrowserType.CHROME); browser.navigate("http://newtours.demoaut.com/"); browser.sync(); EditField userNameEditField = browser.describe(EditField.class, new XPathDescription("//input[@type='text' and @name='userName']")); userNameEditField.setValue("mercury"); EditField passwordEditField = browser.describe(EditField.class, new XPathDescription("//input[@type='password' and @name='password']")); passwordEditField.setValue("mercury"); Image signInImage = browser.describe(Image.class, new XPathDescription("//input[@type='image' and @name='login']")); signInImage.click(); } catch(Error e){ System.out.println(e); Reporter.reportEvent("Runtime error occured","Failed during test",Status.Failed, e); throw e; } } } |
You can write your own custom XPath if you are well versed in writing XPath or you can take the help of the Object Identification Centre (OIC) to create the descriptive object using XPath Description. During object identification using OIC, you can uncheck all other properties except XPath to create the object using the XPath description.
You can copy the XPath value in your clipboard and used it in your scripts. To know more about how to use the Object Identification Center please refer to the following posts.
- Object Identification Center in LeanFT | UFT Developer
- Spy Multiple Objects in LeanFT Using Object Identification Center | OIC
Recommended Posts
- Create Application Model Project in LeanFT | UFT Developer & Write First Test in Java
- Create a UFT Developer | LeanFT Testing Project & Write Your First Test
- Overview of Micro Focus UFT Developer | LeanFT
- Intuitive Way of MySQL Database Testing in Selenium | LeanFT
- Read and Write Excel File in Java Using Apache POI Lib