用cucumber+编写功能测试;量角器+;角2

用cucumber+编写功能测试;量角器+;角2,cucumber,angular,protractor,Cucumber,Angular,Protractor,我在angular2中使用cucumber+量角器编写函数测试时遇到一些问题。 这是我的密码 cucumberCong.js exports.config = { seleniumAddress: 'http://localhost:4444/wd/hub', seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar', framework

我在angular2中使用cucumber+量角器编写函数测试时遇到一些问题。
这是我的密码

cucumberCong.js

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',

    seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',

    framework: 'custom',

    frameworkPath: '../node_modules/protractor-cucumber-framework/index.js',

    // Spec patterns are relative to this directory.
    specs: [
        'spec/**/*.feature'
    ],

    capabilities: {
        'browserName': 'chrome',
        'version': 'ANY'
    },

    baseUrl: 'http://' + (process.env.HTTP_HOST || 'localhost') + ':' + (process.env.HTTP_PORT || webServerDefaultPort),

    cucumberOpts: {
        require: 'spec/**/*.js',
        tags: '@dev',
        format: undefined,
        profile: false,
        'no-source': true
    }
};
login.feature

Feature: Login

  @dev
  Scenario: Login funtion
    Given go login page "http://localhost:8080/#/login"
    Then input userName "username", password "password"
    Then click login
    Then see About page "http://localhost:8080/#/home"
loginSpec.ts

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

var HttpBackend = require('http-backend-proxy');
var proxy = new HttpBackend(browser);


module.exports = function loginPage() {
    var expect = chai.expect;

    this.setDefaultTimeout(500 * 1000);

    this.Given(/^go login page "([^"]*)"$/, function (url, next) {
        browser.driver.get(url);
        next();
    });

    this.Then(/^input userName "([^"]*)", password "([^"]*)"$/, function (userName, password, next) {
        browser.driver.findElement(by.id('userName')).sendKeys(userName);
        browser.driver.findElement(by.id('pass')).sendKeys(password);
        next();
    });

    this.Then(/^click login$/, function (next) {
        proxy.whenGET('http://localhost:3000/login').respond(function(method, url) {
            return [200, {"data": "test"}];
        });
        browser.driver.findElement(by.id('login')).click();
        next();
    });

    this.Then(/^see About page "([^"]*)"$/, function (url, next) {
        expect(browser.getLocationAbsUrl()).to.equal(url);
        next();
    });
};
我的问题是:
1.有时用户名和密码不能输入到元素中,但解析仍然是pass。我不知道为什么。
2.我想使用
'http-backend-proxy'
来模拟数据,而不向服务器发送请求,但它不起作用,错误是
未定义
。发送请求时如何模拟数据

请帮帮我,谢谢。

关于1)

量角器是异步的。您对回调的用法

next()
在零件之后直接执行

browser.driver.findElement(by.id('userName'))
因此,前面语句中的异步部分有时足够快,有时更晚

sendKeys(userName);
这将导致特征步骤变为绿色,如果量角器操作正确,则无法进行控制

您也不使用chai expect

建议1.1:不要使用下一个回调,链异步承诺:

this.Then(/^input userName "([^"]*)", password "([^"]*)"$/, 
function (userName, password) {
    return browser.driver.findElement(by.id('userName'))
        .sendKeys(userName)
    .then(function(){
        return browser.driver.findElement(by.id('pass'))
           .sendKeys(password);
    }    
});
外部的“this.then”函数使用承诺链的结果作为退出点,替换next()回调以离开该步骤

建议1.2:使用chai的expect(也与Chaias异步)

关于(二),,
您的回复必须在正文中包含ng app标签,请参见

您是否找到解决方案?请用答案更新您的问题。
this.Then(/^input userName "([^"]*)", password "([^"]*)"$/, 
function (userName, password) {
    return browser.driver.findElement(by.id('userName'))
        .sendKeys(userName)
    .then(function(){
        return browser.driver.findElement(by.id('pass'))
           .sendKeys(password);
    }    
});
this.Then(/^go to some url$/, function () {
    var targetUrl = "http://example.com";
    browser.get(targetUrl);
    expect(browser.getCurrentUrl()).to.eventually.equal(targetUrl);
});