Need help to read and use some of the data in a file

I’ve read through these topics for using data files and property lists:
http://www.testplant.com/phpBB2/viewtopic.php?t=147&highlight=text+file+data+property+list
http://www.testplant.com/phpBB2/viewtopic.php?t=641&highlight=text+file+data+property+list

… and I think I’ve almost figured out what I need to do, but am still missing a few pieces of the puzzle. I need a script that will read test data from a comma delimited file. I’d like the data file to contain a value indicating if the test is a negative test. As an example, let’s say my data file (with header) looks something like this:


TestId,NegativeOrPositive,LName,FInitial,Age
TC001,P,Smith,D,50
TC002,N,Jones,J,4.0
TC003,N,Williams,A,a
TC004,N,Garcia,B,0

Then I could use the following script to read the data:


put file "myTestDataFile.txt" into testData
put line 1 of testData into header
delete line 1 of testData
set the itemDelimiter to ","
repeat with data = each line of testData
	set record to ( : )
	repeat with n = 1 to the number of items in header
		set property (item n of header) of record to item n of row
	end repeat
	insert record after recordList
End Repeat

Next is where I get lost. When I send the test data to the SUT, I will want to use the NegativeOrPositive value from testData record to determine if the result is a Pass or Fail. How do I use use the NegativeOrPositive value in each record of testData?

Any help is much appreciated!

First, you have a typo in your sample code; you say “item n of row” when what you want is “item n of data”.

To access your NegativeOrPositive value, you can just iterate over the records like this:

repeat with each item of recordList
	// do some stuff with the other values
	put it.NegativeOrPositive
end repeat

I don’t know exactly how you want to use this value, so if you have any other questions, please let us know.

Nice! Thanks for the reply, and for catching the typo. I will try your suggestion. Right after I posted the question, I had also found a way to access the values, but I’m not sure if this is the best way to do it:


put file "myTestDataFile.txt" into testData 
put line 1 of testData into header 
delete line 1 of testData 
set the itemDelimiter to "," 
repeat with data = each line of testData 
   set record to ( : ) 
   repeat with n = 1 to the number of items in header 
      set property (item n of header) of record to item n of row 
   end repeat 
   insert record after recordList 
		put first item of data into testCaseId
		put second item of data into negativeOrPositive
		put third item of data into LName
		put fourth item of data into FInitial
		put fifth item of data into Age

		-- here is where the script will send one or more values to the SUT
		-- here is where the script will use the value of negativeOrPositive to determine Pass or Fail.

End Repeat

Any thoughts about doing it this way?

You’re kinda sorta doing the same thing twice there. If you were just going to do the second one, then there’s not really any reason to go through and create your “record” property lists in the first part of the loop. You could do this if you want to just use the data as you read it:

put file "myTestDataFile.txt" into testData
delete line 1 of testData
set the itemDelimiter to ","
repeat with data = each line of testData
      put item 1 of data into testCaseId
      put item 2 of data into negativeOrPositive
      Click "Last Name Field"
      TypeText item 3 of data 
      Click "First Initial Field"
      TypeText item 4 of data
      Click "Age field"
      TypeText item 5 of data
      -- here is where the script will use the value of negativeOrPositive to determine Pass or Fail.

End Repeat 

Does that help?

Yes, that helps a lot. Problem solved! Thanks.