简体   繁体   English

HtmlUnit和jasmine-maven-plugin是否支持HTML5 sessionStorage?

[英]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-maven-plugin上使用HTML5 sessionStorage的JavaScript函数。 Jasmine uses HtmlUnit to emulate a web browser. Jasmine使用HtmlUnit来模拟Web浏览器。

The problem is that when the automated tests are running during maven build, it says: 问题是当自动化测试在maven构建期间运行时,它会说:

  • 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: 我试过这个如何在FF本地处理sessionStorage(用于测试)然后我有这个错误:

  • TypeError: Expected argument of type object, but instead had type object in file: ... TypeError:类型为object的预期参数,但在文件中有类型对象:...

But if I try to run Jasmine on a web page importing jasmine.js, the test works perfectly. 但是,如果我尝试在导入jasmine.js的网页上运行Jasmine,则测试工作正常。 I have tried this too Can I access HTML5 storages using HTMLUnit , but without success. 我也试过这个我可以使用HTMLUnit访问HTML5存储 ,但没有成功。

An example of code that works on http://tryjasmine.com/ and not in jasmine-maven-plugin: http://tryjasmine.com/上运行但不在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! 正如@Kyle建议的那样,使用带有PhantomJS而不是HtmlUnit的jasmine-maven-plugin上的sessionStorage工作!

More information at: http://searls.github.io/jasmine-maven-plugin/phantomjs.html 更多信息请访问: http//searls.github.io/jasmine-maven-plugin/phantomjs.html

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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