I would like to get the name of the current test case as I would like to use that as part of a new user’s username. I want to wrap up creating a new user through the site in a single keyword.
I am new to groovy, am I right in thinking TestCaseContext is abstract and can’t be instantiated in a custom keyword? Is there a concrete type I can use?
This works perfectly for a parent test case. testCaseContext.getTestCaseId(), gives the name of the test case fine.
But in scenario where there’s a parent Test case, which calls sub testcases, using callTestCase, from within the listener, it always gives the parent test case id.
It should ideally give the name of the called sub-test case.
Not sure, if the listener gets called for a test case that is called from another test case.
what about this? Pass in the scripts class which is it’s name and do a search in the scripts folder for the file then return the folder name which is the test case name.
/**
* pass in script's class such as this.getClass() to return test case name by getting parent folder name
* @param scriptClass = this.getClass()
* @ return String
*/
@Keyword
String getTestCaseName(java.lang.Class scriptClass = this.getClass()) {
String groovyFile = scriptClass.toString().replace('class ','') + '.groovy'
def directory = new File('Scripts')
def filePattern = ~/${groovyFile}/
if (!directory.isDirectory())
{
System.exit(-2)
}
String testCaseName = null
def findFileNameClosure =
{
if (filePattern.matcher(it.name).find())
{
testCaseName = it.getParentFile().getName().toString()
}
}
directory.eachFileRecurse(findFileNameClosure)
return testCaseName
}