建立自訂報告器
概觀
您可以使用提供的結果物件產生自己的報告。這至少可以透過兩種主要方式完成
作為獨立檔案
步驟 1. 建立檔案
在個別檔案中定義您的自訂報告器(例如 `custom_reporter.js`)。選擇回呼或 Promise 來表示報告已完成。
custom_reporter.js
module.exports = {
write : function(results, options, done) {
console.log('custom reporting...');
done();
}
};
custom_reporter.js
module.exports = {
write: async function(results, options) {
console.log('custom reporting...');
}
};
步驟 2. 執行報告器
使用自訂報告器的正確路徑執行以下命令
nightwatch --reporter=junit --reporter=/path/to/custom_reporter.js
執行以下命令來產生多個報告(內建的 HTML 報告和 custom_reporter)– 自 v2.2+
nightwatch --reporter=/path/to/custom_reporter.js --reporter=html
作為 NPM 套件
自訂報告器也可以發佈到 NPM。
範例
如果您不熟悉發佈 NPM 套件,請先閱讀建立和發佈無範圍的公開套件指南。
`index.js` 檔案需要實作與基於檔案的自訂報告器相同的介面
index.js
module.exports = {
write: async function(results, options) {
console.log('custom reporting...');
}
};
用法
- 安裝您想要使用的自訂報告器的 NPM 套件
npm i <nightwatch-custom-reporter>
- 使用自訂報告器
nightwatch --reporter=<nightwatch-custom-reporter>