Hi guys,
I had a serious problem with Katalon Studio, that all TestCase variables are always binding as String from TestSuite to TestCase. And I couldn’t live with it, because it is painful to manually parse everything from String to desired data types. Funny thing is, that not even Boolean or Number works with data binding, but even if it was working, there is no support for custom data types.
So I had an idea, to use Description column in TestCase variables view to define desired data type like this:
(Notice, that I have type set to String for all variables)
These data types are supported, but you can easily add your custom data types (I’ll show it later):
String, Bool, Char, Byte, Short, Int, Long, Float, Double, LocalDate, LocalTime, LocalDateTime, ZonedDataTime, Period, Duration, List, Map and CustomData
All you need to do, is to download this file and put it your Keywords under some package (I put it in package called “_” so I can use it very quickly.
Data.zip (2.4 KB)
Then, it only requires small work at the beggining of project:
- Create these two global variables in you default profile:
CurrentTestCaseId of type String
CurrentTestCaseVariables of type Map
- Put these two lines to TestListener @BeforeTestCase method:
public class TestListener {
@BeforeTestCase
def beforeTestCase(TestCaseContext testCaseContext) {
GlobalVariable.CurrentTestCaseId = testCaseContext.getTestCaseId()
GlobalVariable.CurrentTestCaseVariables = testCaseContext.getTestCaseVariables()
}
}
And finally, you just have to put this line of code at the beginning of every TestCase, where you want to use automatic parsing of variables data types:
def d = __.Data.parse()
And then all TestCase variables are in object “d”, and already parsed to correct data type So you can use it like this:
And if you want to use your custom data parser, just implement ITestDataParser and put it as default parser in Data.parse method (default is TestDataParser created by me)
And if you want your custom data to work with my TestDataParser, either imlement my ICustomTestData interface to provide your parsing logic, or just extend my JsonTestData class, which requires absolutely no work from your side and it will automatically parse your type from string using Json.
So to summarize, you can use easily any data types in your TestCase variables, which require a little bit of work at the beginning, but then it’s all just about making all your custom data types to extend JsonTestData and putting 1 line of code at the beginning of each TestCase. So usage is very easy
And 1 last thing: the code I created is not superb yet, and I haven’t tested it thoroughly, but it seems to be working good. If you guys find any bugs, please tell me
And if anybody of you want to improve this code, please also post it here, thanks