Determine if a Spreadsheet Cell contains Numeric or Text data

While working with some text data from a spreadsheet, I needed to read different kinds of SKU data. In some cases it would be a number such as 1313, while in others it might be a mix of text and numbers such as OU812. Reding the value is normally done with .getNumericCellValue() or .getNumericCellValue(), but what happens when you don't know what the next value would be? To overcome that, it's possible to the use .getCellType() and adjust the read based on type. Using some previous code, reading values from a spreadsheet and populating a Global List would look like the following: for (loop = 1; loop <=rowCount; loop++) { //Assign spreadsheet columns to variables String cellType=sheet.getRow(loop).getCell(0).getCellType() //Determine if the cell contains Text or Numeric data if (cellType=="1"){ String cellTextValue=sheet.getRow(loop).getCell(0).getStringCellValue() GlobalVariable.inventorySKU[loop]=cellTextValue }else{ long cellIntValue=sheet.getRow(loop).getCell(0).getNumericCellValue() GlobalVariable.inventorySKU[loop]=cellIntValue } } In this case, 1 is String and 0 is Integer or Numeric. Based on the length of the SKU, I used long to […]

Adding entries to an open Excel spreadsheet during runtime

When running the API test for several thousand entries, I noted that matching up an invalid request with the correct source data could be a needle in the haystack affair. Instead, it would be better to record any errors right alongside the source data in the XLSX spreadsheet file. That can be done by writing to a new cell during runtime. The process is only a couple of additional lines of code. To start, a new file needs to be opened using: FileOutputStream outFile = new FileOutputStream(new File("//Postman Testing//Data Files//pricing.xlsx") Data can then be written to the sheet using, createCell(5).setCellValue(): The .createCell(5) refers to column 5 in the spreadsheet. I'm using columns 0-4 for source data. sheet.getRow(loop).createCell(5).setCellValue("ERROR: Failure Message on pricing request " + loop + " " + failure_message) At the end, all data needs to be written to the workbook: workbook.write(outFile) And like other files, it needs to be closed when finished: outFile.close() During runtime, if an error […]

Create a new and random UUID for an API call

When working with an API call, it may be necessary to create a new and valid UUID. This can actually be accomplished in a single line of code. def order_id = UUID.randomUUID().toString() This can be sent to the API object as a variable mydate = new Date() def order_id = UUID.randomUUID().toString() //Initialize Order initialize_order = WS.sendRequest(findTestObject('Initialize Order', [('order_id') : order_id, ('date') : mydate]))

A quick way to get the duration of a test using Groovy

From a previous test I worked on, I wanted to point out how to record the execution time of a test. It uses the TimeCategory and TimeDuration libraries. Since my API test doesn't run within a Test Suite, it doesn't record the execution time, but it's still possible to get that information using code similar to the following. import groovy.time.TimeCategory import groovy.time.TimeDuration def timeStart = new Date() //Test Code goes between the Start and Stop definitions def timeStop = new Date() TimeDuration duration = TimeCategory.minus(timeStop, timeStart) log.logWarning("Execution Time: " + duration)

Data Driven API Testing with Katalon using Spreadsheet Data

In expanding my use of Katalon Studio, I have started working on automated API testing. Like Postman, you can set up a manual API test to confirm a request/response combination. But it's possible to take that further and not only send multiple API requests, but validate the response. For a really good tutorial on putting together the API test, take a look at this video. It steps through creating the API object and configuring parameters. Katalon Studio API Testing Following up from that, the code listed below creates a data driven API test wherein it reads data from an XLSX spreadsheeting using the POI model. The purpose of the test is to send a proper request for an inventory item and confirm the price of the item is correct. The spreadsheet contains the inventory data and the price of the item. Additionally, the test records the Response Time and the Status Code of the request. Within Postman this would display […]