通过Grunt将量angular器与Yeoman集成
我想整合量angular器与Yeoman生产的脚手架。 我跟着一个教程,在那里,老的scenario-runner被用来设置e2etesting(通过grunt )。
我想升级我的脚手架,而不是使用量angular器。
有什么想法吗?
-
从
npm安装protractor和grunt-protractor-runner:npm install protractor grunt-protractor-runner --save-dev -
为量angular器(
protractor.conf.js)创buildconfiguration文件,将specs和baseUrl更改为testing文件和testing服务器:exports.config = { seleniumAddress: 'http://localhost:4444/wd/hub', specs: ['test/e2e/*_test.js'], baseUrl: 'http://localhost:9001' //default test port with Yeoman } -
更新你的
Gruntfile.js,在karma任务之后添加以下内容:protractor: { options: { keepAlive: true, configFile: "protractor.conf.js" }, run: {} } -
添加testing中的量angular器任务
grunt.registerTask('test', [ 'clean:server', 'concurrent:test', 'autoprefixer', 'connect:test', 'karma', 'protractor:run' ]); -
下载并启动selenium服务器:
node_modules/protractor/bin/webdriver-manager update node_modules/protractor/bin/webdriver-manager start(在Windows 🙂
node node_modules/protractor/bin/webdriver-manager update node node_modules/protractor/bin/webdriver-manager start -
更新你的
package.json,在"devDependencies"之后添加以下内容。 这将在npm install后运行命令,所以你不需要记住每一次。"scripts": { "install": "node node_modules/protractor/bin/webdriver-manager update" } -
使用grunt运行testing
grunt test
如果你想量angular器为你启动服务器,删除
seleniumAddress: 'http://localhost:4444/wd/hub',
从protractor.conf.js ,然后运行grunt test将在testing期间启动一个独立的selenium实例,并在运行testing套件后退出。
有一件事要补充现有的答案; 如果您想自动启动Selenium服务器,则还必须指定seleniumServerJar和chromeDriver(如果使用Chrome)的位置,否则在您手动启动Selenium服务器之前,testing将不起作用(请确保运行“webdriver-pipe理器更新“首先从命令行):
protractor: { options: { keepAlive: false, configFile: "test/config/protractor.conf.js", noColor: true, // If true, protractor will not use colors in its output. args: { seleniumServerJar: 'node_modules/protractor/selenium/selenium-server-standalone-2.39.0.jar', chromeDriver: 'node_modules/protractor/selenium/chromedriver.exe' } }, run: { } },
正如@ user2172816在他们的答案中提到的 – 从量angular器configuration中删除seleniumAddress: 'http://localhost:4444/wd/hub'通常会导致量angular器为您启动一个Selenium实例。
作为替代,你可以使用grunt-protractor-webdriver启动Selenium:
1)安装并保存grunt-protractor-webdriver
npm install grunt-protractor-webdriver --save-dev
2)将以下内容添加到您的Grunt定义函数中:
grunt.loadNpmTasks('grunt-protractor-webdriver');
3)添加下面的量angular器webdriver任务:
protractor_webdriver: { start: { options: { path: 'node_modules/protractor/bin/', command: 'webdriver-manager start' } } }
4)在运行量angular器之前,将protractor_webdriver添加到test任务中
grunt.registerTask('test', [ 'clean:server', 'concurrent:test', 'autoprefixer', 'connect:test', 'karma', 'protractor_webdriver', 'protractor:run' ]);