Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver + Protractor with grunt

I'm trying to run end-to-end tests with WebDriver and Protractor. No problem when I run it manually with: webdriver-manager start and then protractor test-UI/e2e/conf.js

Now I would like to launch them from a grunt command, so I tried with grunt-shell, joining them with '&&'. But as WebDriver waits, tests are never started. Did someone try this before?

Thanks.

like image 806
ZorGleH Avatar asked Dec 26 '22 12:12

ZorGleH


1 Answers

There is a fork of Grunt-shell called Grunt-shell-spawn (Github Repo) which allows you to run background processes asynchronously. This happens to work very well with starting the selenium webdriver server helping to automate the protractor testing process. There are a few grunt plugins specifically for starting the webdriver server but from my experience they all have small bugs that cause errors once the tests are finished or require you to mark a flag keepAlive: true which means it will not kill the webdriver server process forcing you to ctrl+c or close and re-open the command prompt which can cause a lot of issues when devs are using the functional tests and with continuous integration (CI) servers. Grunt-shell-spawn has the ability to kill the process as you can see at the end of my 'test' task which is really invaluable for maintaining consistency and ease of use.

'use strict';

module.exports = function(grunt) {

grunt.loadNpmTasks('grunt-shell-spawn');
grunt.loadNpmTasks('grunt-protractor-runner');

var path = require('path'); 

grunt.initConfig({
  ...
  ...
  ...
  shell: {
    updateserver: {
      options: {
        stdout: true
      },
      command: "node " + path.resolve('node_modules/protractor/bin/webdriver-manager') + ' update --standalone --chrome'
    },
     startserver: {
      options: {
        stdout:false,
        stdin: false,
        stderr: false,
        async:true
      },
      command:  'node ' + path.resolve('node_modules/protractor/bin/webdriver-manager') + ' start --standalone'
    },
});

grunt.registerTask('test',[
    'shell:updateserver',
    'shell:startserver',
    'protractor:e2e',
    'shell:startserver:kill'
]);
like image 93
sonhu Avatar answered Dec 28 '22 12:12

sonhu