Test data from .csv file

Class library usage, coding and language questions.
User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Test data from .csv file

Post by Support Team » Mon Dec 12, 2011 9:21 am

Hi,

that's because in CSVConnector.cs the ";" is taken as separator and in your .CSV file you've chosen the "," as separator.
You have to either change the separator in your .CSV file or in the code file:

If you'd like to change the code, open CVSConnector.cs go to line 73 and alter it from
...
row = csvData[j].Split(';');
...

to
...
row = csvData[j].Split(',');
...


Do it in the same way at line 60.

Regards,
Tobias
Ranorex Support Team
.
Image

Hermch
Posts: 40
Joined: Thu May 26, 2011 7:17 am
Location: Germany

Re: Test data from .csv file

Post by Hermch » Thu Dec 15, 2011 10:20 am

Does this way to access data in a datasource also work with excel files?

I tried it this way:

Code: Select all

ExcelConnector ExcelConn = New ExcelConnector(@"..\..\login.xlsx")
        	
        	system.Data.DataRow in ExcelConn.Rows
But I cant't access the data in login.xlsx. How can I get this data within my usercode?

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

Re: Test data from .csv file

Post by Support Team » Thu Dec 15, 2011 3:42 pm

Hi,

You can use this code to access the data from a excel file:
ExcelDataConnector connector = new ExcelDataConnector("ExcelConnector", @"C:\Users\mebner\Documents\Ranorex\RanorexStudio Projects\ExcelDataDrivenTesting\TestExcelFile.xlsx", "Tabelle1", "A:C", CheckState.Unchecked);

      Ranorex.Core.Data.ColumnCollection col;
      Ranorex.Core.Data.RowCollection row;
      connector.LoadData(out col, out row);
      
      foreach(Ranorex.Core.Data.Row dataRow in row){
        Report.Info(""+dataRow["FirstName"].ToString());
        Report.Info(""+dataRow["LastName"].ToString());
        Report.Info(""+dataRow["Age"].ToString());
      }
I have used the attached excel file.

Regards,
Markus
Ranorex Support Team
.
Image

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Test data from .csv file

Post by omayer » Thu Dec 15, 2011 4:09 pm

now it reminds me to ask how to retrieve data from sql or oracle database and write it back to the database
Thanks You,
Omayer
Tipu

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Test data from .csv file

Post by omayer » Thu Dec 15, 2011 4:27 pm

Here is the code i am using to process the data but its breaks when the data contains comma and () and "" ...question is how to parse data in .csv that contains ,""()..like "firstname, lastname -(company name)" ,

public void ParseTempJoborderTestData(string filePath)
{

CSVConnector csvConnector = new CSVConnector(filePath);

int i = 0; //countRow

foreach(System.Data.DataRow row in csvConnector.Rows) //reading all the rows

{
string TempJobOrderInputFile = row[0].ToString(); //reading the row from 2 to end

int countRow = i++; // loop count
tempJobOrderInfo[countRow] = TempJobOrderInputFile; //holding each row data into an array

string entireRow = tempJobOrderInfo[countRow];
string [] separate = entireRow.Split(','); //separate the field by comma
soldToCustID[countRow] = separate[0];
contactID[countRow] = separate[1];
Tipu

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

Re: Test data from .csv file

Post by Support Team » Fri Dec 16, 2011 11:11 am

Hi!

I have created an example for you - use the following code in combination with a csv file with the contend i wrote within this post.

Please update to 3.2 to use this code.

Note, if you want to have " within your data, you have to escape this character by writing it 2 times. e.g. "test""test" for test"test.
The code:

Code: Select all

Ranorex.Core.Data.CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector("myconnector","c:\\test.csv",true);
csvConnector.SeparatorChar = ',';
Ranorex.Core.Data.ColumnCollection col;  
Ranorex.Core.Data.RowCollection row;  
csvConnector.LoadData(out col, out row);  
			
foreach(Ranorex.Core.Data.Row dataRow in row)
{ 
  Report.Info(""+dataRow["FirstName"].ToString());  
  Report.Info(""+dataRow["LastName"].ToString());  
  Report.Info(""+dataRow["Age"].ToString());  
}
The csv file:

Code: Select all

FirstName,LastName,Age
1,"""()..like ""firstname, lastname -(company name)", 3
Regards,
Martin
Ranorex Support Team
.
Image