Change data source in code

Ask general questions here.
bennylew
Certified Professional
Certified Professional
Posts: 22
Joined: Wed Sep 17, 2014 10:43 am

Change data source in code

Post by bennylew » Thu Dec 03, 2015 3:42 pm

Hi,

we have twi kinds of test Environments:

1. with productive data (datafile1.xlsx, Default)
2. with synthetic data (datafile2.xlsx)

I want to use zwo different exel files as data sources. The structure will be identically, the only dfference is the data.

Is it possible to change in code the data source connector from datafile1.xlsx to datafile2.xlsx? Of course I could use two different connectors, but in this case I will lose the data binding from the first connector...

Best regards
benjamin

bennylew
Certified Professional
Certified Professional
Posts: 22
Joined: Wed Sep 17, 2014 10:43 am

Re: Change data source in code

Post by bennylew » Thu Dec 10, 2015 11:46 am

No idea? :(

User avatar
Support Team
Site Admin
Site Admin
Posts: 11710
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Change data source in code

Post by Support Team » Thu Dec 10, 2015 4:55 pm

Hi bennylew,

Sorry for the late response.

Unfortunately you need to create two DataConnectors to switch the data, but instead of using the Test Case Properties you could use the following lines of code
var tc = (TestCase) TestSuite.Current.GetTestCase("TestCase");
var source = DataSources.Get("Data2");
tc.DataContext.Source=source;
tc.DataContext.Source.Load();
*Whereby "TestCase" = the name of your Testcase and "Data2" = name of the new Data Connector

Hope that helps.

Regards,
Markus (S)
.
Image