如何设置茉莉为业力e2e测试角应用程序?

Mar*_*kLi 1 testing jasmine angularjs yeoman karma-runner

我尝试使用业力和茉莉与自耕农创建e2e测试.在我的karma-e2e.conf.js添加茉莉花:

files = [
   JASMINE,
   JASMINE_ADAPTER,
   ANGULAR_SCENARIO,
   ANGULAR_SCENARIO_ADAPTER,
   'test/e2e/**/*.js'
];
Run Code Online (Sandbox Code Playgroud)

一个需要异步测试,所以我需要使用runs,waits,waitsFor(https://github.com/pivotal/jasmine/wiki/Asynchronous-specs)

但如果我尝试使用它:

it('test', function () {
    runs(function () {
        ...
    });
});
Run Code Online (Sandbox Code Playgroud)

Scenatio test runner返回:

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)
Run Code Online (Sandbox Code Playgroud)

我不知道问题出在哪里.你能帮我吗?

Phi*_*ley 8

使用Karma的Angular e2e测试不能也不能使用JASMINE适配器.相反,你有ANGULAR_SCENARIO_ADAPTER与编写Jasmine测试类似的感觉.

无论如何,适配器API中的所有命令都是异步的.例如element('#nav-items').count(),不返回数字,它返回一个Future对象.Future对象被放置在队列中并随着跑步者的进展而异步执行.引用API文档:

期待(未来){}匹配:

[...]所有API语句都返回一个future对象,该对象在执行后获得一个值.

如果您需要运行自己的异步测试代码,可以扩展适配器的DSL,这比听起来更容易.这个想法是你返回自己的Future,可以由匹配器评估toBe().在e2e-tests.js中有一些关于如何做到这一点的例子来自Vojta.只需记住done(null, myRetrunValue);在测试代​​码成功时调用(myRetrunValue是匹配器评估的值).或者,done('Your own error message');如果您希望测试失败.

更新:回答以下问题.为了模拟登录,首先添加一个调用的函数logindsl:

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();
    })
  };
});
Run Code Online (Sandbox Code Playgroud)

然后打电话:

beforeEach( function()
{
    expect( login('button.login') ).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)