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:-


 @Before
 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:-


@After
public void tearDown() throws Exception {
browser.close();
}

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
 }

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 instance = new LeanFtTest();
 globalSetup(LeanFtTest.class);
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 globalTearDown();
 }

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

 @After
 public void tearDown() throws Exception {
 browser.close();
 }

 @Test
 public void test(){
 try
 { 
 
 //Navigate to Search Engine
 browser.navigate("www.duckduckgo.com");

 //Search for Learn2Automate blog
 EditField txtSearch = browser.describe(EditField.class, new EditFieldDescription.Builder()
 .type("text").tagName("INPUT").name("q").build());
 
 txtSearch.setValue("learn2automate");

 Button btnSearch = browser.describe(Button.class, new ButtonDescription.Builder()
 .name("S").build());
 
 btnSearch.click();

 //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));
 
 firstResult.click();

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

 searchBox.setValue("leanft");

 Button goButton=browser.describe(Button.class, new ButtonDescription.Builder()
 .name("Go").build());
 
 goButton.click();

 //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)); 
 blogResult.highlight();
 
 
 }
 catch(Exception ex)
 {
 ex.printStackTrace();
 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

Advertisements

Eat, Sleep, Automate. REPEAT !

Tagged with: , , ,
Posted in LeanFT
12 comments on “LeanFT Java Tutorial Part 2 – Create First Test
  1. […] LeanFT Java Tutorial Part 2 – Create First Test […]

  2. Roberto says:

    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?

    • 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 🙂

  3. Shyam says:

    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

  4. Can you please post the code that you used ?

    • Shyam says:

      below is code.

      public void test() throws GeneralLeanFtException, AWTException {
      browser.navigate();
      browser.sync();

      EditField txtVar = browser.describe(EditField.class, new EditFieldDescription.Builder()
      .type(“text”).tagName(“INPUT”).name(“number”).build());

      txtVar.setValue(“xxxxx”);

      }

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

      • Shyam says:

        Hi Harshit

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

  5. Harshit,

    Greetings!

    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.

    • 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.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog Stats
  • 168,130 hits
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: