LeanFT

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 🙂

LeanFT Java Tutorial Part 8 – Running Tests Remotely

Happy Automating !
Harshit Kohli

Advertisements

Categories: LeanFT

Tagged as: , , ,

5 replies »

    • Hi Deva,

      Here’s one way to do an offset click:-

      1. Get the X and Y coordinates of the Insight Object.
      2. Perform a click with with native C#/Java code rather than LeanFT code once you have the location (i did this for C# successfully but not sure for Java).

      The other approach that you can try is Visual Relation Identifiers to access the required object that you want to click on.

      Thanks,
      Harshit

  1. Hi Harshit,

    We are using insight objects in leanft and the objects were identified and the scripts were running last week, but all of a sudden leanft is not able to identify the objects any more. We are sure that nothing have been changed which we are aware of. Could you pls let me know what could be the issue.

    Thanks
    Kishore

    • Hi Kishore,

      Some of the probable reasons that I can think of:-

      1. Change in machine resolution – Try adjusting the similarity value of the objects.
      2. Change in application – some changes to the look and feel might have caused this.
      3. Problem with LeanFT – Try using some other machine/version of LeanFT to make sure whether this is related to LeanFT or not.

      Thanks,
      Harshit Kohli

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