Data Source: choose Data Range programatically

Best practices, code snippets for common functionality, examples, and guidelines.
User avatar
reureu
Posts: 3
Joined: Thu Jul 20, 2017 1:23 pm

Data Source: choose Data Range programatically

Post by reureu » Thu Jul 20, 2017 2:10 pm

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

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Data Source: choose Data Range programatically

Post by asdf » Fri Jul 21, 2017 2:27 pm

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

User avatar
reureu
Posts: 3
Joined: Thu Jul 20, 2017 1:23 pm

Re: Data Source: choose Data Range programatically

Post by reureu » Tue Jul 25, 2017 9:02 am

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

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Data Source: choose Data Range programatically

Post by asdf » Tue Jul 25, 2017 11:45 am

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

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

Re: Data Source: choose Data Range programatically

Post by odklizec » Tue Jul 25, 2017 12:26 pm

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).
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

User avatar
reureu
Posts: 3
Joined: Thu Jul 20, 2017 1:23 pm

Re: Data Source: choose Data Range programatically

Post by reureu » Tue Jul 25, 2017 1:52 pm

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;

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

Re: Data Source: choose Data Range programatically

Post by krstcs » Tue Jul 25, 2017 3:39 pm

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
Shortcuts usually aren't...

joyarjit
Posts: 20
Joined: Fri Oct 13, 2017 6:09 am

Re: Data Source: choose Data Range programatically

Post by joyarjit » Fri Oct 13, 2017 1:27 pm

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?

User avatar
RobinHood42
Posts: 324
Joined: Fri Jan 09, 2015 3:24 pm

Re: Data Source: choose Data Range programatically

Post by RobinHood42 » Tue Oct 17, 2017 8:58 am

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)