More efficent way to reuse user code

Ranorex Studio, Spy, Recorder, and Driver.
houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

More efficent way to reuse user code

Post by houseofcutler » Thu Apr 17, 2014 5:56 pm

Hi All,

I find myself having to check whether elements are visible or no longer visible on a regular basis. To confirm whether pages and or popups have loaded or closed before continuing with further actions.

At the moment I am adding user code each time I need to do this during the test cases.

I am guessing there must be a more efficient way of achieving this? I saw one post where someone mentioned making a method to do this - I'm afraid I'm not a developer and cobble together my code using Google :oops: so am not sure how to implement this.

If anyone can help you will make my life a lot easier.

Many thanks

Ben

Code: Select all

       public void Wait_For_Popup()
        {
        	Report.Log(ReportLevel.Info, "Wait", "Waiting for item 'GroupcallMessengerRoot.Popup' to be visible.");
        	
        	bool WindowVisible = repo.GroupcallMessengerRoot.Popup.Self.Visible;
			while (repo.GroupcallMessengerRoot.Popup.Self.Visible == false)
				{
 				 Thread.Sleep(500);
  				 WindowVisible = repo.GroupcallMessengerRoot.Popup.Self.Visible;
				}
           
        }

        public void Mouse_Click_House()
        {
            Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'GroupcallMessengerRoot.SendMessage.SideMenu.House' at Center.", repo.GroupcallMessengerRoot.SendMessage.SideMenu.HouseInfo);
            if (repo.GroupcallMessengerRoot.SendMessage.SideMenu.House_Li1.Visible != true) {
            	repo.GroupcallMessengerRoot.SendMessage.SideMenu.House.Click();
            }
            
        }

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

Re: More efficent way to reuse user code

Post by krstcs » Mon Apr 21, 2014 3:14 pm

Try changing the first method to:

Code: Select all

       public void Wait_For_Popup()
        {
           Report.Log(ReportLevel.Info, "Wait", "Waiting for item 'GroupcallMessengerRoot.Popup' to be visible.");
           
              int loopLimiter = 20;
-->>         while (!repo.GroupcallMessengerRoot.Popup.Self.Visible)
            {
             Thread.Sleep(500);
             if (loopLimiter <= 0) break;
              loopLimiter--;
            }

            Validate.Attribute(repo.GroupcallMessengerRoot.Popup.Self, "Visible", true);
         }
"!" is logical NOT, so the while reads "While NOT repo.GroupcallMessengerRoot.Popup.Self.Visible, do the following...".

You don't need to create a variable for the WindowVisible, unless you need to use that value somewhere else, which you aren't in this case. Rule of thumb, don't create something unless you need to use it. :D

Also, you don't want to just wait forever for the window to be visible, because it may never be, for various reasons. I put a loopLimiter variable in that counts down from 20 until it hits 0. Once it does, the loop will break out and the test will continue.

I would suggest that you add a Validate.Attribute() and validate the the popup is now visible. If it isn't then your test will fail right there and you can adjust things as needed.


I highly recommend doing some reading on .NET and C#. There are several good books (C# for Dummies, etc. (Not calling you a dummy, just that they are good books... :D )). It will help with learning the basics.
http://www.dummies.com/how-to/computers ... chash.html
Shortcuts usually aren't...

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

Re: More efficent way to reuse user code

Post by krstcs » Mon Apr 21, 2014 5:01 pm

One other thing you may want to consider is, you can make several "versions" of the same repository object. One for normal, one for visible=true, etc.

For example, you could have an object like this:

MyDiv_VISIBLE -- //div[#'myDiv'][@visible='true'] or //div[@id='myDiv' and @visible='true']
MyDiv_NOT_VISIBLE -- //div[#'myDiv'][@visible='false'] or //div[@id='myDiv' and @visible='false']

Use the WaitFor(Exist/Not Exist) action to wait for one or the other of the objects for a specific time. This way you don't have to code as much.
Shortcuts usually aren't...

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: More efficent way to reuse user code

Post by houseofcutler » Tue Apr 22, 2014 3:01 pm

Thank you very much for your replies - I especially like the fact that you have given me the coding C# solution as well as a a non coding solution - I will investigate both.

Also I agree I do need to learn some C# as it will be massively useful with this going forward.

Thanks again

Ben