Condition execution based on Parameter value

Best practices, code snippets for common functionality, examples, and guidelines.
bk_tester
Posts: 26
Joined: Thu Dec 29, 2016 5:37 pm

Condition execution based on Parameter value

Post by bk_tester » Mon Sep 18, 2017 4:57 pm

Hello Ranorex,

In my test I need to handle two types of the dialog which are invoked by double clicking table rows. Table is traced in DDT loop. Based of the appeared dialog type different smart folder should be executed. I used Code Module to identify which type of dialog appears and set appropriate value for the Module variable (DialogType). Than, following smart folders has a condition based on that variable value. For better understanding please have a look at the attached screenshot ConditionalExecution.png
The issue is that DialogType variable looks changed, and SmartFolder condition ignore that value.

So what is the reason why condition is not triggered during test run?

Thanks!
Attachments
ConditionalExecution.png
ConditionalExecution.png (56 KiB) Viewed 775 times

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Condition execution based on Parameter value

Post by Vaughan.Douglas » Mon Sep 18, 2017 5:14 pm

Could it because you're setting the DialogType to "NewItemDialog" and testing for "NewItemType"?
ConditionalExecution.png
ConditionalExecution.png (65.37 KiB) Viewed 772 times
Doug Vaughan

bk_tester
Posts: 26
Joined: Thu Dec 29, 2016 5:37 pm

Re: Condition execution based on Parameter value

Post by bk_tester » Mon Sep 18, 2017 5:25 pm

It's a mistake in text on the screenshot. Sorry for that.
DialogType variable could have value as "NewItemType" or "RequestSeriesDialog". And condition checks whether DialogType is equal to first or second value.

Thanks

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Condition execution based on Parameter value

Post by Vaughan.Douglas » Mon Sep 18, 2017 5:41 pm

I figured as much, but you always double check the easy stuff. :lol:

How are you binding your parameter? In the Data Binding column for Define_NewItemType_Dialog I don't see anything about variables being bound. This could be because your text just covers it or you're not using a module variable to bind the parameter.

So I'm guessing you're just setting the parameter from 005_01_Open_and_preview_all_requests directly from code rather than binding the value to a module variable and binding that module variable back to the DialogType parameter back on the rxtst page. I have no idea if this is causing your problem, but that would be the first thing I'd try.
Doug Vaughan

bk_tester
Posts: 26
Joined: Thu Dec 29, 2016 5:37 pm

Re: Condition execution based on Parameter value

Post by bk_tester » Mon Sep 18, 2017 6:21 pm

I'm setting DialogType parameter in the code in Define_NewItemType_Dialog module, like this:

Code: Select all

TestSuite.CurrentTestContainer.Parameters["DialogType"] = "NewItemType";
This module does not have any data bindings.

It seems that during run time DialogType variable do have required value, because in code i have:

Code: Select all

Report.Info("User code", "Dialog type is: " + TestSuite.CurrentTestContainer.Parameters["DialogType"]);
and in report:
17:49:26 Info User code Dialog type is: NewItemType

Hope this info helps to understand the problem better.

Regards

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Condition execution based on Parameter value

Post by Vaughan.Douglas » Mon Sep 18, 2017 9:07 pm

It looks like Ranorex doesn't like it when you set the parameter directly. You need to user a module variable and bind that back to the parameter. See the attached solution.
HoldMyBeer.zip
Sample Solution
(19.29 KiB) Downloaded 38 times
Doug Vaughan

bk_tester
Posts: 26
Joined: Thu Dec 29, 2016 5:37 pm

Re: Condition execution based on Parameter value

Post by bk_tester » Tue Sep 19, 2017 3:24 pm

Thanks a lot for your idea. It works fine for me. And in the same time my solution uncover a bug or at least possible improvement :)

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

Re: Condition execution based on Parameter value

Post by krstcs » Tue Sep 19, 2017 3:43 pm

I do not think this needs any improvement, and it's definitely not a bug.

The expected and recommend way to handle parameters/variables is to create module variables and bind them to parameters, as Vaughan said. Then you use that variable in your module. You should not be setting the test suite parameter directly as it may not be guaranteed to be updated or used in the way you are expecting, where using it through a local module variable will work every time.
Shortcuts usually aren't...

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Condition execution based on Parameter value

Post by Vaughan.Douglas » Tue Sep 19, 2017 6:13 pm

bk_tester wrote:Thanks a lot for your idea. It works fine for me. And in the same time my solution uncover a bug or at least possible improvement :)
I do see why you'd expect it to work by setting the parameter directly. From a strictly .net standpoint, it should work. I suspect krstcs is right though simply because this functionality is ONLY used in conjunction with Ranorex Studio conditional statements. I'm fairly sure that if you set a parameter directly you can access the value in another module programmatically, or at least you could in prior versions of Ranorex.

On the purely pragmatic level of test design, by not using the module variable as an intermediary you're obfuscating a variable from that rxtst page which acts as an overview for the scenario. You just need to know your audience.
Doug Vaughan

Marcel
Posts: 1
Joined: Wed Sep 20, 2017 11:32 am
Location: Graz, Austria

Re: Condition execution based on Parameter value

Post by Marcel » Wed Sep 20, 2017 12:05 pm

Hello,

Setting a local parameter programmatically and using its value in an IF conditions works normally if you use the parameter inside of its scope (see:lesson-4-ranorex-test-suite and then “Scope of data containers”). The easiest way to demonstrate this is if we take the sent solution “HoldMyBeer” and look at the parameters “Beer” and “MoreBeer” that are defined inside of the “Six_Pack” TC. If we want to access the local parameter “Beer” and “MoreBeer” inside of the child “Mmmmmm_Beer” Smart folder we need to bind them to the module variable like “Better_Beer”.

If not, we need to access the parent parameter with the right code path, in our case we change/update the code in the “WhatToDo” code module to:
// TestSuite.CurrentTestContainer.Parameters["Beer"] = "Good Beer";
TestSuite.CurrentTestContainer.ParentContainer.Parameters["Beer"] = "Good Beer";
So we are setting the value of the “Six_Pack” TC parameter to “Beer”.

Or for example we can create a new code module in our case “SetParameterForIF”:
test_suite_set_param.png
test_suite_set_param.png (115.22 KiB) Viewed 708 times
In this module you write your “normal” code for modifying the local parameter of the “Six_pack” TC:
TestSuite.CurrentTestContainer.Parameters["Beer"] = "Good Beer";
and then the IF condition should work as expected.

Best regards, Marcel
Attachments
HoldMyBeerUpdated.zip
(3.24 MiB) Downloaded 19 times

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Condition execution based on Parameter value

Post by Vaughan.Douglas » Thu Sep 21, 2017 2:55 pm

Marcel wrote:wisdom dropped here...
great explanation. Thanks!
Doug Vaughan

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

Re: Condition execution based on Parameter value

Post by krstcs » Mon Sep 25, 2017 2:15 pm

The problem with that logic is that is assumes that it will always be that way. You can't make that assumption. You should be using local (module) variables, bound to parameters or data columns, not setting them directly. The back-end of many of the objects that Ranorex exposes are not guaranteed to be static for any length of time and they can change on any given release.
Shortcuts usually aren't...