0

I am trying to test a JavaScript function that uses HTML5 sessionStorage on jasmine-maven-plugin. Jasmine uses HtmlUnit to emulate a web browser.

The problem is that when the automated tests are running during maven build, it says:

  • Error: Expected a spy, but got Function.

I have tried this How to deal with sessionStorage locally in FF (for testing) and then I've got this error:

  • TypeError: Expected argument of type object, but instead had type object in file: ...

But if I try to run Jasmine on a web page importing jasmine.js, the test works perfectly. I have tried this too Can I access HTML5 storages using HTMLUnit , but without success.

An example of code that works on http://tryjasmine.com/ and not in jasmine-maven-plugin:

function alertItem(id) {
console.log("start");
var x = sessionStorage.getItem(id);
alert(x);
}

describe("sessionStorage test", function () {    
console = {
    log : function() {},
    error : function() {},
    warn : function() {}
};

var mockup = function() {
    var table = {};
    return {
        getItem: function(key) {
            return table[key];
        },
        setItem: function(key, value) {
            table[key] = value.toString();
        },
        removeItem: function(key) {
            table.pop();
        },      
        clear: function() {
            table = {};
        }
    };
}();

Object.defineProperty(window, 'sessionStorage', {
    value: mockup
});


it("must work", function () {
    console.log("testing...");
    spyOn(sessionStorage, 'getItem').andReturn("my value");
    alertItem("id");
    expect(sessionStorage.getItem).toHaveBeenCalled();
});
});

Does anyone have an idea?

Thanks.

Community
  • 1
  • 1

2 Answers2

2

HTMLUnit does not support many HTML5 features - while your browser does, this is why it will work in the browser but not as part of the build with HTMLUnit.

Mike
  • 51
  • 5
  • Thanks @Mike for the answer. Do you know any maven plugin that runs jasmine without HTMLUnit? Or are HTMLUnit team working to support HTML5? – Vinícius Fonseca Oct 29 '12 at 18:37
  • I have submitted a bug on HTMLUnit sourceforge but they seems not to be working on it, because it includes jasmine maven: http://sourceforge.net/p/htmlunit/bugs/1413/ – Vinícius Fonseca Oct 29 '12 at 18:43
  • 1
    Support for using the jasmine-maven-plugin with PhantomJS instead of HtmlUnit was recently added. There is an example configuration here: http://searls.github.io/jasmine-maven-plugin/phantomjs.html – Kyle Jun 06 '13 at 15:16
0

As @Kyle suggested, using sessionStorage on jasmine-maven-plugin with PhantomJS instead of HtmlUnit worked!

More information at: http://searls.github.io/jasmine-maven-plugin/phantomjs.html

Thank you!