2017-03-21 32 views
1

我是量角器和黄瓜框架的新手。我遵循量角器网站的步骤和这里https://semaphoreci.com/community/tutorials/getting-started-with-protractor-and-cucumber。我有一个配置文件配置了黄瓜框架选项,功能文件和步骤定义文件。但是,当我运行我的cucumber-config文件时,它无法识别我的步骤定义并始终引发错误。对此有何帮助?以下是我的设置文件。量角器配置文件不拾取黄瓜步骤定义

Protractor folder structure

//cucumber-config.js

exports.config = { 
    seleniumAddress: 'http://localhost:4444/wd/hub', 
    capabilities: { 
     browserName:'chrome' 
    }, 
    framework: 'custom', 
    frameworkPath: require.resolve('protractor-cucumber-framework'), 
    specs: [ 
    './features/*.feature' 
    ], 
    cucumberOpts: { 
    require: ['./features/step_definitions/*.steps.js'], 
    tags: [], 
    strict: true, 
    format: ["pretty"], 
    dryRun: false, 
    compiler: [] 
    }, 
    onPrepare: function() { 
    browser.manage().window().maximize(); 
    } 
}; 

//testone.feature

#features/test.feature 
Feature: Running Cucumber with Protractor 

    Scenario: Protractor and Cucumber Test 
     Given I go to "https://angularjs.org/" 
     When I add "Be Awesome" in the task field 
     And I click the add button 
     Then I should see my new task in the list 

//testone_steps.js

var chai = require('chai'); 
var chaiAsPromised = require('chai-as-promised'); 

chai.use(chaiAsPromised); 
var expect = chai.expect; 

module.exports = function() { 
    this.Given(/^I go to "([^"]*)"$/, function(site) { 
    browser.get(site); 
    }); 

    this.When(/^I add "([^"]*)" in the task field$/, function(task) { 
    element(by.model('todoList.todoText')).sendKeys(task); 
    }); 

    this.When(/^I click the add button$/, function() { 
    var el = element(by.css('[value="add"]')); 
    el.click(); 
    }); 

    this.Then(/^I should see my new task in the list$/, function(callback) { 
    var todoList = element.all(by.repeater('todo in todoList.todos')); 
    expect(todoList.count()).to.eventually.equal(3); 
    expect(todoList.get(2).getText()).to.eventually.equal('Do not Be Awesome') 
     .and.notify(callback); 
    }); 
}; 

在运行时protractor cucumber-conf.js,我得到下面的错误...

/opt/protractor_tests 
➔ protractor cucumber.config.js 
(node:3963) DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead. 
[21:19:17] I/launcher - Running 1 instances of WebDriver 
[21:19:17] I/hosted - Using the selenium server at http://localhost:4444/wd/hub 
Feature: Running Cucumber with Protractor 

    Scenario: Protractor and Cucumber Test 
    ? Given I go to "https://angularjs.org/" 
    ? When I add "Be Awesome" in the task field 
    ? And I click the add button 
    ? Then I should see my new task in the list 

Warnings: 

1) Scenario: Protractor and Cucumber Test - features/testone.feature:4 
    Step: Given I go to "https://angularjs.org/" - features/testone.feature:5 
    Message: 
    Undefined. Implement with the following snippet: 

     Given('I go to {stringInDoubleQuotes}', function (stringInDoubleQuotes, callback) { 
     // Write code here that turns the phrase above into concrete actions 
     callback(null, 'pending'); 
     }); 

2) Scenario: Protractor and Cucumber Test - features/testone.feature:4 
    Step: When I add "Be Awesome" in the task field - features/testone.feature:6 
    Message: 
    Undefined. Implement with the following snippet: 

     When('I add {stringInDoubleQuotes} in the task field', function (stringInDoubleQuotes, callback) { 
     // Write code here that turns the phrase above into concrete actions 
     callback(null, 'pending'); 
     }); 

3) Scenario: Protractor and Cucumber Test - features/testone.feature:4 
    Step: And I click the add button - features/testone.feature:7 
    Message: 
    Undefined. Implement with the following snippet: 

     When('I click the add button', function (callback) { 
     // Write code here that turns the phrase above into concrete actions 
     callback(null, 'pending'); 
     }); 

4) Scenario: Protractor and Cucumber Test - features/testone.feature:4 
    Step: Then I should see my new task in the list - features/testone.feature:8 
    Message: 
    Undefined. Implement with the following snippet: 

     Then('I should see my new task in the list', function (callback) { 
     // Write code here that turns the phrase above into concrete actions 
     callback(null, 'pending'); 
     }); 

1 scenario (1 undefined) 
4 steps (4 undefined) 
0m00.000s 
[21:19:22] I/launcher - 0 instance(s) of WebDriver still running 
[21:19:22] I/launcher - chrome #01 failed 1 test(s) 
[21:19:22] I/launcher - overall: 1 failed spec(s) 
[21:19:22] E/launcher - Process exited with error code 1 

/opt/protractor_tests 
➔ 

更新,执行错误

[15:22:59] I/launcher - Running 1 instances of WebDriver 
[15:22:59] I/hosted - Using the selenium server at http://localhost:4444/wd/hub 
Feature: Running Cucumber with Protractor 

    Scenario: Protractor and Cucumber Test 
    √ Given I go to "https://angularjs.org/" 
    √ When I add "Be Awesome" in the task field 
    √ And I click the add button 
    × Then I should see my new task in the list 

Failures: 

1) Scenario: Protractor and Cucumber Test - features\testone.feature:4 
    Step: Then I should see my new task in the list - features\testone.feature:8 
    Step Definition: features\step_definitions\testone.steps.js:22 
    Message: 
    Error: function timed out after 5000 milliseconds 
     at Timeout.<anonymous> (<local>\ProtractorTests\node_modules\cucumber\lib\user_code_runner.js:91:22) 
     at ontimeout (timers.js:365:14) 
     at tryOnTimeout (timers.js:237:5) 
     at Timer.listOnTimeout (timers.js:207:5) 

1 scenario (1 failed) 
4 steps (1 failed, 3 passed) 
0m05.049s 
[15:23:19] I/launcher - 0 instance(s) of WebDriver still running 
[15:23:19] I/launcher - chrome #01 failed 1 test(s) 
[15:23:19] I/launcher - overall: 1 failed spec(s) 
[15:23:19] E/launcher - Process exited with error code 1 
error Command failed with exit code 1. 

回答

6

它试图用CucumberJS 2.0.0+语法 - 这是在发展的时刻。

要么降级CucumberJS 1.3.1或以下,或这样做是为了你的脚步定义:

var chai = require('chai'), 
    expect = chai.expect, 
    chaiAsPromised = require('chai-as-promised'); 
chai.use(chaiAsPromised); 

var {defineSupportCode} = require('cucumber'); 

defineSupportCode(({Given, When, Then}) => { 
    Given(/^I go to "([^"]*)"$/, function(site) { 
    return browser.get(site); 
    }); 

    When(/^I add "([^"]*)" in the task field$/, function(task) { 
    return element(by.model('todoList.todoText')).sendKeys(task); 
    }); 

    When(/^I click the add button$/, function() { 
    var el = element(by.css('[value="add"]')); 
    return el.click(); 
    }); 

    Then(/^I should see my new task in the list$/, function() { 
    var todoList = element.all(by.repeater('todo in todoList.todos')); 
    expect(todoList.count()).to.eventually.equal(3); 
    return expect(todoList.get(2).getText()).to.eventually.equal('Do not Be Awesome'); 
    }); 
}); 

哪个是CucumberJS 2.0.0+语法

编辑

有是在CucumberJS 2.0.0中设置超时的两种方法

默认超时时间

这是设置默认的超时时间为你的所有的情景:

let scenarioTimeout = 200 * 1000, 
    {defineSupportCode} = require('cucumber'); 

defineSupportCode(({setDefaultTimeout}) => { 
    setDefaultTimeout(scenarioTimeout); 
}); 

在这个例子中,我的情况下超时设置为200秒。你可以改变这个,只要你认为合适。

单个步骤

这是为了设置超时一个缓慢步骤:

When(/^I click the add button$/, {timeout: 60 * 1000}, function() { 
    var el = element(by.css('[value="add"]')); 
    return el.click(); 
    }); 

在此示例中,超时被设置为60秒,则可能需要此更大或更小,取决于该步骤在做什么。

+0

用户是否需要在“When”语句中返回webdrivers promise? – cnishina

+0

就我个人而言,我在我的所有步骤定义中使用了我最后一次声明中的回报。所以我会说你应该。这实际上会使该步骤在显示为传递前执行 –

+0

@Kyle非常感谢。它的工作原理,但我总是以'错误:功能在5000毫秒后超时'为失败步骤获得最后一步。我用完整的错误信息更新了问题。请帮帮我。 – mmar

0

在您的配置文件:

require: ['./features/step_definitions/*.steps.js'], 

但是你的文件是:testone_steps.js,它应该是:testone.steps.js

D'你看有什么区别?只需将_更改为.,因为在您的配置文件中使用.