Slack Integration - Need ability for test results (pass / fail) to post to a slack channel

I needed the same feature, so created it myself using the Test Listeners. Before you start, you need to create a new Web Service Object and define the Slack API Web Hooks (Google it). Once you have that you can create a Slack Test Listener Class. I also have a Global Variable “integrateSlack”, which is a boolean value so that we can turn this off and on whenever we want.

class Slack {

private RequestObject slackMessage = findTestObject(‘APIs/Slack’)

private String testCaseStatus = “ERROR”

private String testSuiteStatus = “PASSED”

private void postToSlack() {

if (GlobalVariable.integrateSlack) {

WS.sendRequest(slackMessage)

}

}

/**

* Executes before every test suite starts.

* @param testSuiteContext: related information of the executed test suite.

*/

@BeforeTestSuite

def notifyBeforeTestSuite(TestSuiteContext testSuiteContext) {

slackMessage.setHttpBody(’{“text”: “’ + testSuiteContext.getTestSuiteId() + ': Started”}"’)

postToSlack()

}

/**

* Executes before every test case starts.

* @param testSuiteContext: related information of the executed test suite.

*/

@BeforeTestCase

def notifyBeforeTestCase(TestCaseContext testCaseContext) {

slackMessage.setHttpBody(’{“text”: “’ + testCaseContext.getTestCaseId() + ': Running”}"’)

postToSlack()

}

/**

* Executes after every test case ends.

* @param testSuiteContext: related information of the executed test suite.

*/

@AfterTestCase

def notifyAfterTestCase(TestCaseContext testCaseContext) {

testCaseStatus = testCaseContext.getTestCaseStatus()

Map variables = testCaseContext.getTestCaseVariables()

//slackMessage.setHttpBody(’{“text”: “’ + testCaseName + ': ’ + testCaseStatus + ‘\n’ + variables.toString() + '”}"’)

slackMessage.setHttpBody(’{“text”: “’ + testCaseContext.getTestCaseId() + ': ’ + testCaseStatus + '”}"’)

postToSlack()

if (!testCaseStatus.equals(“PASSED”)) testSuiteStatus = “FAILED”

}

/**

* Executes after every test suite ends.

* @param testSuiteContext: related information of the executed test suite.

*/

@AfterTestSuite

def notifyAfterTestSuite(TestSuiteContext testSuiteContext) {

slackMessage.setHttpBody(’{“text”: “’ + testSuiteContext.getTestSuiteId() + ': ’ + testSuiteStatus + '”}"’)

postToSlack()

}

}