LeanFT C# 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 Inisght 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:-


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK;
using HP.LFT.SDK.Web;
using HP.LFT.SDK.Insight;
using System.Drawing;

namespace LeanFtTestProject1
{
    [TestClass]
    public class LeanFtTest : UnitTestClassBase<LeanFtTest>
    {
        IBrowser browser;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            GlobalSetup(context);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            browser = BrowserFactory.Launch(BrowserType.Chrome);
        }

        [TestMethod]
        public void Test_Insight()
        {   
            //Navigate to Bing
            browser.Navigate("www.bing.com");
            browser.Sync();

            //Search for Learn2automate
            browser.Describe<IEditField>(new EditFieldDescription
            {
                XPath = @"//INPUT[@id=""sb_form_q""]"
            }).SetValue("Learn2automate");

            //Load the Image
            Image img = Image.FromFile(@"C:\BingSearch.PNG");

            //Create the Insight Object
            var objSearch = browser.Describe<IInsightObject>(new InsightDescription(img));

            Assert.IsTrue(objSearch.Exists(5),"Search Icon should be present");

            //Click on the Object if its found            
            objSearch.Click();          
            
        }

        [TestCleanup]
        public void TestCleanup()
        {
            browser.Close();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            GlobalTearDown();
        }
    }
}

LeanFT C# Tutorial Part 8 – Running Tests Remotely

Happy Automating !
Harshit Kohli

Advertisements

Eat, Sleep, Automate. REPEAT !

Tagged with: , , , , , , ,
Posted in LeanFT
27 comments on “LeanFT C# Tutorial Part 7 – Using Insight Objects
  1. […] LeanFT C# Tutorial Part 7 – Using Insight Objects […]

  2. repon says:

    hello. thank you for examples.
    help me, how to work with Openfiledialog of browser – for the load of files. did not find examples

    • Hi,

      I don’t have code handy but i can help you with the process:-

      1. Click the file upload we button as we do for other web objects.

      2. The File download dialog will appear, spy on the object.It should be identified as Std Win object

      3. Select the properties that look unique and copy the code to clipboard or add to application model as you do for web objects.

      4. Similarly identify the upload text box and select button and you are ready with file upload.

      Let me know in case you need any further assistance.

      Thanks,
      Harshit

      • repon says:

        thanks.
        I did not understand. ((
        hp.lft.sdk.stdWin.WindowDescription ?
        ———-
        do not you have possibility to give elementary to look some project on GIT ?

      • Yes I was talking about the StdWin.WindowDescription only.

        I do not have any sample project as of now but thanks for the suggestion, I’ll keep this in mind when I write my next post.

        Thanks,
        Harshit

  3. repon says:

    for me it is difficult to pass with java+selenium on c#+leanft.
    is not there an example with description of Openfiledialog with the choice of file and button Open from this dialog?)

  4. Ibragim says:

    Hi!
    used expectations LeanFt , the same as in Selenium ?

  5. repon says:

    HI!
    you will not prompt, am
    how to pass in it a way to the file?
    if there was selenium – ***.sendKeys(filepath);

    but as in Leanft ?

    • For Insight objects, you can use object.SendKeys(“text”).

      For Web edit, you can use object.SetValue(“text”).

      For just sending keyboard commands, you can use SendKeys.SendWait(“text”) after adding the System.Windows.Forms namespace.

      Thanks,
      Harshit

  6. repon says:

    Hi, rear!

    you worked with ???
    browser.Describe(new CssDesription(“input[type=’file’]”))

    how to choose or specify a way to the file?
    if click() – dialog fails to appear

  7. Denis repon says:

    sorry.
    browser.Navigate(“http://www.softpost.org/selenium-test-page/”);
    browser.Sync();
    var inpfil = browser.Describe(new CSSDescription(“input[type=’file’]”));
    if(inpfil.Exists() && inpfil.IsVisible)){
    inpfil.Click();

    inpfil.SetValue(“D:\\temp\\str2.jpg”);
    Console.WriteLine(inpfil.Value);
    }

    how to choose or specify a way to the file?

    • Whats the error you are getting when you execute the above code ?

      • Denis repon says:

        I showed a bad example((
        code:

        var url2 = “http://www.uniplast-kbe.ru/test/1.php”;
        var css1 = “input#upload_hidden”;
        var css2 = “input#upload_hidden_2”;
        var css3 = “input#upload_hidden_3”;
        var css4 = “input#upload_hidden_4”;

        browser.Navigate(url2);
        browser.Sync();

        browser.Describe(new CSSDescription(css1)).SetValue(filePath);//ok
        browser.Describe(new CSSDescription(css2)).SetValue(filePath);//bad
        browser.Describe(new CSSDescription(css3)).SetValue(filePath);//bad
        browser.Describe(new CSSDescription(css4)).SetValue(filePath);//bad

        //
        how I must choose a file for css2-3-4 ?

  8. Denis repon says:

    browser.Describe(new CSSDescription(css1)).SetValue(filePath);//ok
    browser.Describe(new CSSDescription(css2)).SetValue(filePath);//bad
    browser.Describe(new CSSDescription(css3)).SetValue(filePath);//bad
    browser.Describe(new CSSDescription(css4)).SetValue(filePath);//bad

  9. Denis repon says:

    browser.Describe( IFileField )(new CSSDescription(css1)).SetValue(filePath);//ok
    browser.Describe( IFileField )(new CSSDescription(css2)).SetValue(filePath);//bad
    browser.Describe( IFileField )(new CSSDescription(css3)).SetValue(filePath);//bad
    browser.Describe( IFileField )(new CSSDescription(css4)).SetValue(filePath);//bad

  10. Denis repon says:

    thank you that help…

    css2 – Cannot identify the object “Web.IWebFile”.
    Verify that this object’s properties match an object currently displayed in your application. HP.LFT.SDK в HP.LFT.SDK.Core.ClassModel.TestObjectExecuterBase.HandleReplayError(Int32 errorCode, IDictionary`2 data)
    в HP.LFT.SDK.Web.EditFieldBase.SetValue(String text)

    css3 – Cannot identify the object “[ photo ]”.
    Verify that this object’s properties match an object currently displayed in your application. HP.LFT.SDK в HP.LFT.SDK.Core.ClassModel.TestObjectExecuterBase.HandleReplayError(Int32 errorCode, IDictionary`2 data)
    в HP.LFT.SDK.Web.EditFieldBase.SetValue(String text)

    css4 – Cannot identify the object “[ files ]”.
    Verify that this object’s properties match an object currently displayed in your application. HP.LFT.SDK в HP.LFT.SDK.Core.ClassModel.TestObjectExecuterBase.HandleReplayError(Int32 errorCode, IDictionary`2 data)
    в HP.LFT.SDK.Web.EditFieldBase.SetValue(String text)

  11. Denis repon says:

    do not you know how it to do? can make an attempt do inputs visible at a help Java Script?

  12. Ruchi Sanwal says:

    how to click on an insight object when multiple same objects are present..insight object does not work then to click on first instance..

  13. rajasaekhar says:

    how to work with webtables sing leanft C# (i.e click on link inside the table, getting rownumbers and child objetcs)

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: