简体   繁体   中英

Does HtmlUnit and jasmine-maven-plugin support HTML5 sessionStorage?

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.

HTMLUnit不支持许多HTML5功能 - 当您的浏览器支持时,这就是它在浏览器中工作的原因,而不是HTMLUnit构建的一部分。

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM