Page 1 of 2

Validate.Exists throws exception

Posted: Tue Nov 15, 2011 8:24 am
by anja
Hi,

I tried to use the Validate.Exists method to validate if a table is empty.
I check if the first row could not be found. Within the Ranorex API I found the Validate.Exists Method.

Following codesnipped I use to validate:

Code: Select all

bool exists = Validate.Exists(<RepoItem>, "No entry '{0}'", false);
Like described in the online API description, the last parameter says if an exception should be thrown or not.
In my case an exception is thrown, no matter if I put there false or true...

The same happens if I use the method Validate.NotExists. Could you please help me how to verify if an element is not there?

Thank you
Anja

Re: Validate.Exists throws exception

Posted: Tue Nov 15, 2011 10:02 am
by Support Team
Hi,

by using the Exists/NotExists method with the exceptionOnFail flag set to false you will get a Failure, but the Test Case will execute the code after the validation line, which means there will not be thrown an exception.
By setting the tag to true, the Test Case will stop directly after the validation method, as there will be thrown an exception.

If you just want to report if an item exists or not without making your Test Case fail you can use following method:
RepoItemInfo.Exists()
which can be used e.g. in reporting as follows:
Report.Info("Exists = " + repo.YourAppFolder.YourRepoItemInfo.Exists().ToString());
Regards,
Tobias
Support Team

Re: Validate.Exists throws exception

Posted: Tue Nov 15, 2011 10:11 am
by sdaly
What is the message of the exception?

btw, Validate.Exists does not return a boolean I don't think!

Re: Validate.Exists throws exception

Posted: Tue Nov 15, 2011 10:32 am
by Support Team

Re: Validate.Exists throws exception

Posted: Tue Nov 15, 2011 10:41 am
by sdaly
Ah, nice! You learn something new every day!

Re: Validate.Exists throws exception

Posted: Wed Nov 16, 2011 2:52 pm
by anja
Hi,

thank you for this fast reply...
I retried the validate.exists and in my case the exception was thrown even if I set the exceptionOnFail flag to false....

The ReportItemInfo.Exists() Method does the trick for me... Thank you for this hint...

Greetings
Anja

Re: Validate.Exists throws exception

Posted: Fri Jul 26, 2013 9:04 am
by Patrick Kunst
Hi,
I have the same problem.
When I try to check if a dialogue exists like this

Code: Select all

if(Validate.Exists(repo.DubletteUebernehmen.ButtonJa,null, false))
			{
				//Do some stuff
			}
An error occurs if the element dose not exists and the tests fails.
The error posted to the log is:
No element found for path '/form[@title='Dublette übernehmen?']' within 30s.
The folder 'StandardtestRepository.DubletteUebernehmen' was not found.
Failed to find item 'StandardtestRepository.DubletteUebernehmen.ButtonJa'.
Now I found this post and I tried it with

Code: Select all

RepoItemInfo.Exists()
But this does not work either.
I only can found the methods .Equals() and .ReferenceEquals()


Edit:
Now I tried this.

Code: Select all

Ranorex.Duration d = 20000;			
			if(Validate.Exists(repo.DubletteUebernehmen.ButtonJaInfo.AbsolutePath,d,null, false))
			
			{
				//Do some stuff
			}
No Erorr is posted to the log but a failure. Is it possible to not post the failure because the test is now shown as failed. But it succeeded.

Re: Validate.Exists throws exception

Posted: Mon Jul 29, 2013 3:51 pm
by Support Team
Hello,

You could e.g. try to use the following method to disable the failure in your report:
if(Validate.Exists(repo.DubletteUebernehmen.ButtonJaInfo, null, new Validate.Options(false, ReportLevel.Info)))
{
	//Do some stuff
}
Regards,
Markus (T)

Re: Validate.Exists throws exception

Posted: Mon Sep 16, 2013 9:13 am
by brgibb
I too want to prevent the report from stating an error when the Validate.Exists methods fails to find an element but the exception has been turned off via the Boolean.... an Info message would be fine.

The thing is I also want to do this check specifying a Duration Search Timeout like so:

Result = Ranorex.Validate.Exists(RXPath, DefaultSearchTimer, "{0}", ThrowException);

Is it possible to combine the "new Validate.Options(false, ReportLevel.Info)" in this scenario?

Many thanks,

Re: Validate.Exists throws exception

Posted: Tue Sep 17, 2013 1:00 pm
by Support Team
Hi,

Yes, this is possible.
For more information about the different Exists methods and the Validate.Options please see the following links: Validate.Exists and Validate.Options.
The following should work for you: "Exists(RxPath, Duration, String, Validate.Options)".

Regards,
Markus

Re: Validate.Exists throws exception

Posted: Wed Sep 18, 2013 10:25 am
by brgibb
Hi Markus,

Thanks for your post but how do I incorporate the Throwexception into this?

I do not always want an exception to be thrown and in this scenario I do not want the report to contain an error. I also want to use the duration for the exists check.

So that's:

RXPath
DefaultSearchTimer
"{0}"
ThrowException
Validate.Options(false, ReportLevel.Info)

Can ALL of these things be combined and if so would it simply be this?

Ranorex.Validate.Exists(RXPath, DefaultSearchTimer, "{0}", new Validate.Options(false, ReportLevel.Info));

Re: Validate.Exists throws exception

Posted: Wed Sep 18, 2013 4:04 pm
by Support Team
Hi,

Yes, your code should work!

Regards,
Markus

Re: Validate.Exists throws exception

Posted: Wed Aug 19, 2015 4:50 pm
by sherinjab
Hi,

Even I'm facing the same issue where I want to validate if a control exists or not and I don't want the code to exception if it cannot find the control but just return the boolean value as false, which I need to use for further verification. I'm using the below code to achieve it

Code: Select all

bool isError = Validate.Exists("/form[@title='Hubble Information']","Error info",false);
But I find that the code exceptions with the error "No element found for path '/form[@title='Hubble Information']' within 10s.", regardless of the value as false/true for exceptionOnFail.
I'm not using Ranorex Studio and couldn't find ReportItemInfo while using the code on Visual Studio. And anyway I need to know if the control exists or not in a boolean value to proceed with my test. So I don't think ReportItemInfo would be useful in my case. Is there any other way I can make this work?

Thanks,
Sherin

Re: Validate.Exists throws exception

Posted: Wed Aug 19, 2015 8:13 pm
by krstcs
If you want to get a boolean back, do not use the Validate class. You aren't validating, you are checking for existence.

Use the RepoItemInfo object for the repo item you are looking for with it's Exists() method.

So, if your repo item is 'MyItem', the RepoItemInfo object would be "MyItemInfo" and you would just add ".Exists()" to it in code.

Code: Select all

if (MyItemInfo.Exists()) { //do stuff here }

Re: Validate.Exists throws exception

Posted: Thu Aug 20, 2015 10:02 am
by sherinjab
Hi,

Thanks for the clarification on when to use Validate class but I don't use Ranorex Repository object at all in my code but only access elements using its RxPath using Visual Studio. Find e.g. below:

Code: Select all

     
GenericProperties.IsTemplateLoadError = Validate.Exists("/form[@title='Hubble Information']","",false); 
 if (GenericProperties.IsTemplateLoadError)
                {
                    form = "/form[@title='Hubble Information']";
                    Ranorex.Text errorMessage = form.FindSingle<Ranorex.Text>("/form[@title='Hubble Information']/text[@controlid='65535']");
                    //exceptionText = errorMessage.Caption;
                    Mouse.Click(form.FindSingle<Ranorex.Button>("/form[@title='Hubble Information']/button[@text='OK']"));
                    throw new Exception(errorMessage.Caption); }
);


How can I check for the form's existence in this case?

Thanks,
Sherin