Page 1 of 1

Data Source: choose Data Range programatically

Posted: Thu Jul 20, 2017 2:10 pm
by reureu
Hi,

We are working on a Desktop app test.
This app is customized for various customers.

We are currently trying to add some customer-specific variability to this test.

Our idea consists of using Data Sources and variables.
So we define a CSV data source.
In this CSV file, each row corresponds to a set of Customer-specific test-settings.
Each column corresponds to one Customer-specific test-setting which is bound to a variable.

Now, how can we select the Data Range programatically during test-runtime?
We would like:
  • the Test for CustomerA to use Data Range 1 (aka the line no 1 in the CSV file).
  • the Test for CustomerB to use Data Range 2
  • ...
Alternatively, we could define several Customer-specific CSV Data Sources.
But how could we choose the Data Source programatically?

Kind regards,
Reureu

Re: Data Source: choose Data Range programatically

Posted: Fri Jul 21, 2017 2:27 pm
by asdf
Hi reureu,

I would suggest having a look at the following code. There you set the min and max range for a specific TestCase.

Code: Select all

        	var TC = TestSuite.CurrentTestContainer;
        	var min = TC.DataRange.MinRange = 1;
        	var max = TC.DataRange.MaxRange = 2;
        	TC.DataContext.SetRange(min,max);
I hope this helps.

Kind regards,
asdf

Re: Data Source: choose Data Range programatically

Posted: Tue Jul 25, 2017 9:02 am
by reureu
Thanks asdf!

I should have RTFM'ed earlier.

But it doesn't seem to work.

Your code compiles. But when I execute it, the values of the DataRange used for testing invariably remains the first one.
var TC = TestSuite.CurrentTestContainer;
var min = TC.DataRange.MinRange = 2;
var max = TC.DataRange.MaxRange = 2;
TC.DataContext.SetRange(min,max);
In the debugger, right after calling SetRange, the variables are:
  • TC.DataRange.MinRange = 2;
  • TC.DataRange.MaxRange = 2;
  • TC.DataContext.CurrentRowIndex = 2;
but TC.DataContext.CurrentRow invariably contains the first line of my CSV file (or whatever row is selected in the Data Range setting of the Data Source dialog).

Calling DataContext.SetRange does not update DataContext.CurrentRow.

Is there anything I need to do to make sure this runtime change is taken into account for the rest of my test case execution?
I have tried to place this code
  • in a Setup step of my test case.
  • in the Init method of my test case's user code.
but no luck so far...

I'm running Ranorex version 7.1.0.

Kind regards,
Reureu

Re: Data Source: choose Data Range programatically

Posted: Tue Jul 25, 2017 11:45 am
by asdf
Hi Reureu,

I think that this code just works, if you execute it, before the test case which you actually want to change.
Therefore, you have to adapt the code like in the following example.

Code: Select all

var TC = TestSuite.Current.GetTestContainer("YourTestCase");  
var min = TC.DataRange.MinRange = 2;  
var max = TC.DataRange.MaxRange = 2;  
TC.DataContext.SetRange(min,max);  
Hope that helps.

Regards,
asdf

Re: Data Source: choose Data Range programatically

Posted: Tue Jul 25, 2017 12:26 pm
by odklizec
I'm using exactly the same approach in my projects and it works even in 7.1. As asdf pointed, the code must be run before the test case you want to change (ideally in teardown section of previous TC/SF).

Re: Data Source: choose Data Range programatically

Posted: Tue Jul 25, 2017 1:52 pm
by reureu
Hi chaps,

Thank you very much for your useful replies.

Here is what I did to make it work:
  • I moved my code to a Setup step of my TestSuite, so that my DataRange initialization gets executed before my TestCase.
  • I turned
    var TC = TestSuite.CurrentTestContainer;
    into
    var TC = TestSuite.Current.GetTestContainer("TestCase");
    because CurrentTestContainer was null while executing the Setup step of my TestSuite.
What I didn't understand in the first place was that I could not set the DataRange once the TestCase had been started, neither in the User Code's Initialize block, nor in the TestCase's SETUP step.
I was under the impression that the User Code's Initialize block, or the SETUP step could be used for that purpose.
Did I miss something in the documentation?

It is also questionable whether DataContext.SetRange shall throw an exception when being called after the TestCase has started.

By the way, the following two lines are not required:
TC.DataRange.MinRange = 2;  
TC.DataRange.MaxRange = 2;

Re: Data Source: choose Data Range programatically

Posted: Tue Jul 25, 2017 3:39 pm
by krstcs
Once your test is executing the modules inside the text case or smart folder, that TC/SF's initialization has already happened, which includes the data set being finalized.

You always need to set the parameters for a TC/SF BEFORE the TC/SF starts.

I usually put my initializers immediately before the TC/SF.

--TestSuite
----[Setup]
-------InitializeDataSet_TC1
----TestCase1 <- DatasetForTC1
------TestModule1
------InitializeDataSet_SF1
------SmartFolder1 <- DatasetForSF1
--------TestModule2

Re: Data Source: choose Data Range programatically

Posted: Fri Oct 13, 2017 1:27 pm
by joyarjit
I have only 1 test case to be executed in multiple iterations and do not have any preceding test case.

When i use this approach by having usercode with this code as my first line, even then i am not able to fetch data within specific range. Rather all data is being taken up by ranorex. Please suggest what should i do?

Re: Data Source: choose Data Range programatically

Posted: Tue Oct 17, 2017 8:58 am
by RobinHood42
Hi joyarjit,

As krstcs mentioned, please ensure to initialize the data sets before your test case. In your case you would need to add your code to a module within a global "SETUP"-region.

TestSuite
--->SETUP
--------->SetConnectors Module
--->YourTestCase

Hope this helps,
Robin 8)