check if object exist with loop

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
Tzahi
Posts: 16
Joined: Sun May 17, 2015 2:24 pm

check if object exist with loop

Post by Tzahi » Tue Jul 21, 2015 7:20 am

Hi,

I try to locate object in a page, and it can be randomly in 1 of 10 pages.
I find a solution but it take a long time (1 min to each page):

Code: Select all

int i=0;
bool isObVisable= false;
while  (i<10)
	{
		i++;
		if (i==10)
		{
			Report.Log(ReportLevel.Info, "Obj Not founded", "Obj is not exist at this location. test will failed");
			Report.Failure("Obj not exist", "Obj wasn't found, test can't continue");
			throw new Ranorex.ElementNotFoundException("Item RanoreXPath with no return" , null); 
		}       		
		try {	        				
				isObVisable = repo.Web.obj.Visible; // this section take a min, while finding when it exist take few secs.
			}
		catch (Exception ex)
			{	        				
				Console.WriteLine("element wasn't found exception type: {0} ", ex.GetType().Name);
			}
		
		if (isObVisable)
		{	        			
			Report.Log(ReportLevel.Info, "Obj found", "Obj was founded after " + i.ToString() + " pages and will be click");
			repo.Web.obj.Click();  
			break;
		}
		else
		{	        			
			nextPageBtn.Click();	        			
		}		
	}
note:
I am using Ranorex studio 5.3.2
Best Regards
Tzahi Caspi

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: check if object exist with loop

Post by odklizec » Tue Jul 21, 2015 8:01 am

Hi,

The wait time is so long because you applied "Visible" on repo element, which Effective Timeout is probably 1 minute (learn more about effective timeout >here<)? And if the element is not found on the page, Visible tries to wait full effective timeout for its appearance.

I would suggest you to change your code like this:
try {
        if (repo.Web.objInfo.Exists(5000)) // this checks if the element exists on actual page. Instead of waiting full effective timeout, it searches the element just 5 secs. It may be risky in web-based apps to shorten the wait time though ;) 
        {  
            isObVisable = repo.Web.obj.Visible; // if element is found, check its visibility
        }
        else
        {
            isObVisable = false; // if element is not found, it's safe to assume it's not visible ;)
        }
     }
catch (Exception ex)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Tzahi
Posts: 16
Joined: Sun May 17, 2015 2:24 pm

Re: check if object exist with loop

Post by Tzahi » Tue Jul 21, 2015 8:22 am

Thanks Odklizec,

You just cut me waiting of 5 to 7 min.
The code i post was just for logic, it's not the real one :D .
Also your link is great.
Lucky I didn't take the test yet 8)

Have a great week.
Best Regards
Tzahi Caspi