Use outcome of image based validation as a condition?

Class library usage, coding and language questions.
kmck
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Use outcome of image based validation as a condition?

Post by kmck » Wed Aug 07, 2013 9:26 pm

I have a checkbox object that Ranorex does not see as a checkbox object, so the Checked.Equals(true) option is not valid. What I was thinking of doing instead was to use image based validation to check if the checkbox is checked, and if not, double click the item to make sure it's checked.

Is there a way to use image based validation pass/fail outcome in a conditional statement? I have been toying around a bit with it, but can't seem to come up with a solution.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Use outcome of image based validation as a condition?

Post by Support Team » Thu Aug 08, 2013 4:06 pm

Hello,

You could use image-based validation to check if a checkbox is clicked or not.
Simply create a snapshot of your Repository Element (checked or unchecked) and use 'Validate.CompareImage' in your Recording to compare your element with your snapshot.
This will generate a region 'Image Feature Data' in your <Recording>.cs file.
You might need to adjust the similarity in the properties of your validation action.

You could use the following User Code:
bool isChecked = Validate.CompareImage(<RepoItemInfo>, <ScreenshotChecked>, new Imaging.FindOptions("yourSettings"), string "YourMessage", new Validate.Options("yourSettings"));
Another method would be to store your screenshot on your file system and use Imaging.Compare:
bool isChecked = Imaging.Compare(<RepoItem>, <Bitmap>, findOpt);
You could use a simple IF-statement to do some action if checkbox is checked.

Regards,
Markus (T)
.
Image

kmck
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Re: Use outcome of image based validation as a condition?

Post by kmck » Mon Aug 12, 2013 1:04 pm

Hello Markus,

Thank you for getting back to me. Unfortunately when I try to make the image validation a bool, I get the compile error: "Cannot implicitly convert type 'void' to 'bool' (CS0029)."

I'm also using the ContainsImage validation as opposed to CompareImage if that makes a difference.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Use outcome of image based validation as a condition?

Post by Support Team » Tue Aug 13, 2013 4:04 pm

Hello,

It looks like that you use a method that doesn't return a bool value.
Please take a look at the several ways to use Validate.CompareImage in our API documentation.

Regards,
Markus (T)
.
Image

kmck
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Re: Use outcome of image based validation as a condition?

Post by kmck » Tue Aug 20, 2013 10:17 pm

Hi Markus,

Thanks for your reply, the API really helped but I'm now stuck on another point.

I got the validation to work with Validate.ContainsImage with the following code:

Code: Select all

var diskCleanup = repo.DiskCleanupForC.Table1000;

try {
    //Validate if 'Downloaded Program Files' is checked.
    bool valDPF = Validate.ContainsImage(diskCleanup.DownloadedProgramFilesInfo, DownloadedProgramFiles_Screenshot1, DownloadedProgramFiles_Screenshot1_Options, Validate.DefaultMessage, false);

What this code is doing is checking the items in Windows' "Disk Cleanup", and it works fine if the item is NOT checked, but if it's checked, it hard stops the rest of the items (even though I have it within a try/catch block) and I get the following report error:

Code: Select all

Image not found in element {Cell: Downloaded Program Files}
Last edited by kmck on Wed Aug 21, 2013 2:31 pm, edited 1 time in total.

kmck
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Re: Use outcome of image based validation as a condition?

Post by kmck » Wed Aug 21, 2013 2:31 pm

Actually, I used Validate.CompareImage instead of Validate.ContainsImage and it works fine now :)

I guess my last question would be, is there a way to suppress the module failure if the validated image doesn't exist? I want it to bind the validation to the Boolean and use that Boolean variable to determine if another block of code should execute, but I don't want it to fail the test case. I've tried using a try/catch block, but it doesn't seem to be doing the trick.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Use outcome of image based validation as a condition?

Post by Support Team » Fri Aug 23, 2013 3:07 pm

Hi,

You could for instance change the Validate Options:
Validate.Options options = new Validate.Options();
options.ExceptionOnFail = false;
options.ReportLevelOnFailure = ReportLevel.Info; // you can also set it to another level
            
bool returnValue = Validate.CompareImage(diskCleanup.DownloadedProgramFilesInfo, DownloadedProgramFiles_Screenshot1, DownloadedProgramFiles_Screenshot1_Options,Validate.DefaultMessage, options);
       
if(returnValue == false){
            	
     Report.Info("The Image doesn't match!");
            	
}
Regards,
Markus
.
Image

kmck
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Re: Use outcome of image based validation as a condition?

Post by kmck » Wed Aug 28, 2013 7:43 pm

Hi Markus,

Thank you for that! Using the new instance of Validate.Options worked great.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Use outcome of image based validation as a condition?

Post by Support Team » Thu Aug 29, 2013 7:36 am

FYI: If you don't want a validation to be performed (-> no message logged and no exception thrown), you can also use the Imaging Compare and Contains methods directly.
Those methods will just return a bool value and not throw an exception or log anything to the report.

Regards,
Alex
Ranorex Team
.
Image

daniel147
Posts: 2
Joined: Fri Dec 06, 2013 3:45 am

Re: Use outcome of image based validation as a condition?

Post by daniel147 » Fri Dec 06, 2013 4:09 am

The Validate Options works well in my application. Thanks!