LeanFT Java Tutorial Part 2 – Create First Test

In case you missed on part 1, check it out here.

Now that we are familiar with the code skeleton, we can start writing our first test. I’m taking a basic scenario where we are searching for Leanft on my blog. For doing this, firstly I’ll have to import the web package in LeanFT SDK because I want to automate a web application. Here’s how it needs to be done:-

import com.hp.lft.sdk.web.*;

Now I’ll declare a Browser object as an instance variable to my LeanFtTest class like this:-

public class LeanFtTest extends UnitTestClassBase {
 //The browser object that will be used in the tests
 Browser browser;

After that, we can use the @Before annotation to launch a browser so that a fresh browser will open before each test. This can be done as follows:-

 public void setUp() throws Exception {
 browser = BrowserFactory.launch(BrowserType.CHROME);

Here, BrowserFactory is a class provided by LeanFT SDK’s Web package. The launch method can be used to launch either IE, Chrome, Firefox or Edge. For version support, you can have a look at the availability matrix here (you will need an HP Passport account for this).

Similar to @Before, I’ll be using the @After notation to denote a method which closes the browser instance as follows:-

public void tearDown() throws Exception {

Now I just need to write a test method which will contain my test logic. For the sake of this tutorial, I’ll be going to a search engine called “DuckDuckGo” and then I’ll search for learn2automate. Once my blog opens, I’ll be searching for LeanFT and see that there should be a link that contains the text “LeanFT”. Here’s the full code for doing this:-

package l2a;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.hp.lft.report.*;
import com.hp.lft.sdk.*;
import com.hp.lft.sdk.web.*;

import unittesting.*;

public class LeanFtTest extends UnitTestClassBase {
 //The browser object that will be used in the tests
 Browser browser;

 public LeanFtTest() {
 //Change this constructor to private if you supply your own public constructor

 public static void setUpBeforeClass() throws Exception {
 instance = new LeanFtTest();

 public static void tearDownAfterClass() throws Exception {

 public void setUp() throws Exception {
 browser = BrowserFactory.launch(BrowserType.CHROME);

 public void tearDown() throws Exception {

 public void test(){
 //Navigate to Search Engine

 //Search for Learn2Automate blog
 EditField txtSearch = browser.describe(EditField.class, new EditFieldDescription.Builder()

 Button btnSearch = browser.describe(Button.class, new ButtonDescription.Builder()

 //Select the first search result
 Link firstResult=browser.describe(Link.class, new LinkDescription.Builder()
 .tagName("A").innerText(new RegExpProperty(".*learn2automate.*")).index(0).build());
 Assert.assertTrue("Result with text - \"learn2automate\" should be present",firstResult.exists(10));

 //Search for LeanFT if the blog opens
 EditField searchBox = browser.describe(EditField.class,new EditFieldDescription.Builder()
 Assert.assertTrue("Search box should exist",searchBox.exists(10)); 


 Button goButton=browser.describe(Button.class, new ButtonDescription.Builder()

 //Verify that the blog entry with title LeanFT opens
 Link blogResult = browser.describe(Link.class,new LinkDescription.Builder()
 .color("rgb(0, 102, 204)").tagName("A").innerText(new RegExpProperty(".*LeanFT.*")).index(0).build());
 Assert.assertTrue("Blog entry with text - leanft should appear",blogResult.exists(10)); 
 catch(Exception ex)
 Assert.fail("Unexpected error while looking for LeanFT on Learn2Automate - " + ex.getMessage());


Lets understand it now. First of all we start with the try block. If you have been using UFT, you must be familiar with the pain of using the old school “On Error Resume Next”, so we can counter that here by using try catch block. In case you are new to programming, it would be better if you read about exception handling in Java.

Then we are using the good old navigate function on the browser object that we created in the @Before method to navigate to the DuckDuckGo website.

After that, we create the edit box object where we will set the search term that we are looking for. In case you want to learn how we can create this statement, you should check out my post on using LFT’s Object Spy.

After setting the search term and clicking on the go button, I create the object for the result item and put in an assertion to verify that it should exist. Please note that this assertion enables me to exit the test method if the firstResult object is not found in a matter of 10 seconds.

Similar to the above approach, upon clicking the first result, I look for the search button on my blog and look for the term leanFT and verify the search result similarly. You must have noticed that LFT does not have any explicit wait statement, but we have to use the exists method on an object to do the synchronization.

One thing worth noticing here is that all my reporting is being done with JUnit Assertions. An alternative to this can be the Reporter object that is provided with LeanFT’s SDK. This can be used to create an HTML report of our test which is stored in the RunResults folder inside our Java project. In order to use the Reporter object, you need to call the following method:-

Reporter.reportEvent("Blog entry with text - leanft should appear", "Blog Found", Status.Passed);

In order to run your test, just right click your Test class and Run as JUnit Test.

I hope by now you are able to create a basic script in LeanFT. If you have doubts, feel free to comment !

LeanFT Java Tutorial Part 3 – Object Identification Center

Happy Automating,

Harshit Kohli


20 thoughts on “LeanFT Java Tutorial Part 2 – Create First Test

  1. Hi Harshit, thanks for the nice post. I am wondering whether the example, though instructive, is a typical use case for LeanFT: it seems that a record-and-play framework for web interfaces would allow to do the same with a lower effort (of course it is handy to be able to declare the browser type in one line). Do you have any view on this?

    1. Agree with your point here that Record-and-Play(assuming you are talking about UFT) would do this with lesser effort but following are the issues that you might face in the longer run:-

      1. Exception Handling and Debugging – Modern programming languages have proper Exception management which is lacking in record-playback sort of scripts.
      2. Code Management – this is a very basic example which is not following the guidelines of a proper framework like Page Object Model etc.
      3. 3rd Party Libs – You are now working in a broader ecosystem and you can gain access to a lot of libraries that other people have created(example Log4Net)

      There are many more points but I hope you got what I’m trying to say here 🙂

  2. Hi, I have a question for you. I am new to LeanFT and I am trying a simple sendKeys(RETURN) option. i am not sure which object to associate to? I have a webpage and I set a value to a field and I need to press “Enter” on the keyboard. There is no search icon or anything. I just set the value and press “Enter” on keyboard. How do I do that with LeanFT

    1. Hi Shyam, as per my knowledge there are two ways to do what you are trying to achieve:-

      The ideal way would be to use FireEvent method which LeanFT provides. Maybe you can go through the documentation and figure out the exact code.

      On the other hand, you can use some of the existing Java API’s like Robot. You can find an example here – http://stackoverflow.com/questions/11442471/press-a-key-with-java

      Please try these options. Also, do let me know in case you figure out another way 🙂

    1. below is code.

      public void test() throws GeneralLeanFtException, AWTException {

      EditField txtVar = browser.describe(EditField.class, new EditFieldDescription.Builder()



      after i set the value in the text field, i need to press Enter to display the data

      1. Hi Harshit

        I am able to do the press Enter key with robot api. I was not capturing the pixel location accurately.

  3. Harshit,


    Nice Articles.

    I am working on converting my UFT scripts to leanFT based. Please suggest on following things

    1. I am able to launch Browser ,and I can see title of page but contents of page is not getting loaded
    2. I have also used Browser.Attach and used Title

    Do we need IE drivers. I tried with downloading IE Driver 2.9 Jar ,but it rather was throwing exception.

    3. Like in UFT, we have Add-ins which helps identifying a page ,is that is required.

    1. Hi Aditya,

      In order to check whether the browser is being identified with LeanFT or not, you need to first make sure that LeanFT Runtime Engine is running, and the browser should have the HP Unified Functional Testing Extension installed (in case of IE, its BHO Manager).

      The extensions get downloaded automatically when you launch the browser the first time after you install leanft. You might need to enable the extensions after installation.

      After your Extension is installed and enabled, make sure that the version of your browser is compatible with version of LeanFT. You can refer the LeanFT Product Availability Matrix for that.

  4. browser.describe(EditField.class, new EditFieldDescription.Builder()

    my above code is not working. It is Prompting an error message. The method type(String) is undefined for the type EditFieldDescription.Builder

  5. The error method suggests that the EditFieldDescription class is not coming from the right package. Please check the import statements on the top of the Test class and make sure that everything is coming from com.hp.lft.sdk.web rather than some other package. Best way out would be to add the following import and remove all unnecessary ones:-

    import com.hp.lft.sdk.web.*

    Please let me know if this works.

  6. Hey
    I am also getting the same error :The method type(String) is undefined for the type EditFieldDescription.Builder
    Can some one please suggest the best approach to resolve this issue?

    1. Hi Saleem,

      As suggested in the comment above, just make sure you have the right import statement at the top of your Test class. This errors comes because your IDE implicitly adds these statements in your class.

  7. Hi Harshit
    I am using Eclipse ide to practice the above example, I think eclipse ide is causing the issue
    are you suggesting me to install Jboss ide?to resolve this issue?

  8. Hi Harshit,
    First of all thank you for the very informative blog about LeanFT.
    Could you please give insight on how to automate java application using LeanFT.
    We are using .bat file to launch the application. Could you please share some code to launch application.


    1. Hey Sreejith…you can automate applications launched from bat file easily…just need to keep in mind couple of things:-

      1. Launch the application after the LeanFT Runtime engine is launched.
      2. Make sure the Java option is selected in the Runtime engine settings.
      3. Check that the application objects are recognized by the Object Identification Center.

      You’re good to go once the above steps are performed successfully 🙂

  9. Is there any way we can run our junit runner class in a specific port rather than the default port 5095 , I have hard time trying to change the leanft config since of admin restrictions . Is there any specific leanft sdk class that can set port at runtime.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s