javascript - 通过步骤定义和页面对象在 cucumber Protractor 中实现场景大纲

标签 javascript angular protractor cucumber scenarios

有谁知道为什么当我尝试运行此步骤时可能会显示为“未定义”。下面您将看到一个示例,其中我尝试尝试 cucumber 场景大纲,并且我的“示例:”部分有 1 个条目。此外,还包含页面对象和步骤定义。由于某种原因,当我尝试运行它时,我收到如下错误:

1) Scenario: Verify user can search # ..\features\automation\regression\samplezz.feature:13
   √ Before # ..\support\world.js:21
   √ Given I navigate to the ea site # ..\step_definitions\ea_page_steps.js:4
   √ Then I click on the search icon # ..\step_definitions\ea_page_steps.js:8
   ? When I search for the word angular
       Undefined. Implement with the following snippet:

         When('I search for the word angular', function (callback) {
           // Write code here that turns the phrase above into concrete actions
           callback(null, 'pending');
         });

这是功能文件

Feature: sampleZZ
The purpose of this feature file is to navigate to the eaAutomationa site


  Scenario Outline: Verify user can search
    Given I navigate to the ea site
    Then I click on the search icon
    When I search for the word <word>

    Examples:
    |word|
    |angular|

这是步骤定义:

let {Given, Then, When} = require('cucumber');


Given(/^I navigate to the ea site$/, function(){
    return browser.get(this.url.ud); });

Then(/^I click on the search icon$/, function(){    
    return this.pages.udPage.click_Search();
    });

When(/^I search for the word "([^"]*)" $/, function(word){
     return this.pages.udPage.enter_SearchText(word) });

这是页面对象

class UDPage extends require('../base_page.js') {   constructor() {
    super();
    this.eaautomation = by.css('#new_searchicon > i');
    this.eaLogo = by.css('//#header_logo');   };   click_Search() {
    return element(this.eaautomation).click();   }

  enter_SearchText(text){
    return element(this.eaautomation).sendKeys(text);   }

} module.exports = UDPage;

注意:我在框架中有一个通用构造函数,因此在编写测试时不必导入任何页面。 有人可以帮我理解第 3 步有什么问题,它一直显示未定义吗?

使用以下内容

“依赖项”:{ “柴”:“4.1.2”, "chai-as-promised": "7.1.1", "脉轮": "1.5.0", “ cucumber ”:“4.0.0”, " cucumber -html-reporter": "3.0.4", “fs”:“0.0.2”, “路径”:“0.12.7”, "Protractor ": "5.3.0", “Protractor cucumber 框架”:“4.2.0” }

已编辑-添加 config.js

let path = require('path'),
  environment = require('./environment');

exports.config = Object.assign({}, environment, {

  seleniumAddress: 'http://localhost:4444/wd/hub', // 'http://localhost:4444/wd/hub' to run locally

  capabilities: {
    "browserName": "chrome",
    "shardTestFiles": true,
    "maxInstances": 1,
    "ignoreProtectedModeSettings": true
  },

  specs:[
      '../features/automation/regression/sample2.feature',
  ],



  params: {
    environment: 'qa1', // dit, qa4, or qa1
    platform: 'browser', // browser or mobile
    path: {
      page_objects: path.resolve(__dirname + '/../page_objects'), // Default directory for the page objects
     page_factory: path.resolve(__dirname + '/../page_objects/page_factory.js'), // Default page factory location
     projectRoot: path.resolve(__dirname + '/../') // Default root for the automation
    }
  }

});

最佳答案

删除步骤定义中"([^"]*)"周围的双引号,功能文件中没有引号。

When(/^I search for the word ([^"]*)$/, function(word){});


 enter_SearchText(text) {
    var me = this;

    // wait 15 seconds
    return browser.sleep(15*1000).then(function(){
        return element(me.eaautomation).sendKeys(text);
    });
 }

关于javascript - 通过步骤定义和页面对象在 cucumber Protractor 中实现场景大纲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838734/

相关文章:

Angular - 在 APP_INITIALIZE 上加载的 HTTP 拦截器和配置

email - 如何使用 mail-listener2 获取最新电子邮件

javascript - 如何从 Protractor 中的 promise 中获取嵌套的 JSON

angular - 在 angular2 中迁移到 RC4 后的问题

angular - 没有 NavController 的提供者

javascript - 用于下载 pdf 文件的 Protractor e2e 测试用例

javascript - Underscore _.findWhere 在数组中存在时找不到元素

javascript - 具有 mui-datatables 的对象数组

javascript - Nearley Moo - 语法不适用于使用 Moo 词法分析器

javascript - mousemove 在控制台打开时更频繁地触发?