WebdriverIO - 使用 spec reporter 编辑测试标题或 属性

WebdriverIO - Edit test title or property with spec reporter

有没有办法轻松覆盖 spec reporter 以编辑 Running 属性? 我的用例是我有 2 个功能 object of chrome,这两个功能仅在移动仿真方面有所不同,因为一个是纯桌面大小,另一个是 chrome 模拟移动大小。 问题出在 spec reporter 上,我无法分辨哪个跑步者是移动设备还是桌面设备。我得到类似这样的输出:

------------------------------------------------------------------
[chrome 88.0.4324.96 LINUX #0-0] Spec: /path/to/spec.js
[chrome 88.0.4324.96 LINUX #0-0] Running: chrome (v88.0.4324.96) on LINUX
[chrome 88.0.4324.96 LINUX #0-0] Session ID: 27fe3f66-0197-4e26-ad76-dedbbb442ef5
[chrome 88.0.4324.96 LINUX #0-0]
[chrome 88.0.4324.96 LINUX #0-0] Suite 1
[chrome 88.0.4324.96 LINUX #0-0]    ✓ Test Case #1
[chrome 88.0.4324.96 LINUX #0-0]
[chrome 88.0.4324.96 LINUX #0-0] 1 passing (7.4s)
------------------------------------------------------------------
[chrome 88.0.4324.96 LINUX #1-0] Spec: /path/to/spec.js
[chrome 88.0.4324.96 LINUX #1-0] Running: chrome (v88.0.4324.96) on LINUX
[chrome 88.0.4324.96 LINUX #1-0] Session ID: 8091a4f4-5133-453b-b14a-bd9f814165a8
[chrome 88.0.4324.96 LINUX #1-0]
[chrome 88.0.4324.96 LINUX #1-0] Suite 1
[chrome 88.0.4324.96 LINUX #1-0]    ✓ Test Case #1
[chrome 88.0.4324.96 LINUX #1-0]
[chrome 88.0.4324.96 LINUX #1-0] 1 passing (7.9s)


Spec Files:      2 passed, 2 total (100% completed) in 00:00:14 

我也尝试在挂钩之前编辑套件标题,但没有成功。如果有人能帮我一个简单的方法。

您必须创建自定义报告器并重写 getHeaderDisplay 方法才能实现。

例如:

  1. 创建 my-custom-reporter.js 文件
  2. 导入 SpecReporter 报告者
  3. 声明扩展 SpecReproter 报告者的 CustomReporter class
  4. 声明getHeaderDisplay方法
  5. output 数组中添加有关手机的信息
  6. 导出模块
  7. wdio.conf.js 配置中导入自定义规范报告程序
  8. 加入reporters数组,reporters: [[CustomReporter]],

完整的工作示例:

my-custom-reporter.js 文件中:

const { default: SpecReporter } = require('@wdio/spec-reporter')

class CustomReporter extends SpecReporter {
    getHeaderDisplay(runner) {
        const combo = this.getEnviromentCombo(runner.capabilities, undefined, runner.isMultiremote).trim();
        // Here we get information about mobile device
        const mobileInfo = runner.config.capabilities['goog:chromeOptions']?.mobileEmulation?.deviceName
            ? `${runner.config.capabilities['goog:chromeOptions'].mobileEmulation.deviceName} emulated by`
            : '';
        const output = [
            `Spec: ${runner.specs[0]}`,
            `Running: ${mobileInfo} ${combo}`
        ];
    
        if (runner.capabilities.sessionId) output.push(`Session ID: ${runner.capabilities.sessionId}`);
    
        return output;
    }
}

module.exports = CustomReporter;

wdio.conf.js 文件中:

const CustomReporter = require('./my-custom-reporter')

exports.config = {
    // ... other properties
    reporters: [[CustomReporter]],
}

输出:

[chrome 89.0.4389.90 mac os x #0-0] Spec: /Users/u/test/specs/example.e2e.ts
[chrome 89.0.4389.90 mac os x #0-0] Running:  chrome (v89.0.4389.90) on mac os x
[chrome 89.0.4389.90 mac os x #0-0] Session ID: f0480174d634514e5559e560188d2d84
[chrome 89.0.4389.90 mac os x #0-0]
[chrome 89.0.4389.90 mac os x #0-0] My Login application
[chrome 89.0.4389.90 mac os x #0-0]    ✓ should login with valid credentials
[chrome 89.0.4389.90 mac os x #0-0]
[chrome 89.0.4389.90 mac os x #0-0] 1 passing (3.4s)
------------------------------------------------------------------
[chrome 89.0.4389.90 mac os x #1-0] Spec: /Users/u/test/specs/example.e2e.ts
[chrome 89.0.4389.90 mac os x #1-0] Running: iPhone X emulated by chrome (v89.0.4389.90) on mac os x
[chrome 89.0.4389.90 mac os x #1-0] Session ID: a7eacc89e3b933c895e7bf1d5811e72b
[chrome 89.0.4389.90 mac os x #1-0]
[chrome 89.0.4389.90 mac os x #1-0] My Login application
[chrome 89.0.4389.90 mac os x #1-0]    ✓ should login with valid credentials
[chrome 89.0.4389.90 mac os x #1-0]
[chrome 89.0.4389.90 mac os x #1-0] 1 passing (3.5s)


Spec Files: 2 passed, 2 total (100% completed) in 00:00:18