如何在Visual Studio代码中创建步骤定义

时间:2017-11-16 11:48:12

标签: visual-studio-code cucumberjs

我正在尝试为我在功能文件中添加的步骤创建步骤定义。我在我的VS Code中安装了Cucumber Full Support。但是我仍然无法创建步骤定义。有人可以帮我解决这个问题?

PFB示例代码使其更加清晰  我有一个功能文件包含两个步骤,如

And event list has the following IDs 
  | offlineEventId |
  | 1              |
  | 2              |

And event list has the following IDS and desc
  | offlineId | offlineDescription  |
  | 1         | abc                 |
  | 2         | xyz                 |

我已经为两者写了步骤说明

this.Then(/^event list has the following offline IDs/, function(expectedEventTable){

});



this.Then(/^event list has the following offline event IDs and desc/, function(expectedEventTable) {

});


 But both the steps in feature file is pointing to the first method in step definition 

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

您的步骤定义中似乎有一些错误。首先,名称必须以$ /结尾,而不仅仅是/。其次,您的名称与要素文件中的名称不匹配。您在步骤定义的名称中处于“离线”状态,但“离线”未出现在要素文件中。我不知道为什么你会让两个步骤都运行相同的步骤定义,我猜想在某些你没有共享的代码中某处出现了错误。所以我使用了以下功能文件Test.feature:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

//This function gets cookie value based on conference ID
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

,步骤在步骤定义文件Test.steps.ts

Feature: Test

Scenario: Test Cucumber is working
    Given I want it to work
    When I Run this feature
    Then I should see the right console log messages 
    And event list has the following IDs 
    | offlineEventId |
    | 1              |
    | 2              |
    And event list has the following IDS and desc
    | offlineId | offlineDescription  |
    | 1         | abc                 |
    | 2         | xyz                 |

这给出了以下输出:

module.exports = function () {

  this.Given(/^I want it to work$/, function(){
    console.log('Given step');
  });

  this.When(/^I Run this feature$/, function(){
    console.log('When step');
  });

  this.Then(/^I should see the right console log messages$/, function(){
    console.log('Then step 1');
  });

  this.Then(/^event list has the following IDs$/, function(expectedEventTable){
    console.log('Then step 2');
  });

  this.Then(/^event list has the following IDS and desc$/, function(expectedEventTable) {
    console.log('Then step 3');
  });
};