Sending a message to a Slack-Channel
This example shows a way how to send a message to a Slack-Channel. The solution uses a userdefined method that can be used in different ways. For example, you could use it after finishing a testsuite or when a specific testcase fails.
Please note our exclusion of liability when using this example.
Userdefined Method
Please note that the method uses a global parameter called “SlackWebhook_common”. Create a webhook in slack and create this global parameter in BiG EVAL with the value of the URL of the webhook.
private async Task PostToSlackAsync(string message)
{
try
{
// Get the Slack-Webhook URL from the global parameters.
var url = GetParameter("SlackWebhook_Common").ToString();
// Create a request using an URL that can receive a post.
var request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Construct the JSON-body to send to the Slack-Webhook and convert it to a byte array.
var json = "{ 'text':'" + message + "' }";
var byteArray = Encoding.UTF8.GetBytes(json);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
var dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
var statusCode = ((HttpWebResponse)response).StatusCode;
switch(statusCode)
{
case HttpStatusCode.OK:
LogInfo("Slack-Message successfully posted!");
break;
default:
throw new Exception($"Couldn't post to slack. StatusCode: {statusCode}");
}
}
catch(Exception ex)
{
LogError(ex, "Slack-Message couldn't be posted!");
}
}
Using the userdefined Method
The following testcase-script shows how to use the method to send a message to slack depending on the testcase-outcome.
// Execute test
var info = await ExecuteAsync();
// Define the Slack-Message depending ont he outcome of the testrun.
var message = "";
switch(info.TestResultStatus)
{
case "success":
message = $"The testrun {info.RunId} was successful!";
break;
default:
message = $"The testrun {info.RunId} failed with status '{info.TestResultStatus}'!";
break;
}
// Post to Slack
await PostToSlackAsync(message);
Please, show her example how to send error message to a e-mail.
Sending an email with an error message can be done in the User Interface of BiG EVAL directly. There’s no need to code.
https://bigeval.com/docs/user/alerts/alerts-overview/