Page 1 of 2

Update CSV data source at runtime?

Posted: Thu Jun 04, 2020 1:12 pm
by mrt
Hello folks,

I have the following scenario:

One test case inputs parameters from CSV data source into the application.
When "hitting the save button" inside the application, it creates objects with dynamic names.
Later on, I want to verify those values, but I need the dynamic name to identify the item.

So my idea was to introduce a new column "dynamicName" to the CSV data source - so I can use it in future test cases.
Most likely Ranorex is caching the CSV data, so it is ok that the new value is not available while the current test is running - it will only be needed in future test cases.

So my question:
Is it possible at all to update a data source on runtime?

What I tried so far (testing example)

Code: Select all

 var currentTestContainer = TestSuite.CurrentTestContainer;
 var currentConnector = currentTestContainer.DataContext.Source;
 currentConnector.Load();
 currentConnector.Rows[0].Values[2]="test";
 currentConnector.Store();
I can see in "Local variables" that the value gets updated - but the file content never changes.

Any ideas?

Thanks!

Re: Update CSV data source at runtime?

Posted: Thu Jun 04, 2020 1:38 pm
by odklizec
Hi,

Check the sample code I just posted here:
https://www.ranorex.com/forum/viewtopic ... 542#p60542
I think it may help you too?

Re: Update CSV data source at runtime?

Posted: Thu Jun 04, 2020 2:08 pm
by mrt
Thanks for the quick reply.

So basically you are creating a new data connector and then reassign it to the current connector, right?

Is this absolutely mandatory, or it it possible to edit the current connector directly?

thank you!

Re: Update CSV data source at runtime?

Posted: Thu Jun 04, 2020 2:15 pm
by odklizec
Hi,

Actually, I'm not creating a new data connector file, but editing existing one. The code just uses Ranorex data connector methods to edit csv files.

Re: Update CSV data source at runtime?

Posted: Thu Jun 04, 2020 2:19 pm
by mrt
Ok, then I understood something wrong in the code. :)

I will investigate further - and I have to find out how I could achieve this more dynamically, without knowing the filename or storage place.

Is it somehow possible to ge the filename out of the current connector?

I want to keep my code as flexible as possible, and I do not want to edit all user codes if I somehow move the data sources to another place.
Also I want to use the code snippet in multiple test cases with different data sources.

Re: Update CSV data source at runtime?

Posted: Fri Jun 05, 2020 7:33 am
by mrt
Alright, I am one step closer. :)

When getting the current connector this way:

Code: Select all

var currentConnector =  TestSuite.CurrentTestContainer.DataContext.Source;
it is of type Ranorex.Core.Data.DataCache
respective

Code: Select all

var currentConnector =  TestSuite.CurrentTestContainer.DataContext.Source.Connector;
is of type Ranorex.Core.Data.DataCache.Connector
and both do not provide a filename attribute.
From the help text I don't get the difference between Source and Source.Connector, maybe someone else knows?

Anyway, when specifying the type of class CsvConnector like this:

Code: Select all

var currentConnector = TestSuite.CurrentTestContainer.DataContext.Source.Connector
			 as Ranorex.Core.Data.CsvDataConnector;
it is possible to access the filename property with

Code: Select all

currentConnector.FileName
So that answers my other question.
But unfortunately it doesn't solve my problem.
I see in your code you are building up the CSV from scratch (with headers and rows+columns), which is not practical in my case.

My CSV has about 50 columns and I do not want to re-create all entries and headers - I just want to alter one value in one row and I believe there is a more elegant way? :)

Re: Update CSV data source at runtime?

Posted: Mon Jun 08, 2020 8:14 am
by RobinHood42
Hi,

If the data connector is already defined in your test suite, you just need the name of it in order to load the data.
I think this is the section you need.
var source = DataSources.Get(dataConnectorName);
tc.DataContext.Source=source;
tc.DataContext.Source.Load();
I also posted a code to update the DataCache a while back. You can also try if this solves your issue.
https://www.ranorex.com/forum/updating- ... tml#p41587

Cheers,
Robin

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 10:52 am
by mrt
Hi,

yes, my data connector is already defined (and used) in the current test case.

If I use your code and/or the code from your linked topic, I see the datacache getting updated, but the CSV file is not updated.

Any other ideas what I am doing wrong?

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 10:57 am
by odklizec
Hi,

Could you please post entire code? You see, it's impossible to tell what's wrong without seeing the source code ;)

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 11:16 am
by mrt
Sure. :)
I have tried lots of different code lines, so I tried to cleanup a bit.
I tried using the StoreData method because I assumed this would eventually write to the CSV, but it doesn't.
That is what I tried at last:

Code: Select all

string myNewDynamicString = "test";
        	
var currentContainer = TestSuite.CurrentTestContainer;
var currentConnector = TestSuite.CurrentTestContainer.DataContext.Source.Connector as Ranorex.Core.Data.CsvDataConnector;          
			            
Ranorex.Core.Data.ColumnCollection propTableColumnsCSV; //= myConn.Columns;  
Ranorex.Core.Data.RowCollection propTableRowsCSV; // = myConn.Rows;  
            
currentConnector.LoadData(out propTableColumnsCSV, out propTableRowsCSV);
            
System.Diagnostics.Debug.Print(propTableRowsCSV[0].Values[2]); // prints old value from CSV
propTableRowsCSV[0].Values[2] = myNewDynamicString;
System.Diagnostics.Debug.Print(propTableRowsCSV[0].Values[2]); // prints new dynamic string
       
currentConnector.StoreData(propTableColumnsCSV, propTableRowsCSV);
I tried using the StoreData method because I assumed this would eventually write to the CSV, but it doesn't.

The other approach does not work either:

Code: Select all

Ranorex.Core.Data.DataCache myConn = DataSources.Get("myConnectorName");  
myConn.Load();
myConn.Rows[0].Values[2]=myNewDynamicString;
myConn.Store();
Just to clarify:
I have the CSV data connector defined and assigned to the current test case.
The first step in the testcase is the user code.
I assume to get the value in CSV file on disk immediately changed after user code is finished (Step is completed).

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 11:55 am
by odklizec
Hi,

StoreData method is definitely the right way how to save CSV file and I'm sure it works OK. At least it works for me? ;) However, are you sure you checked the right CSV file? I guess that the CSV you are using is included in solution and copied to output directory? Therefore, if you load and edit data source in runtime, you must examine CSV file stored in bin\Debug or Release folder and not the original data connector source file, stored in solution structure.

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 12:22 pm
by mrt
Hi,

OMG - you are so right. :roll:
I guess that the CSV you are using is included in solution and copied to output directory?
Thank you very much.
I indeed checked the original file and not the one from the bin/Debug folder.

Alright, so now the CSV in the output folder gets updated.
But on next test run it will be overwritten with the original CSV again, right? - and I lose the updated value.

How to cope with that?

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 12:30 pm
by odklizec
Hi,

Well, I'm storing data to csv only in case I want to grab new reference values. Therefore, I'm manually copying (after each test run) newly generated csv files to source solution. I'm not using StoreData to edit source csv files between individual tests. in case I want to store some data from "test 1" and use them in "test 2", I'm using either global/test case/smartfolder parameters (for individual values) or newly create CSV file, where I don't care that the values are lost after each test suite re-run.

What exactly you trying to achieve?

Re: Update CSV data source at runtime?

Posted: Wed Jun 10, 2020 12:43 pm
by mrt
Thanks for your very quick answers. :D

Yes, I used global variable before, but this way I cannot re-check an existing item (by executing the validation test only), because the global variable will be empty.

I try to explain what my situation is, I am not fully happy with my approach anyway:

1) In my Web-Application, I let Ranorex create objects with hundreds of parameters, these are read from several CSVs
2) When saving, the Web-App creates a dynamic name for the object
3) Then I need to validate all those parameters in two places
  • Inside the WebUI (frontend)
  • Inside another application, something like a database frontend (backend)
In both applications I have to use the new dynamically created name for searching after the desired object.

So my idea was - if I already have several CSVs with a line for every object - I add a column where I put in the dynamically created name after creation.
So next time, when running the validation test, I can find the object by using the new name and validate all parameters.

Does that make sense anyhow? ^^

thank you!

Re: Update CSV data source at runtime?

Posted: Tue Sep 29, 2020 8:30 pm
by bansalrehana
Is this absolutely mandatory, or it it possible to edit the current connector directly? 9apps cartoon hd