Report Portal 報告器
一個 WebdriverIO 報告器外掛程式,用於將結果報告到 Report Portal (http://reportportal.io/)。
安裝
最簡單的方式是將 wdio-reportportal-reporter
和 wdio-reportportal-service
作為您 package.json
中的開發相依性。
{
"devDependencies": {
"wdio-reportportal-reporter": "^7.0.0",
"wdio-reportportal-service": "^7.0.0"
}
}
有關如何安裝 WebdriverIO
的說明,請參閱此處。
設定
在您的 wdio.conf.js 檔案中設定輸出目錄
const reportportal = require('wdio-reportportal-reporter');
const RpService = require("wdio-reportportal-service");
const conf = {
reportPortalClientConfig: { // report portal settings
token: '00000000-0000-0000-0000-00000000000',
endpoint: 'https://reportportal-url/api/v1',
launch: 'launch_name',
project: 'project_name',
mode: 'DEFAULT',
debug: false,
description: "Launch description text",
attributes: [{key:"tag", value: "foo"}],
headers: {"foo": "bar"}, // optional headers for internal http client
restClientConfig: { // axios like http client config - https://github.com/axios/axios#request-config
proxy: {
protocol: 'https',
host: '127.0.0.1',
port: 9000,
auth: {
username: 'mikeymike',
password: 'rapunz3l'
}
},
timeout: 60000
}
},
reportSeleniumCommands: false, // add selenium commands to log
seleniumCommandsLogLevel: 'debug', // log level for selenium commands
autoAttachScreenshots: false, // automatically add screenshots
screenshotsLogLevel: 'info', // log level for screenshots
parseTagsFromTestTitle: false, // parse strings like `@foo` from titles and add to Report Portal
cucumberNestedSteps: false, // report cucumber steps as Report Portal steps
autoAttachCucumberFeatureToScenario: false, // requires cucumberNestedSteps to be true for use
sanitizeErrorMessages: true, // strip color ascii characters from error stacktrace
sauceLabOptions : {
enabled: true, // automatically add SauseLab ID to rp tags.
sldc: "US" // automatically add SauseLab region to rp tags.
}
};
exports.config = {
// ...
services: [[RpService, {}]],
reporters: [[reportportal, conf]],
// ...
};
其他 API
可以使用以下方式存取 Api 方法
const reporter = require('wdio-reportportal-reporter')
方法說明
reporter.addAttribute({key, value})
– 為目前測試新增屬性。key
(字串,選用) - 屬性金鑰。它必須是非空字串。value
(字串,必填) – 屬性值。它必須是非空字串。
reporter.addAttributeToCurrentSuite({key, value})
- 為目前套件新增屬性。key
(字串,選用) - 屬性金鑰。它必須是非空字串。value
(字串,必填) – 屬性值。它必須是非空字串。
reporter.addDescriptionToCurrentSuite(description)
- 為目前套件新增一些字串。description
(字串) - 描述內容。文字可以使用 Markdown 格式化。
reporter.addDescriptionToAllSuites(description)
- 為所有即將推出的套件新增一些字串。(在 before all 鉤子中使用,以便每個套件都獲得相同的描述)description
(字串) - 描述內容。文字可以使用 Markdown 格式化。
reporter.sendLog(level, message)
– 將日誌傳送至目前套件\測試項目。level
(字串) - 日誌等級。值 ['trace', 'debug', 'info', 'warn', 'error']。message
(字串)– 日誌訊息內容。
reporter.sendFile(level, name, content, [type])
– 將檔案傳送至目前套件\測試項目。level
(字串) - 日誌等級。值 ['trace', 'debug', 'info', 'warn', 'error']。name
(字串)– 檔案名稱。content
(字串) – 附件內容type
(字串,選用) – 附件 MIME 類型,預設為image/png
message
(字串)– 日誌訊息內容。
reporter.sendLogToTest(test, level, message)
- 將日誌傳送至特定測試。test
(物件) - 來自afterTest\afterStep
wdio 鉤子的測試物件level
(字串) - 日誌等級。值 ['trace', 'debug', 'info', 'warn', 'error']。message
(字串)– 日誌訊息內容。
reporter.sendFileToTest(test, level, name, content, [type])
– 將檔案傳送至特定測試。test
(物件) - 來自afterTest\afterStep
wdio 鉤子的測試物件level
(字串) - 日誌等級。值 ['trace', 'debug', 'info', 'warn', 'error']。name
(字串)– 檔案名稱。content
(字串) – 附件內容type
(字串,選用) – 附件 MIME 類型,預設為image/png
message
(字串)– 日誌訊息內容。
請注意:sendLog
\sendFile
將日誌傳送至目前執行的測試項目。這表示如果您在沒有活動測試的情況下(例如從鉤子或套件層級)傳送日誌,則不會在 Report Portal UI 中報告。
當您需要將螢幕截圖或日誌從 wdio afterTest 鉤子傳送至失敗的測試項目時,方法 sendLogToTest
\sendFileToTest
非常有用。
Mocha 範例
const reportportal = require('wdio-reportportal-reporter');
const path = require('path');
const fs = require('fs');
exports.config = {
...
async afterTest(test) {
if (test.passed === false) {
const filename = "screnshot.png";
const outputFile = path.join(__dirname, filename);
await browser.saveScreenshot(outputFile);
reportportal.sendFileToTest(test, 'info', filename, fs.readFileSync(outputFile));
}
}
...
Jasmine 範例
const reportportal = require('wdio-reportportal-reporter');
const path = require('path');
const fs = require('fs');
exports.config = {
...
async afterTest(test) {
if (test.passed === false) {
const filename = "screnshot.png";
const outputFile = path.join(__dirname, filename);
await browser.saveScreenshot(outputFile);
//!!
Object.assign(test, {title: test.description}}
reportportal.sendFileToTest(test, 'info', filename, fs.readFileSync(outputFile));
}
}
...
WDIO Cucumber「5.14.3+」範例
const reportportal = require('wdio-reportportal-reporter');
exports.config = {
...
afterStep: async function (uri, feature, { error, result, duration, passed }, stepData, context) {
if (!passed) {
let failureObject = {};
failureObject.type = 'afterStep';
failureObject.error = error;
failureObject.title = `${stepData.step.keyword}${stepData.step.text}`;
const screenShot = await global.browser.takeScreenshot();
let attachment = Buffer.from(screenShot, 'base64');
reportportal.sendFileToTest(failureObject, 'error', "screnshot.png", attachment);
}
}
...
}
取得 Report Portal UI 啟動頁面的連結
const RpService = require("wdio-reportportal-service");
...
onComplete: async function (_, config) {
const link = await RpService.getLaunchUrl(config);
console.log(`Report portal link ${link}`)
}
...
或更複雜的方式
const RpService = require("wdio-reportportal-service");
...
onComplete: async function (_, config) {
const protocol = 'http:';
const hostname = 'example.com';
const port = ':8080'; // or empty string for default 80/443 ports
const link = await RpService.getLaunchUrlByParams(protocol, hostname, port, config);
console.log(`Report portal link ${link}`)
}
...
將測試報告至現有啟動
如果您想將測試報告至現有的活動啟動,您可以透過環境變數 REPORT_PORTAL_LAUNCH_ID
將其傳遞至報告器。您有責任完成啟動以及啟動此類啟動。
export REPORT_PORTAL_LAUNCH_ID=SomeLaunchId
npm run wdio
授權條款
此專案是根據 MIT 授權條款授權的 - 有關詳細資訊,請參閱 LICENSE.md 檔案