Bitmap comparison.

7 posts, 0 answers
  1. SD
    SD avatar
    62 posts
    Member since:
    Apr 2010

    Posted 08 Dec 2010 Link to this post

    Hi Guys, 

    Could you please advice if Webaii 2.0 APIs support some form of Bitmap comparison ? 

    ta,
    SD.
  2. Cody
    Admin
    Cody avatar
    3354 posts

    Posted 10 Dec 2010 Link to this post

    Hello SD,

    Yes, you should be able to use code something like this:

    System.Drawing.Bitmap expectedbmp = new System.Drawing.Bitmap("myExpected.png");
     
    HtmlImage img = Find.ById<HtmlImage>("myImageId");
    System.Drawing.Bitmap actualbmp = img.Capture();
     
    ArtOfTest.Common.PixelMap expected = ArtOfTest.Common.PixelMap.FromBitmap(expectedbmp);
    ArtOfTest.Common.PixelMap actual = ArtOfTest.Common.PixelMap.FromBitmap(actualbmp);
     
    Assert.IsTrue(expected.Compare(actual, 5.0));

    In the above code I am getting the image of a "myImageId" element from the browser and comparing to an expected image stored on disc named "myExpected.png". The comparison is allowing a 5% tolerance meaning there may be up to a 5% difference in the image or else the Assert will fail and cause the test to abort.

    Greetings,
    Cody
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  3. SD
    SD avatar
    62 posts
    Member since:
    Apr 2010

    Posted 12 Dec 2010 Link to this post

    Ta mate that's highly appreciated. 
  4. SD
    SD avatar
    62 posts
    Member since:
    Apr 2010

    Posted 12 Dec 2010 Link to this post

    Hi Cody, 

    I'm getting 'The type of namespace PixelMap does not exist in the namespace ArtOfTest.Common error message.  
    I'm using the following namespaces in the my lib (And using Webaii 2.0)....What am I missing ? 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Drawing;
     
    using ArtOfTest.WebAii.Controls.HtmlControls;
    using ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;
    using ArtOfTest.WebAii.Core;
    using ArtOfTest.WebAii.ObjectModel;
    using ArtOfTest.WebAii.TestAttributes;
    using ArtOfTest.WebAii.TestTemplates;
    using ArtOfTest.WebAii.Win32.Dialogs;
    using ArtOfTest.Common;
     
    using ArtOfTest.WebAii.Silverlight;
    using ArtOfTest.WebAii.Silverlight.UI;
     
    using NUnit.Framework;
    using Core = NUnit.Core;
    using System.Text.RegularExpressions;



    Ta.

  5. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 15 Dec 2010 Link to this post

    Hi Seth,
       I used your exact code:
    Copy Code
    System.Drawing.Bitmap expectedbmp = new System.Drawing.Bitmap("myExpected.png");
     
    HtmlImage img = Find.ById<HtmlImage>("myImageId");
    System.Drawing.Bitmap actualbmp = img.Capture();
     
    ArtOfTest.Common.PixelMap expected = ArtOfTest.Common.PixelMap.FromBitmap(expectedbmp);
    ArtOfTest.Common.PixelMap actual = ArtOfTest.Common.PixelMap.FromBitmap(actualbmp);
     
    Assert.IsTrue(expected.Compare(actual, 5.0));

    and your exact Using statements:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Drawing;
     
    using ArtOfTest.WebAii.Controls.HtmlControls;
    using ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;
    using ArtOfTest.WebAii.Core;
    using ArtOfTest.WebAii.ObjectModel;
    using ArtOfTest.WebAii.TestAttributes;
    using ArtOfTest.WebAii.TestTemplates;
    using ArtOfTest.WebAii.Win32.Dialogs;
    using ArtOfTest.Common;
     
    using ArtOfTest.WebAii.Silverlight;
    using ArtOfTest.WebAii.Silverlight.UI;
     
    using NUnit.Framework;
    using Core = NUnit.Core;
    using System.Text.RegularExpressions;

    I used them in an WebAii NUnit test.
    I only had problems resolving Drawing and the NUnit assemblies but after I added references to System.Drawing, NUnit.Framework and Nunit.Core - I was able to build the test without any problems.

    Are sure you created your project correctly. Here's how I did it:
    I created a new Test Project.
    From the Solution Explorer I right-clicked:
    Add-->New Item-->Test-->WebAii 2010.3-->NUnit

    Everything needed to use PixelMap was added automatically for me and I can't see any reason why it wouldn't be for you. What version of WebAii are you using? Go ahead and upgrade to the latest 12.13 release from here in case you're using anything older. Try creating a new Project/Test - your problem might be resolved.

    Please let me know how it goes!

    Best wishes,
    Stoich
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  6. Murali
    Murali avatar
    1 posts
    Member since:
    Feb 2012

    Posted 19 Jun 2012 Link to this post

    Hi, i tried the same, it throws exception


    ArtOfTest.Common.Design.Exceptions.ExecutionException: Exception thrown executing coded step: '[ImageComapare] : New Coded Step'. ---> System.ArgumentException: Parameter is not valid.
       at System.Drawing.Bitmap..ctor(String filename)
       at TestProject1.WebTest.ImageComapare() in c:\Users\vivid_staff\Documents\Test Studio Projects\SP_DAR130612\WebTest.tstest.cs:line 51
       --- End of inner exception stack trace ---
       at ArtOfTest.WebAii.Design.Execution.ExecutionEngine.InternalExecuteSteps(AutomationStepList stepList, TestResult testResult, Object codeBehindInstance, Boolean& stopExecution)


    This is my Code, have placed the flowers1.png in my project folder.



    System.Drawing.Bitmap expectedbmp = new System.Drawing.Bitmap("flowers1.png");
    HtmlImage img = Find.ById<HtmlImage>("ctl00_PlaceHolderMain_vContentPageContent_MSO_ContentDiv");
    System.Drawing.Bitmap actualbmp = img.Capture();
    ArtOfTest.Common.PixelMap expected = ArtOfTest.Common.PixelMap.FromBitmap(expectedbmp);
    ArtOfTest.Common.PixelMap actual = ArtOfTest.Common.PixelMap.FromBitmap(actualbmp);
    Assert.IsTrue(expected.Compare(actual, 5.0));



  7. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 25 Jun 2012 Link to this post

    Hello Seth,
    you're not feeding the correct parameter to the constructor (see screenshot 1).

    You need to feed a full path to the constructor. So this line:
    System.Drawing.Bitmap expectedbmp = new System.Drawing.Bitmap("flowers1.png");
    needs to change to (for instance):
    System.Drawing.Bitmap expectedbmp = new System.Drawing.Bitmap("c:\\flowers1.png");

    Kind regards,
    Stoich
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top