我尝试使用karma和jasmine创建e2e测试。在我的karma-e2e.conf.js中,我添加了jasmine:
files = [
JASMINE,
JASMINE_ADAPTER,
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'test/e2e/**/*.js'
];A需要异步测试,所以我需要使用runs、waits、waitsFor (https://github.com/pivotal/jasmine/wiki/Asynchronous-specs)
但如果我试着使用它:
it('test', function () {
runs(function () {
...
});
});Scenatio测试运行程序返回以下内容:
TypeError: Cannot call method 'runs' of null
at runs (http://localhost:8080/adapter/lib/jasmine.js:562:32)
at Object.<anonymous> (http://localhost:8080/base/test/e2e/eduUser.js:42:3)
at Object.angular.scenario.SpecRunner.run (http://localhost:8080/adapter/lib/angular-scenario.js:27057:15)
at Object.run (http://localhost:8080/adapter/lib/angular-scenario.js:10169:18)我不知道问题出在哪里。你能帮帮我吗?
发布于 2013-06-03 20:04:32
Karma的Angular e2e测试不能也不能使用JASMINE适配器。取而代之的是ANGULAR_SCENARIO_ADAPTER,它的感觉类似于编写Jasmine测试。
适配器的API中的所有命令都是异步的。例如,element('#nav-items').count()不返回数字,它返回一个Future对象。Future对象被放在一个队列中,并随着运行进程的进行而异步执行。引用API docs
expect(future).{matcher}:
..。所有API语句都会返回一个future对象,该对象在执行后会获得一个赋值。
如果您需要运行自己的异步测试代码,您可以扩展适配器的DSL,这比听起来更简单。这个想法是你返回你自己的Future,它可以由像toBe()这样的匹配器计算。在the e2e-tests.js Gist from Vojta中有一些关于如何做到这一点的例子。只需记住在测试代码成功时调用done(null, myRetrunValue); (myRetrunValue是匹配器计算的值)。如果希望测试失败,则返回done('Your own error message');。
更新:回答下面的问题。要模拟登录,首先向dsl添加一个名为login的函数
angular.scenario.dsl('login', function() {
return function(selector) {
// @param {DOMWindow} appWindow The window object of the iframe (the application)
// @param {jQuery} $document jQuery wrapped document of the application
// @param {function(error, value)} done Callback that should be called when done
// (will basically call the next item in the queuue)
return this.addFutureAction('Logging in', function(appWindow, $document, done) {
// You can do normal jQuery/jqLite stuff here on $document, just call done() when your asynchronous tasks have completed
// Create some kind of listener to handle when your login is complete
$document.one('loginComplete', function(e){
done(null, true);
}).one('loginError', function(e){
done('Login error', false);
});
// Simulate the button click
var loginButton = $document.find(selector || 'button.login');
loginButton.click();
})
};
});然后打电话给:
beforeEach( function()
{
expect( login('button.login') ).toBeTruthy();
});https://stackoverflow.com/questions/16892697
复制相似问题