2017-07-14 121 views
0

我有角度2应用程序。我写了基本的黄瓜测试。我已添加功能和步骤定义文件。我不明白为什么它不能读取步骤定义文件,即使我已明确定义它。当我尝试执行黄瓜测试,我得到这个错误信息:黄瓜 - 无法执行步骤定义文件

enter image description here

版本:量角器 - 5.1.2,节点-7.5,NPM -4.1.2,黄瓜-2.3.1, 量角器黄瓜-framewrok - 3.1.2

下面是示例代码:

//cucumber.conf.js 
 
exports.config = { 
 
    framework: 'custom', 
 
    frameworkPath: require.resolve('protractor-cucumber-framework'), 
 
    //seleniumAddress: 'http://localhost:4444/wd/hub', 
 
    directConnect: true, 
 
    specs: ['test/e2e/cucumber/sample.feature'], 
 
    capabilities: { 
 
    'browserName': 'chrome', 
 
    chromeOptions: { 
 
     args: [ 
 
     //"--headless", 
 
     "--disable-gpu", 
 
     '--disable-extensions', 
 
     '--no-sandbox', 
 
     '--disable-web-security' 
 
     ], 
 
    }, 
 

 
    }, 
 
    baseUrl: 'http://localhost:8080/dashboard/#/', 
 
    useAllAngular2AppRoots: true, 
 

 

 
    // cucumber command line options 
 
    cucumberOpts: { 
 
    require: ['test/e2e/cucumber/menu.steps.js'], 
 
    tags: [], 
 
    strict: true, 
 
    format: ["pretty"], 
 
    dryRun: false, 
 
    compiler: [] 
 
    }, 
 

 
    onPrepare: function() { 
 
    browser.manage().window().maximize(); 
 
    }, 
 

 
    resultJsonOutputFile: './test/e2e/results.json' 
 
} 
 

 
/** 
 
//test/e2e/cucumber/sample.feature 
 
Feature: The Dashboard has 2 views, Main view and Status view 
 

 
Scenario: I want to have 2 tabs with Displayed text "Main View" 
 
and "Status View" 
 
on the homepage 
 

 
Given I go to Dashboard homepage 
 
When I click on the Main view 
 
Then the main view page is displayed 
 
When I click on the Status view 
 
Then the status view page is displayed 
 

 
*/ 
 

 
//test/e2e/cucumber/menu.steps.js 
 
var chai = require('chai'); 
 
var chaiAsPromised = require('chai-as-promised'); 
 

 
chai.use(chaiAsPromised); 
 
var expect = chai.expect; 
 
browser.ignoreSynchronization = true; 
 
module.exports = function() { 
 
    this.Given(/^I go to Dashboard homepage$/, { 
 
     timeout: 100 * 1000 
 
    }, function() { 
 
     return browser.get('http://localhost:8080/dashboard/#/'); 
 
     // browser.waitForAngular(); 
 
    }); 
 

 
    this.When(/^I click on the Main view$/, { 
 
     timeout: 100 * 1000 
 
    }, function() { 
 
     element(by.css('[href="#/mainView"]')).click(); 
 
    }); 
 

 
    this.Then(/^the main view page is displayed$/, function() { 
 
      return browser.getCurrentUrl().then(function(url) { 
 
      console.log("URL= " + url); 
 
      }); 
 

 
      this.When(/^I click on the Status view$/, { 
 
      timeout: 100 * 1000 
 
      }, function() { 
 
      element(by.css('[href="#/statusView"]')).click(); 
 
      }); 
 

 
      this.Then(/^the status view page is displayed$/, function() { 
 
      return browser.getCurrentUrl().then(function(url) { 
 
       console.log("URL= " + url); 
 
      }); 
 
      }); 
 
     }

回答

0

如果您使用的是CucumberJS 2,则需要使用其他方式编写步骤,有关详细信息,请参见here

例子:

// features/step_definitions/file_steps.js 
 
var { 
 
    defineSupportCode 
 
} = require('cucumber'); 
 

 
defineSupportCode(function({Given}) { 
 

 
    Given(/^I need to rewrite my steps$/, function() { 
 
    return Promise.resolve('pending'); 
 
    }); 
 
    
 
});

其次要注意的是,这是不行的,你放置browser.ignoreSynchronization = true;上的文件的顶部。当用量角器读取文件时,浏览器还没有启动,所以它会失败。

如果您需要测试无角网页,请将browser.ignoreSynchronization = true;置于onPrepare()的配置文件中。

+0

谢谢。是的,黄瓜版本是不同的。现在我降级到1.3.3,它工作。我添加了'browser.ignoreSynchronization = true;'以避免超时问题。这使得Protractor不会等待Angular promise,例如$ http或$ timeout中的那些promise。 (https://stackoverflow.com/questions/44672875/scripttimeouterror-timed-out-from-task-protractor-waitforangular) – Mythri

0

根据我的经验,功能文件对格式非常敏感。就像一个yml文件,我有步骤不运行,因为制表符和空格,所以我也会检查。

相关问题