Dynamically assigning data sources

Ranorex Studio, Spy, Recorder, and Driver.
AccidentReport
Posts: 78
Joined: Tue Dec 04, 2012 2:30 pm

Dynamically assigning data sources

Post by AccidentReport » Tue Mar 11, 2014 10:53 am

In my web application I have a screen used to edit variables. Some of these are structured in an easy to automate way, however a few are a bit too dynamic and need more intelligence in the test. Something that I am struggling to get my brain around currently and need a bit of cloud thinking!

For example, I have a variable type called Distribution. This variable defines the percentage of items that go down a specific path. It can have anything between 2 and 20 paths and the total of all the path percentages must equal 100.

The basic structure that I've come up with for the test is shown in the attachment below. What I want to do is have a data source on the "Distribution" test case which can define between 1 and n distribution variables which I want to update. I figure that including the variable details within that data source would get quite difficult so I then want a separate data source for the "Paths" test case which actually has the details of each path for a variable. The way I figure it though I can't actually assign the CSV file to the "Paths" test case as that would limit me to just one varibale. Is it possible to assign a CSV as a data source for a test case whilst it's actually running?

Does that make any sense? Can anyone provide thoughts on the best way forward with this or will I need to limit it to one variable per run?

Thanks in advance for any help or suggestions!
You do not have the required permissions to view the files attached to this post.

AccidentReport
Posts: 78
Joined: Tue Dec 04, 2012 2:30 pm

Re: Dynamically assigning data sources

Post by AccidentReport » Wed Mar 12, 2014 4:34 pm

Well as it turns out I haven't found a solution that is exactly what I want...but nobody else seems to have any ideas either!

Anyway, What I've got is a User Code module I can feed specific csv files into for each of the distribution variables and then loops round. Code Below.

Not sure if this will help anyone but since its the best I've got I thought I'd share!

Code: Select all

string variableName = "bob";
int i = 0;

try {
	// Create CSV Connector and output for rows and cols
	Ranorex.Core.Data.CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector("myCSVConnector", "C:\\Users\\Public\\Documents\\" + variableName +".csv", true);
	Ranorex.Core.Data.ColumnCollection col; 
	Ranorex.Core.Data.RowCollection row; 
	csvConnector.LoadData(out col, out row); 
				
	// Create list of cells in dist edit window
	DivTag myDivTag = repo.website.FrmChildMain.Ctl00ContentPlaceHolder1DivGrdPCD;
	IList<Ranorex.InputTag> myCells = myDivTag.FindDescendants<Ranorex.InputTag>();
				
	foreach(Ranorex.Core.Data.Row dataRow in row)
	{
		for (i = 0; i < count; i += 2) {
			if (dataRow["branchName"].ToString() == myCells[i].TagValue.ToString())
			{
				myCells[i+1].Click();
				myCells[i+1].PressKeys("{Home}{Shift down}{End}{Shift up}");
				myCells[i+1].PressKeys("{Back}");
				myCells[i+1].PressKeys(""+dataRow["branchPercentage"].ToString());
			}				  	
		}				  
	}
} catch (Exception) {
            	
} 

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

Re: Dynamically assigning data sources

Post by krstcs » Wed Mar 12, 2014 6:31 pm

I use SQL Connectors and SQL Server Express for all my data.

I create stored procedures in SS and just call them in my tests.

When I need to make a connector dynamic, I just change the connector's SQL query string right before the test case that uses that connector.

This is pretty simple and I don't have to manipulate a lot of data directly.

The module that I use to change the connector takes as Test Variables any parameters that I am passing to the sql query, so I can then bind those to the parent test case's (or global params as needed) data so it is all dynamic.

So, say I need to loop through customer orders, but only for the current customer:

I would have 2 stored procedures (SPs) that take default parameters so they would always return null if nothing was passed, which allows me to skip test cases if there is no matching SQL data:
Get_CustomerInfo (returns CustomerID, FirstName, LastName, EmailAddress, Password, etc.)
Get_CustomerOrderInfo_for_CustomerID (returns CustomerOrderID, Date, Store, etc.)

I would then make 2 connectors:
CustomerInfoSQL
CustomerOrderInfoSQL

Both would call the SPs without parameters so they would default to no records returned, but would allow for data binding in Ranorex.

Then I would have a UserCode Module:
Set_CustomerOrderInfoSQL_for_CustomerID

It would have CustomerID as a Test Variable (right-click the code and select "Add Test Variable") that defaults to "0".

In the Run() method I would add the following to the last line:

Code: Select all

MYLIB.Data.SetupSqlDataConnector("CustomerOrderInfoSQL", string.Format("exec Get_CustomerOrderInfo_for_CustomerID @CustomerID={0}", CustomerID));
Which in turn calls the following:

Code: Select all

public static void SetupSqlDataConnector(string dataCacheName, string queryString) {
    ((SqlDataConnector)DataSources.Get(dataCacheName).Connector).Query = queryString;
}
It takes a bit, but once you get it working I think it is much easier to work with.
Shortcuts usually aren't...

jpa
Posts: 3
Joined: Mon Mar 17, 2014 11:13 am

Re: Dynamically assigning data sources

Post by jpa » Mon Mar 17, 2014 11:16 am

AccidentReport wrote: For example, I have a variable type called Distribution. This variable defines the percentage of items that go down a specific path. It can have anything between 2 and 20 paths and the total of all the path percentages must equal 100.
When I need a variable number of values, I usually type them all into a single variable separated by comma:

Code: Select all

percentages = 20,27,50,10
It is then easy to separate them in user code:

Code: Select all

string[] percents = percentageList.Split(',')