How to configure Slack with Subject7 using Slack API

 

Subject7 allows you to integrate with Slack to receive notification messages of your test execution results after each execution in cloud run.

 

Prerequisites:

  1. Slack App
  2. Slack Channel to Receive notifications
  3. Slack API for post messages

Configuration in Slack

  1. Follow the instructions here to create an app on Slack
  2. After creating an app, follow the instructions here to add Scopes under OAuth & Permissions page
  3. Once finished, click Install to Workspace
  4. Your Bot User OAuth Token will be generated. Save it somewhere safe.
  5. Follow instructions here to get the ID of the channel where your app will post messages. Save it somewhere safe.
  6. Invite the app to the channel where your notifications will be posted using
    /invite @BOT_NAME

 

Configuration in Subject7

We will explore how we can make a REST call back at the completion of an execution set to notify test results to the team on Slack Channel.

 

A. Steps to create a REST Connection

  1. Create a REST connection to post message on Slack channel. Go to the Artifacts menu and select REST connections. Make sure you do NOT use any non-alphanumeric characters in any of the fields. Basically no dashes or underscores and other characters. 

  2. For Authentication of Slack account, enter the username and password with Type: None in Authentication Options

  3. URL Options will be the Slack API call url: https://slack.com/api/chat.postMessage

  4. REST Options → Method: POST

  5. REST Options → Headers
    Authentication = Bearer {token}
    Content-Type = application/json

  6. REST Options → Payload

{
  "channel": "channel_ID",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "Total No. of test cases passed: @execution_total \n Execution set id: @execution_set_id \n Execution id: @execution_id \n Execution Result: @execution_result%. \nApplication Build: @execution_build"
      }
    }
  ]
}

 

chrome_ezDyobXf4g.gif

You can use the dynamic reserved variables in the body of the call. 

For the execution level and test case level, the dynamic variables can be used in the Rest Connection callback, refer Execution Callbacks for dynamic variables.

NOTE: The dynamic variables of each level (execution and Test Case) correspond to just that level.
@pass_rate can be used in both execution and test case level
- for execution level @pass_rate gets sent once for the whole execution set
- for test case a response is received after each test case execution is completed. Example, if you use 3 test cases and use Test Case Level for @pass_rate, in each response you will receive 33%, 66% and 100%

 

B. Steps to create an Execution Set Callback:

  1. Configure Execution Set Callback. Browse to the Execution Set of your choice and in the callback section choose condition "Any" or “Success” or “Failure” or “Timeout” and then for Rest connection choose "Slack_Callback". You can also choose at which event/level you want the callback to occur. Either execution level (when All tests are executed) or at test case level (a call after each test execution). slack.gif

  2. Use your preferred REST tool to invoke the execution set
    cURL Request:
curl -X POST "https://platform.subject-7.com/api/v2/executions" -H "accept: application/json" -H "Authorization: Basic YW11dGhhOkBXU1gxcWF6" -H "Content-Type: application/json" -d "{\"name\":\"Regression_Test_Web\",\"configuration\":{\"pool\":\"win10\",\"buildNumber\":\"2.0.0.1\",\"browserTypes\":[\"CHROME\"],\"videoCaption\":true,\"delay\":10,\"rotations\":1,\"runtimeSpeed\":1000,\"callbackArgs\":\"URL=https://slack.com/api/chat.postMessage,text= Total No. of test cases passed: @execution_total \ Execution set id: @execution_set_id \ Execution id: @execution_id \ Execution Result: @execution_result%. \Application Build: @execution_build\",\"dataSetSelection\":[{\"testCaseName\":\"Reference_Web_Application\",\"templateName\":\"Users\",\"dataSetNames\":[\"admin\"]}]}}"

 

PowerShell - Rest Method:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("accept", "application/json")
$headers.Add("Authorization", "Basic YW11dGhhOkBXU1gxcWF6")
$headers.Add("Content-Type", "application/json")

$body = "{`n `"name`": `"Regression_Test_Web`",`n `"configuration`": {`n `"pool`": `"win10`",`n `"buildNumber`": `"2.0.0.1`",`n `"browserTypes`": [`n `"CHROME`"`n ],`n `"videoCaption`": true,`n `"delay`": 10,`n `"rotations`": 1,`n `"runtimeSpeed`": 1000,`n `"callbackArgs`": `"URL=https://slack.com/api/chat.postMessage,text= Total No. of test cases passed: @execution_total `\n Execution set id: @execution_set_id `\n Execution id: @execution_id `\n Execution Result: @execution_result%. `\nApplication Build: @execution_build`",`n `"dataSetSelection`": [`n {`n `"testCaseName`": `"Reference_Web_Application`",`n `"templateName`": `"Users`",`n `"dataSetNames`": [`n `"admin`"`n ]`n }`n ]`n }`n}"

$response = Invoke-RestMethod 'https://platform.subject-7.com/api/v2/executions' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
  • You can execute the above execution set from Subject7 portal refer Cloud Run an Execution Set

  • To invoke the execution set using PowerShell, Open Windows PowerShell on desktop, copy and paste the above PowerShell - RestMethod and Enter

 
9de8c963-847a-44ff-9aa9-442ca2d6eaae.gif
 
  • To Invoke the execution set using Postman: Import the above cURL request to Postman

dd8f64ed-6766-42a4-8f5f-ef983eeda2b4.gif
 
 

In this article we learned how to add Slack callback. We can also add multiple callbacks to the execution set. Example, Slack callback, Jira callback etc.

 

Have more question? Ask a Question


Other relevant articles

How to use Callback after a cloud execution

Execution Set Callback updating a JIRA ticket

How to use (Ref ID) of a test case in Execution Callback

Available variables for REST Connection callbacks

Modifying Available variables for REST Connection callbacks

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.