Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何访问所有的;“文本”;cucumber的json属性';s";“步骤”-对象并将其记录到控制台?_Javascript_Json_Webdriver Io_Cucumberjs_Wdio V6 - Fatal编程技术网

Javascript 我如何访问所有的;“文本”;cucumber的json属性';s";“步骤”-对象并将其记录到控制台?

Javascript 我如何访问所有的;“文本”;cucumber的json属性';s";“步骤”-对象并将其记录到控制台?,javascript,json,webdriver-io,cucumberjs,wdio-v6,Javascript,Json,Webdriver Io,Cucumberjs,Wdio V6,我试图将cumber'step'-项目的步骤描述的字符串/regex记录到控制台。这是一个示例步骤 Given Alice is hungry 。。。这是步骤定义的第一行 Given( /^Alice is hungry$/, () => { 我试图在webdriverio的Cumber特定钩子的上下文中,使用作为参数提供的“step”对象的字符串表示,将“Given Alice is Hunger”记录到控制台。运行 beforeStep: function ({ uri, feat

我试图将cumber'step'-项目的步骤描述的字符串/regex记录到控制台。这是一个示例步骤

Given Alice is hungry
。。。这是步骤定义的第一行

Given( /^Alice is hungry$/, () => {
我试图在webdriverio的Cumber特定钩子的上下文中,使用作为参数提供的“step”对象的字符串表示,将“Given Alice is Hunger”记录到控制台。运行

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禁用下一行无未定义 log(\`Running step“${step.text}`); } …我得到的只是

 [0-0] Running "undefined"
 [0-0] Running "undefined"
我尝试了以下两个选项:

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.feature.children.steps.text}
children
是一个数组,因此您不能只键入
.steps
。您需要首先访问某个元素,如
[0]。步骤

与步骤类似,步骤是一个数组:

“步骤”:[…]
${step.text}
无效,因为
step
没有属性text。它具有以下键:

uri
feature

我接受了帕维斯曼的回答作为问题的解决方案

对于希望在执行cumber步骤时将其记录到控制台的任何人,只需将其添加到wdio配置文件:

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;
},

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