How do we handle empty string dynamically in assertions in ReadyAPI
ERROR: An error occurred [empty String], see error log for details
Hi i have a need to validate two data sources using nested loop on multiple records in several tables Steps
- Build a Rest Request
- get the datasource
- getting the data from db as expected result
- write a groovy script inorder to get a value to be validated with db
- Asert the values are equal
Issue : I have multiple empty values in the data sources from the api(actual value) and expected value db Question : When i run my script i am getting this error ERROR: An error occurred [empty String], see error log for details for empty values asserted . Please help as i am on a deadline and am aunable to find a solution online .
Hi,
The below example and function show how you can test for an empty string. Just create a brand new Groovy script step, paste in the contents and click Go in the Groovy window. Ensure you can see the logging for the answers....
def value = "1.12"; log.info("value ${value} is empty? ${ isEmpty(value)}"); value = "1"; log.info("value ${value} is empty? ${ isEmpty(value)}"); value = ""; log.info("value ${value} is empty? ${ isEmpty(value)}"); value = null; log.info("value ${value} is empty? ${ isEmpty(value)}"); def isEmpty(stringvalue) { if(stringvalue == null) { return true; } else { if((stringvalue.length() == 0) || (stringvalue == "") ) { return true; } else { return false; } } }
Also, Groovy Double does not have a tryParse function unlike other languages, but it does throw an exception which might help... Next example...
def myValue = "1.12"; log.info("is ${myValue} parsable as double? ${canBeDouble(myValue)}"); myValue = "1"; log.info("is ${myValue} parsable as double? ${canBeDouble(myValue)}"); myValue = "abc"; log.info("is ${myValue} parsable as double? ${canBeDouble(myValue)}"); def canBeDouble(theValue) { try { def a = Double.parseDouble(theValue); return true; } catch (NumberFormatException e) { //the parseDouble failed and you need to handle it here return false; } }
You can then maybe use these examples, or modify them, to test suspect values from the db before trying the assert.