How to skip steps if element does not exist

Best practices, code snippets for common functionality, examples, and guidelines.
keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

How to skip steps if element does not exist

Post by keenon » Thu May 28, 2015 6:36 pm

I am currently working with client data which obviously will vary from client to client (# of different assets, income, etc.) The way I have this recording setup is so that it will get the value of each income per client. I'd like to continue grabbing the income until there are no more income values listed, therefor skip the next getvalue and possibly the next few steps. At this point I'd move onto another section of the test. Sorry I am fairly new to Ranorex and c# as well. I think I have exhausted the possibility of being able to do this without usercode. Please let me know if this is even possible. I have quite a bit of tests that may rely on this. Any help you can provide me would be greatly appreciated.

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

Re: How to skip steps if element does not exist

Post by Support Team » Mon Jun 01, 2015 9:46 am

Hello keenon,

I quite agree with you. This can’t be done without using UserCode. In order to help you we would need more information about your application under test. May I ask if it’s somehow possible to access your application or a sample application, which depicts the mentioned AUT.

Thanks,
Robert

User avatar
jasoncleo
Posts: 37
Joined: Mon Jun 08, 2015 7:37 am

Re: How to skip steps if element does not exist

Post by jasoncleo » Tue Jun 09, 2015 2:17 am

Hi Keenon,

We faced the same dilemma ourselves when planning our tests in Ranorex. We were often going to face varying scenarios, based on certain context information (i.e. user that was to be logged in to the AUT, the data subfields may not be present, etc).

In the end, user code was the only option, as the recording modules have no facility for logical branching (i.e. if-else).

The benefits we found using the user code modules are huge. You can use classes to abstract the functionality of a UI screen, so that the test case code module just has to call high-level operations from the class that is designed to deal with the nitty gritty of the UI specifics, e.g.: doLogin(user, password).

Then within the doLogin() method, it does the operations of testing for the UI screen elements:

Code: Select all

        void doLogin(string user, string pwd)
        {
        	var repo = MyRepoProject.MyRepoRepository.Instance;
        	
        	if (repo.Login.LoginUserIdInfo.Exists(0))
        		repo.Login.LoginUserId.PressKeys(user);
        	
        	if (repo.Login.LoginPasswordInfo.Exists(0))
        		repo.Login.LoginPassword.PressKeys(user);
        	
        	if (repo.Login.BtnLoginInfo.Exists(0))
        		repo.Login.BtnLogin.Click();
        }
*NOTE: How you structure elements in your own repository is up to you, so the above code will not work as a straight copy-paste unless you adjust your repository to match.

The benefit of the above is that your user code module can now just call doLogin() and it will do all the above steps, making your code modules cleaner and easier to read. You can also embed various reporting statements and exception handling to determine whether you want your test to continue, or abort, if something isn't as expected.

Then ultimately you test case code module could become something as simple as:

Code: Select all

        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;

            var util = new SupportingClassHere();
            var testDataSet = util.LoadDataSet(@"dataset.file");
            while (testDataSet.hasNext())
            {
                util.doLogin(testDataSet.getData("username"),
                             testDataSet.getData("password"));
                if (string.IsNullOrEmpty(testDataSet.getData("someData")))
                {
                    util.doSomeStuff(testDataSet);
                } else {
                    util.doOtherStuff(testDataSet);
                }
                // Can have Validate calls, or do your own checks and call Report methods to log success/failure.
                util.doLogout();
            }
        }


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

Re: How to skip steps if element does not exist

Post by Support Team » Wed Jun 10, 2015 9:03 am

Hello jasoncleo,

Thank you for sharing your experiences with Ranorex :)

Regards,
Robert

keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

Re: How to skip steps if element does not exist

Post by keenon » Thu Jul 30, 2015 7:32 pm

Thank you so much Jason. Sorry it took so long, but I finally got around to working on this again and your solution worked perfectly. It's actually rather simple as well like you mentioned. This will open endless possibilities for now! You made my day.

keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

Re: How to skip steps if element does not exist

Post by keenon » Thu Jul 30, 2015 8:39 pm

Actually I went about it using a combination of what you described and another way. I figured I might as well share for anyone else faced with this problem.

Code: Select all

public void Validate_Income()
        {
        	bool someIncome;
        	
        	someIncome = myRepo.myForm.buttonInfo.Exists();
        		
        	if(someIncome)

        	{
        		do something
        	}
        }

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

Re: How to skip steps if element does not exist

Post by krstcs » Fri Jul 31, 2015 2:38 pm

A coding tip for those who are new to it:
Try shortening/minimizing your code when you can. It makes it more readable and you use fewer resources.

Instead of this:

Code: Select all

public void Validate_Income()
        {
           bool someIncome;
           
           someIncome = myRepo.myForm.buttonInfo.Exists();
              
           if(someIncome)

           {
              do something
           }
        }
try this:

Code: Select all

public void Validate_Income()
        {
           if (myRepo.myForm.buttonInfo.Exists())
           {
              //do something
           }
        }
Instead of adding the extra boolean declaration (uses more memory that you don't really need to use) and set operation, just do the actual 'exists' check inside the if. Anyone reading it later on will know exactly what you meant to do by reading it. Also, you can set the value in the declaration as well:

Code: Select all

bool buttonExists = myRepo.myForm.buttonInfo.Exists();
Again, this makes the code more easily understood.

The rule-of-thumb for declaring variables is: If you are going to use the value only once, don't declare a variable, just fetch the value where you need it; if you are going to use the value two or more times, declare a variable and fetch the value once, UNLESS you need to get the current value EVERY TIME YOU NEED IT (for exampled, getting the current value of a text field as a user types into it for autocomplete, etc.). This is just a rule-of-thumb though... :D

This probably doesn't make a huge difference in our tests, but it could in longer tests, and definitely does in applications that have a lot of data. And, good coding practices should always be followed, no matter what we are working on.
Shortcuts usually aren't...

keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

Re: How to skip steps if element does not exist

Post by keenon » Fri Jul 31, 2015 9:42 pm

Wow I feel silly lol. Thanks krstcs that worked perfectly as well and is much shorter/simpler. You da real MVP.

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

Re: How to skip steps if element does not exist

Post by krstcs » Mon Aug 03, 2015 2:52 pm

You're welcome!
Shortcuts usually aren't...