【发布时间】:2014-05-22 16:41:36
【问题描述】:
我目前正在我的 AngularJS 应用程序和 Grunt 中使用 E2E 测试量角器。 我跟着https://www.npmjs.org/package/grunt-protractor-runner的安装,我有2个问题:
第一:
我想知道,如果可能的话,我的 Karma 单元测试中会有这种行为:
- 启动测试服务器
- 运行所有测试
- 当测试文件或任何其他 html/js 文件更改时,重新运行所有测试
我非常喜欢这种实时观看和自动重新加载的方式,因为它减少了整个套件的启动时间,并使一切变得更快。
但是对于 Protractor,我有一个问题:每次我用 Grunt 开始测试时,测试都会执行,然后一切都关闭了。虽然,我在Gruntfile.js 的量角器配置部分将keepAlive 选项设置为true。有什么想法吗?
第二个:
我无法让它工作,将 Protractor 连接到我的 Angular 应用程序。我是否必须启动 grunt 服务器来提供文件?当我尝试访问我的测试中的某个页面时,例如browser.get('app/index.html'); 或browser.get('#/');,我无法访问该页面。我玩过protractor.conf.js 中的baseUrl 选项(如baseUrl: 'http://localhost:' + (process.env.HTTP_PORT || '8000'))。但在我看来,我必须先启动 grunt 服务器,对吗?我该怎么做?或者还有其他选项可以访问我的 Angular 应用程序吗?我正在使用 sass 和指南针,所以我想,我需要 compass:dist 以某种方式。
更新:
我找到了解决方法。我注册了以下 grunt 任务:
grunt.registerTask('e2e', [
'clean:server',
'concurrent:test',
'autoprefixer',
'connect:test',
'concurrent:server',
'autoprefixer',
'protractor',
'watch'
]);
当以grunt e2e 启动时,我的服务器将根据protractor.conf.js 中的baseUrl: 'http://10.0.0.200:9001' 启动,并且我的测试正在运行。测试完成后,watch 任务会监视更改的文件,并在需要时重新运行量角器。
这样做的缺点是,每次watch 启动protractor 任务时,都会产生一个新的 Chrome 实例,并在测试后终止。 有没有办法防止量角器杀死 Chrome 实例?
此外,我想知道为什么在每个教程中总是说“只需运行grunt protractor 并执行您的测试......”。 为什么我需要一个额外的任务来启动我的 grunt 服务器来访问我的 angularjs 应用程序?
这是我的配置文件:
protractor.conf.js
exports.config = {
capabilities: {
'browserName': 'chrome'
},
chromeOnly: true,
specs: ['test/e2e/**/*.js'],
jasmineNodeOpts: {
showColors: true
},
framework: 'jasmine'
};
Gruntfile.js
module.exports = function(grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
grunt.loadNpmTasks('grunt-protractor-runner');
grunt.initConfig({
// ...
protractor: {
options: {
configFile: "protractor.conf.js",
keepAlive: false,
noColor: false
},
run: {}
},
// ...
});
// ...
grunt.registerTask('test:e2e', [
'clean:server',
'compass:dist',
'compass:server',
'autoprefixer',
'connect:test',
'protractor'
]);
// ...
}
谢谢!
【问题讨论】:
-
为什么要投反对票?请稍微解释一下
-
另一个反对票,你能解释一下我的问题有什么问题以帮助改进它吗?
标签: javascript angularjs gruntjs protractor