Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Class library usage, coding and language questions.
1985Primus
Posts: 6
Joined: Thu Sep 30, 2021 4:25 pm

Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Post by 1985Primus » Thu Sep 30, 2021 4:43 pm

Hi,

I have the following probleme.

Everytime I run my Ranorex TestSuite, at one point Ranorex has to Enter a Code which only can be used once.
I can provide a big list of this codes as Data Scource and now nead a methode to make sure that every code is not used twice.
At the moment i see 2 options but no 100% way to implement them.

Option 1: After Entering the code the column is removed from the Datascource.
-i don't now how to delete a column from a DataSource

Option 2: Save which column was used last Run and then change the used Column for the next Run
- Im not sure if this is really possible in Ranorex.

I hope somebody can provide a way to implement one of the options or a better option :D

dhale
Posts: 84
Joined: Thu Feb 27, 2014 7:33 pm

Re: Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Post by dhale » Thu Sep 30, 2021 7:15 pm

Few options to consider:
1.) Is there any way to get the code from a database directly? <-- do this if at all possible
2.) Or maybe call a webservice directly to get the code? <-- do this if at all possible
3.) Specify excel file or text file as resource, and just replace the resource prior to each run with a new version that has new code.
4.) you say "column" but you could consider using "rows": if you have a list of codes, and they are in rows - then you could in theory have a column that indicates if a particular row has been "used"

1985Primus
Posts: 6
Joined: Thu Sep 30, 2021 4:25 pm

Re: Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Post by 1985Primus » Fri Oct 01, 2021 3:38 pm

Option 3 goes into the right direction.
But one other Idea:
Is it possible to change the used Datarange of a Testcase or Smartfolder during Runtime? Then it would be very easy. Put all codes in a DataConnector and increase the used range by 1.

1985Primus
Posts: 6
Joined: Thu Sep 30, 2021 4:25 pm

Re: Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Post by 1985Primus » Fri Oct 01, 2021 6:01 pm

I tried to implement my idea with increasesing the used Datarange during runtime but at the moment i have the problem that the changes are not saved in the Testsuite and are only active during the runtime.

Code: Select all

var currentTC = TestSuite.Current.GetTestContainer("TCName");
var Min = currentTC.DataRange.MinRange+2;
var Max = currentTC.DataRange.MaxRange+2;
currentTC.DataContext.SetRange(Min,Max);
Is it possible to save the changed range so that the next time i run the Testsuite it starts with the changed value?

1985Primus
Posts: 6
Joined: Thu Sep 30, 2021 4:25 pm

Re: Entering OneTimeUse Code during runtime or Deleting Column from DataScource

Post by 1985Primus » Wed Oct 06, 2021 3:30 pm

Now i have a working solution. It was hard work. :D

Code: Select all

 public static void RecreateVoucherCodeFile(string testCaseName, string DataSourceName)
 { 
        //Step 1: Create a new csv file with identitcal path an name to overwrite the current csv
		//create CSV data connector
		string filepath = @"C:\\Ranorex\\VoucherCodes.csv";
		Ranorex.Core.Data.CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector(DataSourceName,filepath,true);			
		csvConnector.SeparatorChar = ',';
		Ranorex.Core.Data.ColumnCollection OPCSVTableColumns = new Ranorex.Core.Data.ColumnCollection();
		Ranorex.Core.Data.RowCollection OPCSVTableRows = new Ranorex.Core.Data.RowCollection(OPCSVTableColumns);
				
	//Step 2: Load the complet csv into an Array
		csvConnector.LoadData(out OPCSVTableColumns, out OPCSVTableRows);
				
		int codeArrayLength = OPCSVTableRows.Count;			
		string[] codeArray =new string[codeArrayLength+1];
		int i = 0;
		foreach(Ranorex.Core.Data.Row dataRow in OPCSVTableRows)
		{ 
			codeArray[i] = dataRow[0].ToString();
			Report.Info(codeArray[i]);
			i = i+1;
		}

	//Step 3: Save the Array as Columns in the file
		//Emty  OPCSVTableRows
		OPCSVTableRows.Clear();

		//Add all elements to new csvfile execpt the first element which contains the used Code
		for (int z = 1; z < codeArrayLength; z++)
		{
			if (!string.IsNullOrEmpty(codeArray[z]))
			{
				OPCSVTableRows.Add(new string[1]{codeArray[z]});
			}
		}
		
	//Step 4: Save the file 
		// save CSV connector to file
		csvConnector.StoreData(OPCSVTableColumns, OPCSVTableRows);
				
	//Step 5: Let the Testsuie reload the datasource
		var tc = (TestCaseNode) TestSuite.Current.GetTestContainer(testCaseName);
		var source = DataSources.Get(DataSourceName);
		tc.DataContext.Source=source;
		tc.DataContext.Source.Load();
        
        }
I only have one small issue with this code.
After finishing a testrun including this code, Ranorex still ask me to save the changed DataSource.
How can I fix this during Runtime?