Page 1 of 1

Use outcome of image based validation as a condition?

Posted: Wed Aug 07, 2013 9:26 pm
by kmck
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.

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

Posted: Thu Aug 08, 2013 4:06 pm
by Support Team
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)

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

Posted: Mon Aug 12, 2013 1:04 pm
by kmck
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.

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

Posted: Tue Aug 13, 2013 4:04 pm
by Support Team
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)

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

Posted: Tue Aug 20, 2013 10:17 pm
by kmck
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}

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

Posted: Wed Aug 21, 2013 2:31 pm
by kmck
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.

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

Posted: Fri Aug 23, 2013 3:07 pm
by Support Team
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

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

Posted: Wed Aug 28, 2013 7:43 pm
by kmck
Hi Markus,

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

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

Posted: Thu Aug 29, 2013 7:36 am
by Support Team
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

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

Posted: Fri Dec 06, 2013 4:09 am
by daniel147
The Validate Options works well in my application. Thanks!