跳至主要内容

Slack 报告器

wdio-slack-reporter 是一个第三方软件包,更多信息请参见GitHub | npm

version downloads license webdriverio

来自WebdriverIO 的报告器,使用传入 WebhookWeb API 将结果发送到Slack
此软件包与WebdriverIO 6.x 及更高版本兼容。

Slack 通知截图

Notification

WebdriverIO 4.x 或更低版本兼容性

此项目与 WebdriverIO 6.x 及更高版本兼容。
如果您使用的是 4.x 或更低版本,请使用wdio-slack-reporter

安装

最简单的方法是将@moroo/wdio-slack-reporter 作为开发依赖项保留在您的package.json 中。

{
"devDependencies": {
"@moroo/wdio-slack-reporter": "8.1.0"
}
}

您可以简单地通过以下方式完成:

  • NPM
npm install @moroo/wdio-slack-reporter --save-dev
  • Yarn
yarn add -D @moroo/wdio-slack-reporter

有关如何安装WebdriverIO 的说明,请参见此处

配置

在 wdio.conf.js 文件的顶部添加

ES6

// wdio.conf.js
import SlackReporter from '@moroo/wdio-slack-reporter';

为了使用报告器,您需要在 wdio.conf.js 中将 slack 添加到您的 reporters 数组中。

// wdio.conf.js
export.config = {
reporters: [
[
SlackReporter,
{
slackOptions: {
type: 'web-api',
channel: process.env.SLACK_CHANNEL || 'Cxxxxxxxxxx',
slackBotToken: process.env.SLACK_BOT_TOKEN || 'xoxb-xxxxxxxxxx-xxxxxx...',
},
}
],
],
};

配置选项

支持以下配置选项。为了发送通知,您必须设置webhookweb-api。如果同时设置了web-apiwebhook,则使用web-api

Webhook(传入 Webhook)

webhook(必需

传入 Webhook 应将通知发送到的 Slack 频道。如果未配置 URL,则不会发送通知。

  • 范围:webhook
  • 类型:字符串

slackName(可选

username 的值将在 Slack 通知中显示为发送者。

  • 范围:webhook
  • 类型:字符串
  • 默认值:"WebdriverIO Reporter"

slackIconUrl(可选

要在 Slack 中显示的图标的 URL

  • 范围:webhook
  • 类型:字符串
  • 默认值:"https://webdriverio.node.org.cn/img/webdriverio.png"

Web API(Slack 机器人)

slackBotToken(必需

Web API 应将通知发送到的 Slack 频道。需要机器人用户令牌。机器人访问令牌始终以xoxb开头。机器人令牌需要chat:writefiles:write 的 OAuth 范围。请参见下文了解更多详细信息。

  • 范围:web-api
  • 类型:字符串

channel(必需

发送消息到的频道、私人组或 IM 频道。可以是编码的 ID 或名称。请参见下文了解更多详细信息。"如何查找频道 ID" - stackoverflow -

  • 范围:web-api
  • 类型:字符串

uploadScreenshotOfFailedCase(可选

将此选项设置为 true 以将屏幕截图附加到失败的用例。

  • 范围:web-api
  • 类型:布尔值
  • 默认值:true

notifyDetailResultThread(可选

此选项仅在 notifyTestFinishMessage 选项为 true 时有效。

如果您希望在发布到 Slack 的测试结果通知中添加包含结果详细信息的线程,请将此选项设置为 true。

  • 范围:web-api
  • 类型:布尔值
  • 默认值:true

filterForDetailResults(可选

此选项仅在 notifyDetailResultThread 选项为 true 时有效。

将您想要的过滤器添加到此选项的数组中,详细结果将在 Slack 中被过滤掉并发送到线程。(如果没有过滤器(数组为空或未定义),则应用所有过滤器。)过滤器列表passedfailedpendingskipped

  • 范围:web-api
  • 类型:数组(passed | failed | pending | skipped)
  • 默认值:['passed', 'failed', 'pending', 'skipped']

createScreenshotPayload(可选

此选项自定义上传的屏幕截图的有效负载,用于测试失败。

  • 范围:web-api
  • 类型:函数

createResultDetailPayload(可选

此选项自定义通知测试详细结果的有效负载。

  • 范围:web-api
  • 类型:函数

通用

title(可选

将此选项设置为测试标题。

  • 范围:webhookweb-api
  • 类型:字符串

resultsUrl(可选

提供指向测试结果的链接。它是通知中的可点击链接。

  • 范围:webhookweb-api
  • 类型:字符串

notifyTestStartMessage(可选

将此选项设置为 true 以发送测试开始通知。

  • 范围:webhookweb-api
  • 类型:布尔值
  • 默认值:true

notifyFailedCase(可选

将此选项设置为 true 以将测试结果中失败的用例附加到报告给 Slack。

  • 范围:webhookweb-api
  • 类型:布尔值
  • 默认值:true

notifyTestFinishMessage(可选

将此选项设置为 true 以发送测试结束通知。

  • 范围:webhookweb-api
  • 类型:布尔值
  • 默认值:true

useScenarioBasedStateCounts(可选) - 仅限 Cucumber

将此选项设置为 true 以将状态计数从基于测试(步骤)更改为基于场景。(仅限 Cucumber)

  • 范围:webhookweb-api
  • 类型:布尔值
  • 默认值:false

emojiSymbols(可选

此选项更改默认的 Emoji 集。

  • 范围:webhookweb-api
  • 类型:对象
  • 默认值
    • passed - ✅ :white_check_mark:
    • failed - ❌ :x:
    • skipped - ⏸ :double_vertical_bar:
    • pending - ❔ :grey_question:
    • start - 🚀 :rocket:
    • finished - 🏁 :checkered_flag:
    • 观看 - ⏱ :stopwatch:

createStartPayload (可选)

此选项自定义测试开始时通知的负载。

  • 范围:webhookweb-api
  • 类型:函数

createFailedTestPayload (可选)

此选项自定义测试失败时通知的负载。

  • 范围:webhookweb-api
  • 类型:函数

createResultPayload (可选)

此选项自定义通知测试结果的负载。

  • 范围:webhookweb-api
  • 类型:函数

使用传入 Webhook

如果您使用 webhook,则无法进行线程和上传。
因此,与uploadthread 相关的函数不可用。

配置示例

// wdio.conf.js
import SlackReporter from "@moroo/wdio-slack-reporter";

export.config = {
reporters: [
[
SlackReporter, {
// Set the Slack Options used webhook.
slackOptions: {
type: 'webhook',
webhook: process.env.SLACK_WEBHOOK_URL || "https://hooks.slack.com/........",
slackName: "WebdriverIO Reporter",
slackIconUrl: "https://webdriverio.node.org.cn/img/webdriverio.png",
},
// Set the Title of Test.
title: 'Slack Reporter Test',
// Set the Test Results URL.
resultsUrl: process.env.JENKINS_URL,
// Set the notification of Test Finished
notifyTestFinishMessage: true,
// Set the scenario-based state count (Only Cucumber)
useScenarioBasedStateCounts: true,
// Customize Slack Emoji Symbols.
emojiSymbols: {
passed: ':white_check_mark:',
failed: ':x:',
skipped: ':double_vertical_bar:',
pending: ':grey_question:',
start: ':rocket:',
finished: ':checkered_flag:',
watch: ':stopwatch:'
},
// Override the createStartPayload function.
createStartPayload: function (runnerStats: RunnerStats): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
},
// Override the createFailedTestPayload function.
createFailedTestPayload: function (testStats: TestStats): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
},
// Override the createResultPayload function.
createResultPayload: function (runnerStats: RunnerStats, stateCounts: StateCount): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
}
}
],
],
};

使用 Web API

要使用 api,您需要如下所示的范围。
chat:writefiles:write。有关更多详细信息,请参见下文

配置示例

// wdio.conf.js
import SlackReporter from "@moroo/wdio-slack-reporter";

export.config = {
reporters: [
[
SlackReporter, {
// Set the Slack Options used web-api.
slackOptions: {
type: 'web-api',
slackBotToken: process.env.SLACK_BOT_TOKEN || "xoxb-xxxxxxxxxx-xxxxxx...",,
channel: process.env.SLACK_CHANNEL || "Cxxxxxxxxxx",
// Set this option to true to attach a screenshot to the failed case.
uploadScreenshotOfFailedCase: true,
// Set this option to true if you want to add thread with details of results to notification of test results posted to Slack.
notifyDetailResultThread: true,
// Set the Filter for detail results. (array is empty or undefined, all filters are applied.)
filterForDetailResults: [
'passed',
'failed',
'pending',
'skipped'
],
// Override the createScreenshotPayload function.
createScreenshotPayload: function (testStats: TestStats, screenshotBuffer: Buffer): FilesUploadArguments {
const payload: FilesUploadArguments = {
// do something...
}
return payload;
},
// Override the createResultDetailPayload function.
createResultDetailPayload: function (runnerStats: RunnerStats, stateCounts: StateCount): ChatPostMessageArguments {
const payload: ChatPostMessageArguments = {
// do something...
}
return payload;
}
},
// Set the Title of Test.
title: 'Slack Reporter Test',
// Set the Test Results URL.
resultsUrl: process.env.JENKINS_URL,
// Set the notification of Test Finished
notifyTestFinishMessage: true,
// Set the scenario-based state count (Only Cucumber)
useScenarioBasedStateCounts: true,
// Customize Slack Emoji Symbols.
emojiSymbols: {
passed: ':white_check_mark:',
failed: ':x:',
skipped: ':double_vertical_bar:',
pending: ':grey_question:',
start: ':rocket:',
finished: ':checkered_flag:',
watch: ':stopwatch:'
},
// Override the createStartPayload function.
createStartPayload: function (runnerStats: RunnerStats): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
},
// Override the createFailedTestPayload function.
createFailedTestPayload: function (testStats: TestStats): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
},
// Override the createResultPayload function.
createResultPayload: function (runnerStats: RunnerStats, stateCounts: StateCount): IncomingWebhookSendArguments {
const payload: IncomingWebhookSendArguments = {
// do something...
}
return payload;
}
}
],
],
};

支持的 API

getResultsUrl

类型() => string | undefined

获取结果 URL。

// getResultsUrl.spec.ts
import SlackReporter from '@moroo/wdio-slack-reporter';

describe('Get the resultsUrl value', function () {
before(function () {
const resultsUrl = SlackReporter.getResultsUrl();
if (resultsUrl) {
// do something...
}
});
it('Do something', function () {
// do something...
});
});

setResultsUrl

类型(url: string) => void

设置结果 URL。
(如果每次测试结果的 URL 发生变化,这将很有用。)

// setResultsUrl.spec.ts
import SlackReporter from '@moroo/wdio-slack-reporter';
import { RESULTS_URL } from '../constants';

describe('Set the resultsUrl value', function () {
before(function () {
const resultsUrl = RESULTS_URL + new Date().toISOString();
SlackReporter.setResultsUrl(resultsUrl);
});
it('Do something', function () {
// do something...
});
});

uploadFailedTestScreenshot

类型(data: string | Buffer) => void

将屏幕截图作为线程添加到失败的测试通知中。
(如果您使用的是 webhook,这将打印警告并不会执行任何操作。)

// terminal console
WARN @moroo/slack-wdio-reporter: Not using web-api or disabled notifyFailedCase or uploadScreenshotOfFailedCase options.
// wdio.conf.js
export.config = {
afterTest: async function (test, context, result) {
if (error) {
const result = await browser.takeScreenshot();
SlackReporter.uploadFailedTestScreenshot(result);
}
}
}

postMessage

类型(payload: ChatPostMessageArguments) => Promise<WebAPICallResult>

向 Slack 发布消息。
(如果您使用的是 webhook,这将抛出错误。)

// terminal console
ERROR @moroo/slack-wdio-reporter: Not using web-api.
// post.spec.ts
import SlackReporter, {
ChatPostMessageArguments,
WebAPICallResult,
} from '@moroo/wdio-slack-reporter';

describe('Post Function Test', function () {
it('Post a message', async function () {
const payload: ChatPostMessageArguments = {
// do something...
};
const result: WebAPICallResult = await SlackReporter.post(payload);
});
});

upload

类型(payload: FilesUploadArguments) => Promise<WebAPICallResult>

将文件上传到 Slack。
(如果您使用的是 webhook,这将抛出错误。)

// terminal console
ERROR @moroo/slack-wdio-reporter: Not using web-api.
// upload.spec.ts
import SlackReporter, {
FilesUploadArguments,
WebAPICallResult,
} from '@moroo/wdio-slack-reporter';

describe('Upload Function Test', function () {
it('Upload a files', async function () {
const payload: FilesUploadArguments = {
// do something...
};
const result: WebAPICallResult = await SlackReporter.upload(payload);
});
});

send

类型(payload: IncomingWebhookSendArguments) => Promise<IncomingWebhookResult>

向 Slack 发送消息。
(如果您使用的是 web-api,这将抛出错误。)

// terminal console
ERROR @moroo/slack-wdio-reporter: Not using webhook.
// send.spec.ts
import SlackReporter, {
IncomingWebhookSendArguments,
IncomingWebhookResult,
} from '@moroo/wdio-slack-reporter';

describe('Sand Function Test', function () {
it('Send a message', async function () {
const payload: IncomingWebhookSendArguments = {
// do something...
};
const result: IncomingWebhookResult = await SlackReporter.send(payload);
});
});

添加屏幕截图

如果您想将屏幕截图作为线程添加到失败的测试通知中,请在截取屏幕截图后添加uploadFailedTestScreenshot 函数。

// wdio.conf.js
export.config = {
afterTest: async function (test, context, result) {
if (error) {
const result = await browser.takeScreenshot();
SlackReporter.uploadFailedTestScreenshot(result);
}
}
}

已知问题

未同步

如果出现以下错误,请在wdio.conf.js 中设置reporterSyncIntervalreporterSyncTimeout

ERROR @wdio/runner: Error: Some reporters are still unsynced: SlackReporter
//wdio.conf.js
export.config = {
//
// Determines in which interval the reporter should check if they are synchronized if they report their logs asynchronously (e.g. if logs are streamed to a 3rd party vendor).
reporterSyncInterval: 500,
// Determines the maximum time reporters have to finish uploading all their logs until an error is being thrown by the testrunner.
reporterSyncTimeout: 20000,
}

Jasmine 选项 - expectationResultHandler

在此处添加 uploadFailedTestScreenshot 函数也不起作用。
这是因为该函数在每次测试后都会执行,因此当前测试未知。

// wdio.conf.js
export.config = {
jasmineOpts: {
// Jasmine default timeout
defaultTimeoutInterval: 60000,
//
// The Jasmine framework allows interception of each assertion in order to log the state of the application
// or website depending on the result. For example, it is pretty handy to take a screenshot every time
// an assertion fails.
expectationResultHandler: function (passed, assertion) {
if (passed) {
return;
}
/*
Adding the uploadFailedTestScreenshot function here doesn't work either.
This is because the function works after every test, so the current test is unknown.

[x] const result = await browser.takeScreenshot();
[x] SlackReporter.uploadFailedTestScreenshot(result);
*/
},
},

// Add it here.
afterTest: async function (test, context, result) {
if (result.error) {
const result = await browser.takeScreenshot();
SlackReporter.uploadFailedTestScreenshot(result);
}
}
}

欢迎!我如何帮助您?

WebdriverIO AI Copilot