【问题标题】:How can I access all the "text" properties of the json of cucumber's "step"-object and log them to the console?如何访问黄瓜“步骤”对象的 json 的所有“文本”属性并将它们记录到控制台?
【发布时间】:2021-01-04 15:28:04
【问题描述】:

我正在尝试将黄瓜“步骤”-oject 的步骤描述的字符串/正则表达式记录到控制台。这是一个示例步骤

Given Alice is hungry

...这是步骤定义的第一行

Given( /^Alice is hungry$/, () => {

我正在尝试将“鉴于 Alice 饿了”记录到控制台,使用作为参数提供的“步骤”对象的字符串表示,在 webdriverio 的特定于黄瓜的钩子的上下文中。跑步

beforeStep: function ({ uri, feature, step }, context) {
    console.log(`Running "${JSON.stringify(step, null, 4)}"`);
}

...产生这个输出:

[0-0] Running "{
    "uri": "featureFiles\\dev\\my-first-feature-file.feature",
    "feature": {
        "type": "Feature",
        "tags": [],
        "location": {
            "line": 8,
            "column": 1
        },
        "language": "en",
        "keyword": "Functionality",
        "name": "Eating too many cucumbers is not good for you",
        "children": [
            {
                "type": "Scenario",
                "tags": [
                    {
                        "type": "Tag",
                        "location": {
                            "line": 10,
                            "column": 3
                        },
                        "name": "@Szenario-Eating-all-the-cucumbers"
                    }
                ],
                "location": {
                    "line": 11,
                    "column": 3
                },
                "keyword": "Szenario",
                "name": "Eating a few is no problem",
                "steps": [
                    {
                        "type": "Hook",
                        "location": {
                            "line": 187,
                            "column": 0,
                            "uri": "node_modules\\@wdio\\cucumber-framework\\build\\index
.js"
                        },
                        "keyword": "Hook",
                        "text": ""
                    },
                    {
                        "type": "Step",
                        "location": {
                            "line": 12,
                            "column": 3
                        },
                        "keyword": "Given",
                        "text": "Alice is hungry"
                    },
                    {
                        "type": "Step",
                        "location": {
                            "line": 13,
                            "column": 5
                        },
                        "keyword": "When ",
                        "text": "she eats 3 cucumbers'"
                    },
                    {
                        "type": "Step",
                        "location": {
                            "line": 14,
                            "column": 5
                        },
                        "keyword": "Then ",
                        "text": "she will be full"
                    },
                    

但是,当我使用

beforeStep: function ({ uri, feature, step }, context) {
    // eslint-disable-next-line no-undef
    console.log(\`Running step "${step.text}"`);
}

...我得到的只是

 [0-0] Running "undefined"
 [0-0] Running "undefined"

我已经尝试了以下 2 个选项:

console.log(`Running "${step.feature.children.steps.text}"`);
console.log(`Running "${feature.children.steps.text}"`);

在这两种情况下都会产生以下结果:

[0-0] Error in "BeforeStep Hook"
Cannot read property 'text' of undefined

我做错了什么?

【问题讨论】:

  • 我没有直接在 step 下看到文字。看起来文本在 step.children.steps 下。您可能需要使用来获取所有文本值。

标签: javascript json webdriver-io cucumberjs wdio-v6


【解决方案1】:

我们来看这个例子:

${step.feature.children.steps.text}

children 是一个数组,所以你不能只输入.steps。您需要先访问某个元素,例如[0].steps

与steps类似,steps是一个数组:

"steps": [ ... ]

这个${step.text} 不起作用,因为step 没有属性文本。它有以下键:

uri
feature

【讨论】:

    【解决方案2】:

    我接受了 pavelsman 的答案作为问题的解决方案。

    对于任何想要在执行时将黄瓜步骤记录到控制台的人,只需将其添加到 wdio-configuration 文件中:

    let scenarioIndex = 0;
    let stepIndex = 0;
    
    beforeStep: function ({uri, feature, step}, context) {
        if(typeof step.feature.children[scenarioIndex] !== "undefined") {
            let keyword = step.feature.children[scenarioIndex].steps[stepIndex].keyword;
            let stepText = step.feature.children[scenarioIndex].steps[stepIndex].text;
            if (stepText !== "") {
                console.log(keyword + stepText);
            }
            stepIndex++;
        }
    },
    afterScenario: function () {
        scenarioIndex++;
        stepIndex = 0;
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2018-02-08
      • 2015-09-15
      相关资源
      最近更新 更多