2016-03-16 128 views
2

当我使用角度2中的黄瓜+量角器编写函数测试时,存在一些问题。
这是我的代码使用黄瓜+量角器+角度2编写函数测试

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.有时用户名和密码不能输入到元素中,但解析仍然是通过。我不知道为什么。
2.我想用'http-backend-proxy'模拟数据而不发送请求到服务器,但它不起作用,错误是angular is not defined。发送请求时如何模拟数据?

请帮助我,谢谢。关于1

+0

你有没有发现任何这个决议?请用答案更新你的问题。 –

回答

1

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(); 
}); 

量角器是asynchron。您的使用回调

next() 

部分

browser.driver.findElement(by.id('userName')) 

后直接执行,从而在先前声明的异步部分有时是速度不够快,有时晚。

sendKeys(userName); 

这导致功能步骤变为绿色,如果量角器事情做得正确,则无法控制。

你也不使用柴期望。

建议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”函数使用承诺链作为出口点的结果,替换下一个()回调离开这一步。

建议1.2:使用柴氏预计(也asyncronous与chaiAsPromised)

this.Then(/^go to some url$/, function() { 
    var targetUrl = "http://example.com"; 
    browser.get(targetUrl); 
    expect(browser.getCurrentUrl()).to.eventually.equal(targetUrl); 
}); 

关于2), 你的回应必须包含在体内的NG-app标记,看到Uncaught ReferenceError: angular is not defined - AngularJS not working