LeanFT Java Tutorial Part 7 – Using Insight Objects

Insight Object Identification has been one of my favorite features from HP since it was launched with UFT in 2012. I have been using it in a lot of my UFT scripts. In case you are not familiar with this concept, its a way of using Image based identification rather than object based identification that we generally do. It can be reaaaally helpful in following situations:-

1. An Object is not getting identified.
2. An Object does not belong to the supported technology stack, i.e. the underlying technology of the product is not supported (for eg: I used Insight to write a script for iTunes!)
3. You want to execute a script without hooking into the application(sometimes applications become slow on hooking)

Lets take an example of searching something from Bing(although this does not apply to the above 3 rules but taking this for the sake of simplicity).

BingSearchJPG

Here is how you can use Insight in in this case:

Take a cropped snapshot of the yellow search icon and save it in a file( can be BMP,JPG or PNG) and write the following code:-


package l2a;

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.imageio.ImageIO;

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.insight.*;
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.bing.com");

 //Search for Learn2Automate blog
 EditField txtSearch = browser.describe(EditField.class, new EditFieldDescription.Builder()
 .type("search").tagName("INPUT").name("q").build());
 
 txtSearch.setValue("learn2automate");
 
 File imgFile = new File("C:\\Users\\auto_admin\\Desktop\\BingSearchIcon.PNG");
 RenderedImage image = ImageIO.read(imgFile);
 
 //Create Insight Object with 90% similarity
 InsightObject btnSearch = browser.describe(InsightObject.class, new InsightDescription(image,90)); 
 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()
 .tagName("A").innerText(new RegExpProperty(".*leanft.*")).index(0).build());
 
 Assert.assertTrue("Blog entry with text - leanft should appear",blogResult.exists(10)); 
 blogResult.highlight();
 
 LinkDescription targetLinks = new LinkDescription.Builder()
 .href(new RegExpProperty(".*learn2automate.*")).tagName("A").build();
 
 Link[] myBlogLinks = browser.findChildren(Link.class, targetLinks);
 
 Assert.assertTrue("There should be atlease 1 Link present", myBlogLinks.length>0);
 
 
 
 
 }
 catch(Exception ex)
 {
 ex.printStackTrace();
 Assert.fail("Unexpected error while looking for LeanFT on Learn2Automate - " + ex.getMessage());
 }
 }

}
 

Feel free to comment ! Would love to hear what you feel after reading this:)

Happy Automating !
Harshit Kohli

Tagged with: , , ,
Posted in LeanFT

LeanFT Java Tutorial Part 6 – Parameterizing Tests

Since we are working with Junit framework, we’ll have to deal with some of its limitations. One of them being that our test methods can not have parameters passed to them while being called(although there are other ways to do this but they are way too complicated). Lets focus on a simple approach that we can use:-

Our Eclipse project has a leanft.properties file which is used to set some properties for the Runtime engine. We can put our parameters inside this file and read them during execution. A sample properties file in which i have added 2 custom keys looks like this:-

# ===========================================
# SDK Settings
### To change behavior, uncomment relevant options. 
# ===========================================

#The Username to be used while logging in
username="kohli_harshit@learn2automate.com"

#The Password to be used while logging in
password="l2a"

Now if we have a test method which needs to use these keys, our c# code will look something like this:-


 @Test
 public void testLogin(){
 try
 { 
 FileInputStream iStr = new FileInputStream("resources\\leanft.properties");
 Properties prop = new Properties();
 prop.load(iStr);
 
 
 String uName = prop.getProperty("username");
 String pwd = prop.getProperty("password");
 
 //test the login function here
 

This way we can parameterize our JUnit test methods. One thing that I would like to put forward here is that this is a very simple way of doing this and we can do much better with other testing frameworks like TestNG which I’ll be covering in the later posts.

LeanFT Java Tutorial Part 7 – Using Insight Objects

Happy Automating !
Harshit Kohli

Tagged with: , , , , ,
Posted in LeanFT

LeanFT Java Tutorial Part 5 – Finding Child Objects

If you have worked with UFT/QTP, you must be pretty familiar with the ChildObjects method but in case you have not, this method is used to find a collection of objects that match a given set of description properties.

For example, if you google “leanft tutorial”, and you want to find out how many links are redirecting to this blog, you will have to write code in the following way:-


 LinkDescription targetLinks = new LinkDescription.Builder()
 .href(new RegExpProperty(".*learn2automate.*")).tagName("A").build();
 
 Link[] myBlogLinks = browser.findChildren(Link.class, targetLinks);
 
 Assert.assertTrue("There should be atlease 1 Link present", myBlogLinks.length>0);
 

So here the ChildObjects method(from UFT) is replaced by the FindChildren method and the logic more or less remains the same.

Please comment if you face any issues while executing this.

LeanFT Java Tutorial Part 6 – Parameterizing Tests

Happy Automating!
Harshit Kohli

Tagged with: , , , ,
Posted in LeanFT

LeanFT Java Tutorial Part 4 – Application Models

In case you missed out on part 3, here you go:-

LeanFT Java Tutorial Part 3 – Object Identification Center

Basically there are two ways in which we can specify the properties of the objects that will be used in our script:-

1. Descriptive Programming – This is the approach that was demonstrated in the previous tutorial where we specified the object’s properties in the Test Method itself(by copying the object properties from the clipboard).

2. Application Model – Application Model is to LeanFT, what object repository is to UFT. Simply put together, its a collection of Test objects that we can use in our scripts. It acts as a centralized repository which can be shared across different scripts.

Which approach is better is a very subjective question and I will leave that to you to figure out which suits you best. Here is how we can create Application Models in LeanFT:-

1. When you navigate to New-> Other -> LeanFT, then you need to decide between the two:-

  • Application Model Item – You need to create this if your Application model needs to be used inside the designated project only. This will be created just like a class inside a package in your project. If you want to use this in other classes in the same package, you can use it directly and if you want to use this in other class in different package, then you need to import the package of the Application Model and then you can use it.
  • Application Model Project – You need to create this if your Application model needs to be used across multiple projects. For using this in another project, you just need to add your project to the build path and then import the corresponding package and start using the objects.

For the sake of this tutorial, I’ll be covering Application Model Item.

2. Define the name as well as the package of the model. After that, you will see an Application Model tsrx file is created. Now you can start adding objects to it.

3. There are two ways in which we can add the objects:-

a) Specify the object type and properties explicitly by clicking on the + icon on the top left corner:-

AppModel1

b) Open the Object Identification center from the App Model and after selecting the appropriate object and the identification properties, click on the add to App Model icon :-

AppModel3

After you have the required objects in the Application Model, you can refer to these objects as follows:-

AppModelJavaJPG

One thing worth noticing here is that I need to pass the browser instance when I want to create an object of my application model. This will enable LeanFT to understand that all objects in my application model are to be identified with the passed browser object as the parent. Passing browser is required only in case of Web Automation scripts, for other technologies we can create the application model object without passing a context object.

So this is how you can add objects to the application model and use them in your scripts.

LeanFT Java Tutorial Part 5 – Finding Child Objects

Happy Automating !
Harshit Kohli

Tagged with: , , , , , ,
Posted in LeanFT

LeanFT Java Tutorial Part 3 – Object Identification Center

LeanFT’s object identification center offers features that can give UFT’s object spy a run for its money !

You can invoke it by clicking the “HAT” icon on the Eclipse toolbar. After the Spy has opened, you can click on the Start Spying button to spy on a particular object.

In the below example, I spied on the Google search button on the google homepage and it shows all the properties of the button:-

Object Identification Center

After I click on the Sort by Recommended button as well as Edit Mode button, I will get check boxes in front of each property so that I can select which ones I will be using:-

Using Regular Expressions

As you can see above, clicking on the “STAR” icon on the right of a property will make it as a regular expression and we can use the standard ones like .* and others.

After we are done with the property selection, we can click on the highlight button to check whether the properties are fine or not. A New feature here is that if there are multiple objects matching the description, LeanFT will highlight all the objects rather than giving an error.

So now we have decided the properties and confirmed by highlighting, its time to click the Generate code button which will copy the Java equivalent of the code to our clipboard and we can readily use this in the test.

The code will look  something like:-


browser.describe(Button.class, new ButtonDescription.Builder()
 .buttonType("submit").tagName("INPUT").name(new RegExpProperty(".*Search.* ")).build());

LeanFT Java Tutorial Part 4 – Application Models

Happy Automating !

Harshit Kohli

Tagged with: , , , , ,
Posted in LeanFT

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

Tagged with: , , ,
Posted in LeanFT

LeanFT Java Tutorial Part 1 – Introduction

If you are looking for a basic overview of LeanFT, You can check out my post here.

This tutorial will basically get you started with the tool and further things will be covered after that. You will notice that LeanFT uses more or less the same concepts that UFT does but with a better IDE and a cleaner approach.

In case you don’t have LeanFT installed on your machine, you can follow the instructions mentioned in my post here.

Once the installation is sorted, when you launch Eclipse, you will notice that the LeanFT Runtime Engine will start running. The engine needs to be running whenever we want to use LeanFT as all communication goes through it.

Before jumping onto Test script creation, you need to be familiar with a unit testing framework called JUnit. In case you’re not, you can refer to this link. Once you’re done, you can create your first script.

For creating a new Project, go to File->New->Other, you should see a folder for LeanFT. Inside the folder, following types of projects available to you:-

  1. LeanFT Application Model Item – An application model is LeanFT’s way of saying Object Repository(referring to UFT concept here). You should create this project if you want to create a collection of objects of your AUT (read Application under test). You need to choose this whenever you have an existing project and you want to add just a Model class to it.
  2. LeanFT Application Model Project – Contrary to Application Model Item, the Project needs to be created if we want multiple LeanFT projects to share an Application Model. This is on the same lines of Shared Object Repository in UFT whereas Application Model Item is like a Local Object Repository in UFT.
  3. LeanFT JUnit Data Driven Test Case – This will create a new class which uses the Data Driving feature of JUnit. You can use this if you want to fetch your data set from Excel/ALM and then want to run a method iteratively on the set of Data.
  4. LeanFT JUnit Project – This is a JUnit Test project which can have multiple test methods. We’ll be using this as part of this tutorial.
  5. LeanFT JUnit Test Case – If you want to create just a Test case rather than Project, then use this option.

Once you create a new JUnit project, it should look something like :-


package l2a;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.hp.lft.sdk.*;
import com.hp.lft.verifications.*;
import unittesting.*;

public class LeanFtTest extends UnitTestClassBase {

 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 {
 }

 @After
 public void tearDown() throws Exception {
 }

 @Test
 public void test() throws GeneralLeanFtException {
 
 }

}
 

Lets look at the code line by line and understand it. Firstly, we are giving the name of the package(“l2a”) which states that the class that we are about to declare belongs to this package. After that, we are importing some classes and packages that will help us work with LeanFT and JUnit.

Then comes the definition of our class – “LeanFtTest”. This class is extending a class called “UnitTestClassBase” which is provided by HP. Please note that since this is a template provided by HP, a lot of things are already setup for you. So basically this class would help you in getting your LeanFT SDK initialized(and some other operations at the backend) which you don’t need to handle yourself.

Then comes the constructor for our class which is left for us to implement in case we need it. As of now, we can just leave it as it is.

After that, we have the @BeforeClass annotation which denotes that the method succeeding this annotation will be run once before  any of the test methods execute. In the method that follows, HP has done some activities that will setup your LeanFT SDK and initialize your LeanFT Report.

Similar to @BeforeClass, the @AfterClass annotation denotes that the method following it will execute once after all the test methods have executed. Here, the SDK is cleaned up and the Report is generated inside the current working directory within a folder called RunResults.

@Before annotation is used to declare a method that will run each time before a Test Method is executed and similarly goes the @After annotation which runs each time after a Test Method is executed.

@Test annotation denotes that the following method is a Test Method(you can also think of it as a Test Case). This will contain your test logic.

Note that we can use this template to automate any types of application that LeanFT supports – Web, Java,.NET, Windows and many more.

Now since we are familiar with the structure of the JUnit project template, we are in a position to write our first Web automation script which we’ll be covering in the next post.

LeanFT Java Tutorial Part 2 – Create First Test

Happy Automating !

Harshit Kohli

Tagged with: , , , , , ,
Posted in LeanFT
Blog Stats
  • 99,111 hits
Categories

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

Join 713 other followers

Follow

Get every new post delivered to your Inbox.

Join 713 other followers

%d bloggers like this: