量角器测试,访问和修改Window对象的属性 [英] Protractor testing, access and modify Window object properties

查看:222
本文介绍了量角器测试,访问和修改Window对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写我们在项目中使用的身份验证一个简单的端到端测试,验证基于JSON网络令牌被设置成window.localStorage.satellizer_token上。

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 .

要设置它,我用下面的code,但我所看到的它并没有真正设置窗口对象的实际localStorage的财产。

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');
    });
});

});

我知道已经有类似的东西<一个href=\"http://stackoverflow.com/questions/26666043/can-i-access-the-document-or-window-objects-from-within-a-protractor-test\">here和<一个href=\"http://stackoverflow.com/questions/26102132/access-window-object-browser-scope-from-protractor\">here但所有的例子我能找到的关于访问而已,我也需要修改窗口的属性。

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.

什么是用量角器测试窗口对象?

推荐答案

工作方案:

    browser.executeScript(function () {
        window.localStorage.satellizer_token = "eyJ0eXAiOiJKV1QiLCJhbGasdsOiJIUzI1NiJ9.eyJpZCI6IjE3MjUzIiwiaWF0IjoxNDM0Mzc1NjU3LCJleHAiOjE0NjU5Mjk2NTd9.VbhdWQ_gOb7X8pmOGLDjBKURQUQlcAfGSGvLRansQCphk";
    });

这篇关于量角器测试,访问和修改Window对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆