Search code examples
angularjstestingprotractorvcr

How to get tests to record fixtures with AngularJs like vcr in Rails?


I've been looking everywhere, I tried node-replay but with protractor but it won't work with selenium.

I've also tried vcr.js and sepia.

How do I go about setting up my tests for that they make the initial calls but store them as cassettes like vcr.

Cheers.


Solution

  • I've been setting up sepia to be used with protractor. It works now, here is what I did:

    I assume you've already set up grunt-connect to run your protractor tests.

    Then you'll need to wait for the event listening event from the connect configuration: grunt.event.once('connect.test.listening', function)

    And that's where you will configure sepia.

      grunt.event.once('connect.test.listening', function(host, port) {
        /**
         * Configure sepia here
         */
    
        var sepia = require('sepia').withSepiaServer();
    
        // Use your custom configuration
        sepia.configure({
          verbose: true,
          debug: true,
          includeHeaderNames: false,
          includeCookieNames: false
        });
    
        // I have some path/body content to filter configured in the vrc configuration
        var bodyFilters = grunt.config('vcr.filters.body') || [];
        var pathFilters = grunt.config('vcr.filters.path') || [];
        var regexPath = function(string) {
          var escapedString = string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
          return new RegExp(escapedString);
        };
    
        // Filter path
        _.map(pathFilters, function(filter) {
          sepia.filter({
            url: regexPath(filter.path),
            urlFilter: function(url) {
              return url.replace(filter.pattern, filter.replacement);
            }
          });
        });
    
        // Filter body content
        _.map(bodyFilters, function(filter) {
          sepia.filter({
            url: regexPath(filter.path),
            bodyFilter: function(body) {
              return body.replace(filter.pattern, filter.replacement);
            }
          });
        });
      });
    });