Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngulrJS + Grunt + Protractor: keepAlive and watch

I'm currently playing around with E2E tests an Protractor in my AngularJS app and Grunt. I followed the installation from https://www.npmjs.org/package/grunt-protractor-runner, and I have 2 questions:

First:

I was wondering, if it's possible, to have that behaviour from my unit tests with Karma:

  • start the test server
  • run all tests
  • when a test file or any other html/js file changes, re-run all tests

I like that way with live watch and auto reloading very much, because it reduces the startup time from the whole suite, and makes everything faster.

But with Protractor, I have a problem: Everytime I start the test with Grunt, the tests are executed, and then everything shuts down. Although, I set the keepAlive option to true in the protractor configuration part of my Gruntfile.js. Any ideas?

Second:

I can't get it to work, to connect Protractor to my angular app. Do I have to start the grunt server, to serve the files? When I try to visit a page in my tests like browser.get('app/index.html'); or browser.get('#/');, I can't access the page. I've played around with the baseUrl option (like baseUrl: 'http://localhost:' + (process.env.HTTP_PORT || '8000')) from my protractor.conf.js. But to me it looks like, I have to start the grunt server before, am I right? And how can I do this? Or are there any other options for accessing my angular app? I'm using sass with compass, so I think, I need the compass:dist taks in some way.

UPDATE:

I found a workaround. I registered the following grunt task:

  grunt.registerTask('e2e', [
    'clean:server',
    'concurrent:test',
    'autoprefixer',
    'connect:test',
    'concurrent:server',
    'autoprefixer',
    'protractor',
    'watch'
  ]);

When started with grunt e2e my server gets started, based on the baseUrl: 'http://10.0.0.200:9001' inside my protractor.conf.js, and my tests are running. When the tests are completed, the watch task watches for changed files, and reruns protractor if needed.

The drawback of this is, that everytime watch starts the protractor task, a new Chrome instance is spawned, and after the test killed. Is there a way to prevent protractor from killing the Chrome instance?

Furthermore, I was wondering why in every tutorial it always says sth like "Just run grunt protractor and your tests are executed...". Why do I need an additional task for starting up my grunt server to access my angularjs application?

And here are my config files:

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'
  ]);

  // ...
}

thanks!

like image 281
23tux Avatar asked Apr 09 '14 12:04

23tux


2 Answers

I was wondering, if it's possible, to have that behaviour from my unit tests with Karma:

Typically, you wouldn't run E2E tests after every HTML change. The main reason for this is that running E2E tests can take a long time as you add more and more tests. Therefore to answer the question, I don't believe there is any built in way to support running E2E tests after each change in the same browser. I'd recommend using Unit Tests as a mechanism to test after each change and running your E2E tests manually.

Do I have to start the grunt server, to serve the files?

Yes, you need to start a server to run your protractor tests against. You aren't starting a "grunt server", you're actually starting a web server that Selenium will use which is the engine Protractor uses for its tests. This is one of the major differentiating factors (among others) between using Protractor and ng-scenario.

Is there a way to prevent protractor from killing the Chrome instance?

Once again, I don't believe that Protractor was meant to be used like this since you shouldn't need to run your E2E test after each change. To answer the question though, I don't believe so.

Why do I need an additional task for starting up my grunt server to access my angularjs application?

Same answer as above in regards to Selenium.

like image 153
Always Learning Avatar answered Oct 14 '22 14:10

Always Learning


Why do I need an additional task for starting up my grunt server to access my angularjs application?

As a separation of concern Protractor does not manage the spinup and teardown of a web server to run the tests, this is a different approach from Karma . I would imagine this is due to the fact that the tests are run on the server side, where a web browser is "driven" to perform actions from your test code.

Is there a way to prevent protractor from killing the Chrome instance?

Short answer NO.
Long answer, you can use the debug option and set a breakpoint in your code, but that's probably not what you are looking for. The keepAlive option you tried only keeps the browser alive if a test fails, it doesn't keep the browser up once the tests are over.

This question might be of interest to you.

like image 26
Renaud Avatar answered Oct 14 '22 13:10

Renaud