Create Slack command to notify automation result – Ruby language

 

 

Nowaday, many IT project have automation testing suite, and they want notify after the automation run complete automatically. This post maybe help them, it is one of solution if their team you Slack.

You can use Incoming Webhook to post a notify request to Slack channel

screen-shot-2017-02-22-at-13-46-19

 

Here is screenshot of setting:

screen-shot-2017-02-22-at-13-48-03

and the next, you create a Ruby file like:

def send_slack_bot_summary duration,passrate,total
url = 'xxxxx'
payload = {'attachments' => [{
'title' => "Regression Summary,more details at: xxxxx",
'text' => "Testing started at #{Time.now} on #{$env[1]} #{$env[0]},#{$env[2]}",
'color' => '#4C0099',
'fields' => [
{
'title' => 'Duration (s)',
'value' => duration,
'short' => true
},
{
'title' => 'Passrate',
'value' => "#{passrate}/#{total} tests passed",
'short' => true
}
]
}
]}
init_header = {'Content-Type' => 'application/javascript'}
encode = JSON.generate(payload)
RestClient.post(url, encode, init_header)
end
def send_slack_bot_details list_tc,error
url = 'xxxxx'
payload = {'attachments' => [{
'title' => "List of failed cases,more details at: xxxxx",
'text' => "Testing started at #{Time.now} on #{$env[1]} #{$env[0]},#{$env[2]}",
'color' => '#FF3333',
'fields' => [
{
'title' => 'TC_ID',
'value' => list_tc,
'short' => true
},
{
'title' => 'Errors',
'value' => error,
'short' => true
}
]
}
]}
init_header = {'Content-Type' => 'application/javascript'}
encode = JSON.generate(payload)
RestClient.post(url, encode, init_header)
end

 

And in Slack you can see this message after automation run completed

screen-shot-2017-02-22-at-13-51-56

Advertisements

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 )

w

Connecting to %s