We are building a new experience for our SmartBear Community.
This site is now read-only. You can still browse and search as usual. Learn More
This site is now read-only. You can still browse and search as usual. Learn More
Hi,
Re iterating over a spreadsheet to find a row.
I'm using this table in my Excel lookup spreadsheet.
| CustomerId | Customer Name | Surname | DOB |
| 1 | John | Lennon | 9/10/1940 |
| 2 | Paul | McCartney | 18/6/1942 |
| 3 | George | Harrison | 25/2/1943 |
| 4 | Ringo | Starkey | 7/7/1940 |
To do the lookup, I wrote some Groovy script. I used this page to help me read the Excel spreadsheet... https://www.anilvij.com/how-to-read-data-from-excel-using-groovy-soapui/ in order to write my Groovy script.
Here's the Groovy script you can use as an example. Note : This works for xls files, not xlsx.
import com.eviware.soapui.support.XmlHolder;
import jxl.*;
import jxl.write.*;
// For the sake of example, these are the values returned from your db and these are what we will look for in Excel.
def forename = "George";
def surname = "Harrison";
def dob = "25/2/1943";
Workbook workbook1 = Workbook.getWorkbook(new File("C:\\temp\\CustomerData.xls")) //file containing the data
Sheet sheet1 = workbook1.getSheet(0) //Index 0 will read the first sheet from the workbook, you can also specify the sheet name with "Sheet1"
//Sheet sheet1 = workbook1.getSheet("Sheet1")
size= sheet1.getRows().toInteger() //get the number of rows, each row is a data set
log.info("Number of rows is ${size}.");
// We will use these in our loop.
def found = false;
def row = 0;
log.info("Looking for ${forename} ${surname}");
for (int i = 1; i < size; i++) {
def id = sheet1.getCell(0,i).getContents(); // getCell(column,row) getContents() teases out the value.
def sourceForename = sheet1.getCell(1,i).getContents();
def sourceSurname = sheet1.getCell(2,i) .getContents();
log.info("${id} - ${sourceForename} ${sourceSurname}");
if((forename.equals(sourceForename)) && (surname.equals(sourceSurname))) {
// We've found a match. Let's note the row and break out of loop to save time.
found = true;
row = i;
break;
}
}
if(found){
// Compare DOB
// You don't have to use assert, you could use a boolean check or whatever suits.
assert dob == sheet1.getCell(3,row).getContents();
log.info("DOB is a Match!");
} else {
log.info("Could not find ${sourceForename} ${sourceSurname} in the Excel lookup sheet.");
}
workbook1.close() //close the file
Hi Rich,
Thanks so much for responding to my query, and apologies that my explanation of what I'm trying to do wasn't so clear.
Basically the test customer database was created in 2017, and the DOB of the customers in the DB is dynamic which keeps the age static. So the Excel doc we have works out their age in days at the time the test pack was produced, and in another column deducts that value from "TODAY" to produce a new date of birth which maintains the customer's age. So if the customer was 10,000 days old when the test pack was created, the date in this column will mean they are always 10,000 days old (this reflects the DOB in the database). Hope that makes more sense.
So the value in the DOB field is dynamic, and I want to use this Excel doc as a data source. My aim is that I will be able to choose a test customer name from the test pack at random, and using the values in <Forename></Forename> and <Surname></Surname>, ReadyAPI will look up the DOB from the corresponding row in the Data Source and pull that value in the DOB column through into the <DOB></DOB> field.
Thanks
Rachael
Hi,
It took me a bit of time to understand what is required. I was blindsided by the use of the word datasource.
Is this right? Your datasource is actually the db as is where you are getting the forename and surname for each test. During each test, you're using the Excel spreadsheet as lookup to verify that the service DOB and Excel DOB match. What you need is the means to iterate over each row in Excel until you find the matching forename/surname pairing and return the DOB for that.
I have never tried reading Excel in Groovy, but I can give it a go if my understanding above is correct.