You could use a Test Listener. I would create a TestCaseListener which would look like this:
class TestCaseListener {
/**
* Executes before every test case starts.
* @param testCaseContext related information of the executed test case.
*/
@BeforeTestCase
def loginBeforeTestCase(TestCaseContext testCaseContext) {
def testCaseId = testCaseContext.getTestCaseId()
def idsToSendAPIRequestBeforehand = [
"Test Cases/UsersTests/CreateUser",
"Test Cases/Utility/TestConnection"
]
if(idsToSendAPIRequestBeforehand.contains(testCaseId)) {
def response = WS.sendRequest(findTestObject('AuthenticationService/POST_Sign-in'))
def statusCode = WS.getResponseStatusCode(response)
if (statusCode != 200) {
testCaseContext.skipThisTestCase()
}
}
}
}
So, before every UI Test Case, if the ID of the wanted UI Test Case is in the list, then you will send an API call to, let’s say, login. If the login is not successful, skip the UI Test Case, otherwise, go on with the UI Test Case.