量角器测试:browser.get清除sessionStorage

时间:2016-01-21 11:56:44

标签: protractor

我的量角器测试有一个非常奇怪的问题。我试图直接在sessionStorage上设置正确的凭据,但身份验证服务并没有接受它。我已将其与以下脚本隔离:

describe('The search', () => {
    beforeAll(() => {
        browser.executeScript(() => {
            window.sessionStorage.setItem('something', JSON.stringify({"test":"test"}));
        });
    });

    it('works when changing URL', () => {
        var result1 = browser.executeScript('return window.sessionStorage.getItem(\'something\')');
        browser.get('/');
        var result2 = browser.executeScript('return window.sessionStorage.getItem(\'something\')');
        expect(result1).toBe(result2);
    });
});

输出:

1) The search works when changing URL
  Message:
    Expected '{"test":"test"}' to be null.
  Stack:
    Error: Failed expectation
        at Object.<anonymous> (/source/search.spec.ts:12:25)
        at process._tickCallback (node.js:368:9)

正如您所看到的,只要调用browser.get(),就会清除sessionStorage。有没有人知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

导航到URL并然后设置本地存储项目对我有用:

describe('The search', function () {
    var value = JSON.stringify({"test":"test"});

    beforeAll(function () {
        browser.get('/');
        browser.executeScript(function (value) {
            window.sessionStorage['something'] = value;
        }, value);
    });

    it('works when changing URL', function () {
        var result = browser.executeScript('return window.sessionStorage.getItem(\'something\');');
        expect(value).toEqual(result);
    });
});