6

I'm trying to write a simple e2e test for the authentication we use in our project, Authentication is based on a json web token which is set into window.localStorage.satellizer_token .

To set it i use the code below, but for what i see it doesn't really set the real localStorage property of the window object.

describe('login', function () {
it('should set the satellizer token and be allowed to get panel', function () {
    browser.driver.get('http://example.com/');
    browser.driver.executeScript(function () {
        return window.localStorage;
    }).then(function (localStorage) {
        expect(localStorage.satellizer_token).toBe(undefined);
        localStorage.satellizer_token = "eyJ0fdaccKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjE3MjUzIiwiaWF0IjoxNDM0Mzc1NjU3LCJleHAiOjE0NjU5Mjk2NTd9.VbhdWQ_gOb7X8pmOGLDjBKURxcaWQlIXQGvLRansQCphk";
        expect(localStorage.satellizer_token).toBe("eyJ0fdaccKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjE3MjUzIiwiaWF0IjoxNDM0Mzc1NjU3LCJleHAiOjE0NjU5Mjk2NTd9.VbhdWQ_gOb7X8pmOGLDjBKURxcaWQlIXQGvLRansQCphk");
        browser.driver.get('http://example.com/panel');
        expect(browser.driver.getTitle()).toEqual('http://example.com/panel');
        expect(browser.driver.getCurrentUrl()).toEqual('http://example.com/panel');
    });
});

});

I know there is already something similar here and here but all the examples i can find are about access-only, i need also to modify window properties.

What is the correct way to interact with the window object in protractor tests?

Community
  • 1
  • 1
pietrovismara
  • 6,102
  • 5
  • 33
  • 45

2 Answers2

5

Working solution:

    browser.executeScript(function () {
        window.localStorage.satellizer_token = "eyJ0eXAiOiJKV1QiLCJhbGasdsOiJIUzI1NiJ9.eyJpZCI6IjE3MjUzIiwiaWF0IjoxNDM0Mzc1NjU3LCJleHAiOjE0NjU5Mjk2NTd9.VbhdWQ_gOb7X8pmOGLDjBKURQUQlcAfGSGvLRansQCphk";
    });
pietrovismara
  • 6,102
  • 5
  • 33
  • 45
0
browser.driver.manage().window()

Seen here: https://github.com/bolshchikov-public/protractor-best-practices/blob/master/Practices.md#set-screen-size

David Pelayo
  • 158
  • 10
  • For what it looks the window method has only a set of predefined methods to interact with the window object, but it doesn't allow me to set new properties. – pietrovismara Jun 15 '15 at 21:02
  • 1
    Exact. Because what it returns is an interface, defined as follows: http://angular.github.io/protractor/#/api?view=webdriver.WebDriver.Window. If you want to manage the window object in a custom way I would say it's okay how you are doing right now. I would recommend you just to declare the callback anonymous function outside the execute script, to be able to reuse it better, like: `var localStorageSetter = function () { window.localStorage.satellizer_token = 'whatever'; } browser.executeScript(localStorageSetter); ` – David Pelayo Jun 16 '15 at 06:44