Real-time Automation Results in Slack Channel

Atlassian_Landing_Page_Marquee

     We know that Slack is a powerful communication tool that brings the team together, wherever you are. Slack offers channels to stay organized and focused on central spaces for conversations, files, tools, and people. Slack provides workspaces, applications, and various team collaboration options. Slack applications help to act as chatbots on various occasions.

     The major advantages of Slack include all team communication in one place, integration with various services, one to one and private groups, the ability to integrate various bots or apps to your slack channel depending on your needs.

    Nowadays, we know that most of the CI tools have the capability to integrate with slack to share real-time test execution results. In this article, I would like to share a couple of utilities written in Java that describe how to send realtime automation test results via slack channels. The automation results included both plain text results and the attached reports.

Prerequisites

  • You must have a slack account.
  • Add the latest jslack dependency into pom.xml if you are using Maven.
  • Add the latest version of following dependencies to pom.xml to handle the API operations,
    • httpclient
    • httpmime  
  • Add extentreports dependency to pom.xml file to generate the HTML automation report.

Step-by-step procedure

  1. Create an account in Slack and then create a Slack workspace. Use https://slack.com/get-started#/
  2. Create a Slack application to communicate with your channel about the automation execution status. Use https://api.slack.com/apps
  3. Create a Slack channel by clicking plus icon right to Channels in your slack workspace. Keep the Slack channel name in your mind and it useful during the scripting.
  4. Add created Slack application to your Slack channel by clicking Add an app link from the slack channel.
  5. Go to https://api.slack.com/apps and select your Slack application. Select Incoming Webhooks and activate it. Scroll down the page and you will get Webhook URL for your channel. This Webhook URL is an important parameter in your script which helps to send the messages to your Slack channel.
  6. Go to OAuth & Permissions section, there you will get Bot User OAuth Access Token which helps the Slack application to send files into your Slack Channel. Copy the Bot User OAuth Access Token and you can use it in the script.
  7. Scroll down in OAuth & Permissions page to reach the Scopes section. Make sure that following scopes are added,
    • Bot Token Scopes:
      • file:write
      • incoming:webhook
    • User Token Scopes:
      • file:write

  I have created two Slack utilities to send real-time test results and the test execution reports to the Slack channel. Following are the methods:

  • sendTestExecutionStatusToSlack helps to send the test results to the Slack channel with the help of the Webhook URL and channel name.
  • sendTestExecutionReportToSlack helps to send the automation test execution report with the help of file upload API, Bot User OAuth Token, and channel name. Following are the detailed implementation of both methods,

private static String urlSlackWebHook = “YOUR_WEBHOOK_URL”;
private static String channelName = “YOUR_CHANNEL_NAME”;
private static String botUserOAuthAccessToken = “YOUR_BOT_USER_OAuth_TOKEN”;

public void sendTestExecutionStatusToSlack(String message) throws Exception {
try {
StringBuilder messageBuider = new StringBuilder();
messageBuider.append(message);
Payload payload = Payload.builder().channel(channelName).text(messageBuider.toString()).build();

WebhookResponse webhookResponse = Slack.getInstance().send(urlSlackWebHook, payload);
webhookResponse.getMessage();
} catch (IOException e) {
System.out.println(“Unexpected Error! WebHook:” + urlSlackWebHook);
}
}

public void sendTestExecutionReportToSlack(String testReportPath) throws Exception {
String url = “https://slack.com/api/files.upload?token= ” + botUserOAuthAccessToken + “&channels=” + channelName
+ “”;
try {
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(new File(testReportPath));
builder.addPart(“file”, fileBody);
httppost.setEntity(builder.build());
HttpResponse response = null;
response = httpclient.execute(httppost);
HttpEntity result = response.getEntity();
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

     You can use above both methods based on your convenience in the automation listener class. If you are using the TestNG framework then you can implement the ITestListener interface and use both methods inside the implemented methods of ITestListener to send real-time test execution results to your Slack channel.

Sample Output: Slack View

Slack Automation Flow

    Try to use the above methods to send real-time test results to Slack channel and enjoy your automation.

make it perfect!

3 thoughts on “Real-time Automation Results in Slack Channel

  1. String url = “https://slack.com/api/files.upload?token=” + botUserOAuthAccessToken + “&channels=” + channelName
    + “”;

    in this url… what is this ‘token=”’ ? how to get it from?

    Liked by 1 person

    1. Bot user access tokens, if your Slack app includes a bot user, upon approval the JSON response will contain an additional node containing an access token to be specifically used for your bot user, within the context of the approving workspace.

      To get this, login to https://api.slack.com/ and go to “Your Apps”, select your app. Left side you can see “OAuth & Permissions”, select that option; scroll down and you can see Bot User OAuth Access Token and copy that value, then paste into inseat of “botUserOAuthAccessToken” in code.

      Like

      1. String url = “https://slack.com/api/files.upload?token=” ”
        + botUserOAuthAccessToken + “&channels = ” + channelName + “”;

        I tried like this but this is not working, I dont know why is mistake.?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s