Validate.Exists throws exception

Class library usage, coding and language questions.
anja
Posts: 67
Joined: Fri Nov 26, 2010 3:39 pm

Validate.Exists throws exception

Post by anja » Tue Nov 15, 2011 8:24 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Validate.Exists throws exception

Post by Support Team » Tue Nov 15, 2011 10:02 am

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

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Validate.Exists throws exception

Post by sdaly » Tue Nov 15, 2011 10:11 am

What is the message of the exception?

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


User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Validate.Exists throws exception

Post by sdaly » Tue Nov 15, 2011 10:41 am

Ah, nice! You learn something new every day!

anja
Posts: 67
Joined: Fri Nov 26, 2010 3:39 pm

Re: Validate.Exists throws exception

Post by anja » Wed Nov 16, 2011 2:52 pm

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

Patrick Kunst
Posts: 9
Joined: Thu Jul 25, 2013 3:29 pm

Re: Validate.Exists throws exception

Post by Patrick Kunst » Fri Jul 26, 2013 9:04 am

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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Validate.Exists throws exception

Post by Support Team » Mon Jul 29, 2013 3:51 pm

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)

brgibb
Posts: 39
Joined: Wed Jun 05, 2013 2:15 pm

Re: Validate.Exists throws exception

Post by brgibb » Mon Sep 16, 2013 9:13 am

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,

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Validate.Exists throws exception

Post by Support Team » Tue Sep 17, 2013 1:00 pm

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

brgibb
Posts: 39
Joined: Wed Jun 05, 2013 2:15 pm

Re: Validate.Exists throws exception

Post by brgibb » Wed Sep 18, 2013 10:25 am

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));

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Validate.Exists throws exception

Post by Support Team » Wed Sep 18, 2013 4:04 pm

Hi,

Yes, your code should work!

Regards,
Markus

sherinjab
Posts: 23
Joined: Wed Oct 08, 2014 2:57 pm

Re: Validate.Exists throws exception

Post by sherinjab » Wed Aug 19, 2015 4:50 pm

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

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Validate.Exists throws exception

Post by krstcs » Wed Aug 19, 2015 8:13 pm

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 }
Shortcuts usually aren't...

sherinjab
Posts: 23
Joined: Wed Oct 08, 2014 2:57 pm

Re: Validate.Exists throws exception

Post by sherinjab » Thu Aug 20, 2015 10:02 am

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