2013-04-11 121 views
1

我正在嘗試爲AngularJS TODO MVC應用程序編寫單元測試,而且我有點卡住學習e2e測試語法。AngularJS e2e用ENTER鍵測試

到目前爲止,這是我有:

describe('todomvc', function() { 

    beforeEach(function() { 
     browser().navigateTo('../app/index.html'); 
    }); 

    afterEach(function() { 
     localStorage.clear(); 
    }); 

    describe('localstorage behavior', function() { 
     it('should load with zero items in localstorage', function() { 
      expect(repeater('#todo-list li').count()).toEqual(0); 
      input('newTodo').enter('Foo Bar'); 
      expect(repeater('#todo-list li').count()).toEqual(1); 
     }); 
    }); 

}); 

而且我的配置:

basePath = '../'; 

files = [ 
    ANGULAR_SCENARIO, 
    ANGULAR_SCENARIO_ADAPTER, 
    'test/e2e/**/*.js' 
]; 

autoWatch = false; 

browsers = ['Chrome']; 

//singleRun = true; 

proxies = { 
    '/': 'http://localhost:8000/' 
}; 

junitReporter = { 
    outputFile: 'test_out/e2e.xml', 
    suite: 'e2e' 
}; 

總之,我需要一種方法來模擬「ENTER」鍵,因爲這是這個TODO MVC如何應用程序將項目添加到列表中。我怎樣才能做到這一點?

回答

1

你可以一個類屬性添加到您的格式如

<form class="myForm"> 

,並添加這一步你的測試文件

element(':form.myForm').submit(); 

我想,這可能會奏效。

0

你可以用一點jQuery來觸發「回車」鍵。請記得在你的配置文件中鏈接一個jQuery庫以使其正常工作。

describe('localstorage behavior', function() { 
    it('should load with zero items in localstorage', function() { 
     var e = jQuery.Event("keydown", { 
      keyCode: 13 
     }); 

     expect(repeater('#todo-list li').count()).toEqual(0); 
     input('newTodo').enter('Foo Bar'); 

     element = angular.element('<input name="mytext" type="text" ng-model="mytext">'); 
     element = compile(element)(scope); 

     element.trigger(e); 
     scope.$digest(); 

     expect(repeater('#todo-list li').count()).toEqual(1); 
    }); 
});