.Exists not working Properly

Class library usage, coding and language questions.
Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

.Exists not working Properly

Post by Deepak_Singh » Mon Apr 07, 2014 10:51 am

Hi All,

I am doing scripting and using .Exists to check whether the User Is Logged in or not, If not then Perform a Login.
But the problem is when user is not logged then the Scripts run Properly but when the .Exists fails then it does not go to Else ststement instead it gives error regarding "Element not found".
Please Help.


The code is below:

Code: Select all

var repo = TrailRepository.Instance;
            
            if(repo.Explorer.LoginInfo.Exists())
            {	var Login = repo.Explorer.Login;
            	Report.Info("Login","User Already Logged in!!!");
            	Delay.Milliseconds(1000);
            }
            else
            {
            	Report.Info("Login","Not Logged");
            }
            if(repo.Explorer.LogoutInfo.Exists())
            {
            	var Logout = repo.Explorer.Logout;
            	Logout.DoubleClick();
            	var text1001 = repo.Login.Text1001;
            	text1001.Click();
            	Delay.Milliseconds(100);
            	text1001.PressKeys("{Delete}");
            	text1001.PressKeys("Test");
            	Delay.Milliseconds(1000);
            	var text7003 = repo.Login.Text7003;
            	text7003.Click();
            	Delay.Milliseconds(100);
            	text7003.PressKeys("[email protected]");
            	Delay.Milliseconds(1000);
            	var buttonOK = repo.Login.ButtonOK;
            	buttonOK.Click();
            	Delay.Milliseconds(100);
            }
            
            else
            {
            	Report.Info("Hi","Not Logged On");
            }
            
Thanks,
Deepak

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Wed Apr 09, 2014 12:15 pm

Hi,

I tried to look in the API documentation, but I did not find anything about the Info class's Exist() method, so I have to guess:

repo.Explorer.LoginInfo.Exists() does not return false (as we would expect), when the element does not exist, but rather it throws an exception.

So try to change the if-else statements to try-catch statements.

Hope this helps,

Best Regards,
Zoltán Major

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

Re: .Exists not working Properly

Post by krstcs » Wed Apr 09, 2014 1:33 pm

Exists() returns TRUE if the element for the item exists, and FALSE if it does not. However, it will attempt to find the item for the entire time of the SearchTimeout value for the item and all parents.

The exception you are getting is probably due to another issue. If you post the report and a snapshot we might be able to help you figure it out.
Shortcuts usually aren't...

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Thu Apr 10, 2014 12:57 pm

Hi,

This is now serious. I think its the IF statement which is having the problems.
I removed the .exist and used .visible property.
Still same error If the if statement is true then ok but if the If statement is false then still it is trying to search for the element and giving Error.

Please help ASAP.

Regards,
Deepak :(

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Thu Apr 10, 2014 1:21 pm

Hi,

You can try it out, just make a new project and look for a single element if it exists or not.

I just did a small project, and used usercode and codemodule. Both is running fine for me. Write to ranorex support, and send them any information you could, like project itself and a snapshot.

Regards,
Zoltán

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

Re: .Exists not working Properly

Post by krstcs » Thu Apr 10, 2014 1:38 pm

Make sure that the parent object is also existing/visible. This is likely the issue.

Ranorex Spy is your friend. Use it to check the whole path.
Shortcuts usually aren't...

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Fri Apr 11, 2014 10:12 am

Hi All,

I found what was creating problems.

If you drag and drop the object from the Repository and after that check for the existence of the object.
In this scenario if the object is Present then the Test will run without any problem, but if the object is not present still Ranorex tries to find the object and throws an error saying "object not found".

Solution: First check for the existence with "repo.objectInfo.exists()"and if found then declare the variable(drag and drop the object from the Repository) and then use it.
"Worked for me" :lol:

Regards,
Deepak :D

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Wed Apr 16, 2014 9:00 pm

Hi All,

2 things i need to ask.

1: .exists property checks whether the object is available or not. If object exists then fine but if its not available then ranorex instead of going into Else statement tries to find the object for 1.5 mins and then goes into Else statement. This delays my test case execution a lot so is there a way to reduce this time???

2:When i write Delay.Millisecond(60000)
then ranorex waits for 60 seconds. If the next page gets loaded then still it waits for completion of the time. Apart from Shift+Pause which is manual cant we do it in some way that ranorex waits till the whole page is loaded so this Delay dependency is not there.

For Ex: we have in qtp browser.page .sync
here sync method waits for full page to load by itself without any manual hardcoding in the script.

Thanks in Advance,
Deepak

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Thu Apr 17, 2014 4:10 pm

Hi Deepak,

1. set the timeout property of the element, and the element's parent (there should be 2 more parent levels, since every level adds 30 sec to the effective timeout).

To set a repository elements searchtimeout to 1 sec try this:

Code: Select all

repo.Ranorexwebpage.SearchTimeout=1000;
2. The delay is working as intended, it just waits all the time. For waiting a page to load, try waitfordocumentloaded: http://www.ranorex.com/Documentation/Ra ... Loaded.htm

Here is an another forum topic about document reload: http://www.ranorex.com/forum/waitfordoc ... t1057.html

Best Regards,
Zoltán

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Mon Apr 21, 2014 8:19 am

Hi Zoltan,

This means I have to write this for every object or Just add page into the Repository and write and it will work for all the objects in that page?

Regards,
Deepak

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Tue Apr 22, 2014 1:14 pm

Hi Deepak,

Unfortunately you have to set the timeout for every element.

If you set the page's timeout to 1 sec, then all of the elements on the page will have a timeout of 31 sec.

You can set the default timeout of NEWLY created elements to a custom value in the Settings page.

For the elements already in repository, you can just use a text editor (I use notepad++), and do a search&replace for timeout.


Hope I am clear,

Best regards,
Zoltan

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Tue Apr 22, 2014 1:52 pm

Hi Zoltan,

Adding wait for every element!!! :?
Not a good option and not a good Automation practice I think.

Another Question:
Is there any method or predefined function in Ranorex that waits for the page to load all the objects?

Like .sync is present in QTP ?

Regards,
Deepak

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Tue Apr 22, 2014 11:34 pm

Hi Deepak,

Do you use web pages, desktop application or a mobile application?

If you are using webpages, try first with WebDocument.WaitForDocumentLoaded(), described here: http://www.ranorex.com/Documentation/Ra ... Loaded.htm

If this is not working, or you are using other technologies, then use the usual stuff:
Wait for elements to appear, or wait for elements to disappear. Set a high enough timeout for those elements, and the waiting is solved :)

Regards,
Zoltan

Deepak_Singh
Posts: 76
Joined: Fri Mar 14, 2014 2:37 pm

Re: .Exists not working Properly

Post by Deepak_Singh » Wed Apr 23, 2014 11:21 am

Ok Will Try that,

One More question. Can we perform a Long click on an object?
If yes then how? :?:

Regards,
Deepak

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: .Exists not working Properly

Post by mzperix » Wed Apr 23, 2014 11:58 am

Hi Deepak,

How can you click long? :)

See Mouse class: http://www.ranorex.com/Documentation/Ra ... _Mouse.htm

among the methods is the Mouse.Click(MouseButtons, Duration): http://www.ranorex.com/Documentation/Ra ... ick_28.htm

The duration will tell how much time should elapse between mouse down and mouse up.

Hope this helps,
Zoltan

P.S. Look into the Ranorex API: http://www.ranorex.com/Documentation/Ranorex/ It helps you is looking for the actions that QTP has also. And for everything else, there is .NET :)