Loop problem

Best practices, code snippets for common functionality, examples, and guidelines.
yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Loop problem

Post by yaro2o » Thu Jun 21, 2018 9:30 pm

Hi
I heave problem with my code, how break the while loop ? the last if doesnt work. How check the window1 is not visible ? I want something like this:

1.Try do something
2.If 1. does not work and window1 is visible (this window can be more than 1) close all window1
3.If window1 is not visible break the loop

My code work almost well,If try is ok they dont do catch, if try not workink do catch, close all window1 but it still search window1,it does not end the while loop and throws an error in report

Code: Select all

           try
            {
            	Ranorex.Report.Info("First try do something");
           		
            }
            catch
            
            {
            	
            	while (repo.window.window1.Self.Visible)
            	{
            		Ranorex.Report.Info("second");
                        repo.window.window1.Self.Close();
         		
            		if(repo.window.window1.Self.Visible == false) 
            		{
            			break;
            		}


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

Re: Loop problem

Post by odklizec » Fri Jun 22, 2018 9:53 am

Hi,

It would be helpful if you post the exact error you are getting. Anyway, I think that the error is thrown after closing last window1? And it's thrown, because window1 no longer exists, which means the element repo.window.window1 does not exists and any attempt to access its attributes must fail.

What you need to do is to replace repo.window.window1.Self.Visible with repo.window.window1.SelfInfo.Exists(). Exists method returns true/false and does not throw an exception if the element does not exists. Hope this helps?
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

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Fri Jun 22, 2018 2:05 pm

Hi thx for your answer, I found the answer to my question and it was about Exist

now my code looks like this

Code: Select all

if(repo.window.FrmMain.TfrxPreviewWorkspaceInfo.Exists(1000))
        	{
        	repo.window.FrmMain.TfrxPreviewWorkspace.PressKeys("{escape}");
        	}
        	else
        	{
          var var1 = false;
        
			while (repo.Clean.window.SelfInfo.Exists())	
	        {
	          repo.Clean.window.Self.Close();
	          var1 = true;
	        }
			
			if(var1)
			{
			  Ranorex.Report.Info("sample text");
			}
			
			if(var1 == false)
			{
			  Ranorex.Report.Info("sample text2");
			}
        	}




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

Re: Loop problem

Post by odklizec » Mon Jun 25, 2018 11:54 am

Hi,

I'm glad you solved the problem ;) Using 'Exists' for validation of element availability is a very good idea.
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