有没有办法获取令牌并将其设置为本地存储? [英] is there way to get and set token to local storage?

查看:18
本文介绍了有没有办法获取令牌并将其设置为本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器在 http:localhost/上编写 e2e 测试,因为我们只开发前端应用程序.我必须登录客户端集成并从本地存储手动复制令牌值并将其传递给我创建的自定义登录方法.是否可以自动执行此登录过程?

I'm writing e2e tests on http:localhost/ using protractor since we are only developing front-end app. I have to login into clients integration and manually copy token value from local storage and pass it to custom login method witch I created. Is there away to automate this login process?

任何建议都会很棒

到目前为止,我已经尝试过 window.localStorage.getItem() 和 window.localStorage.setItem()

So far I have tried window.localStorage.getItem() and window.localStorage.setItem()

public static Login(): void {
    browser.get('login url');
    browser.driver.findElement(by.id('username')).sendKeys('user');
    browser.driver.findElement(by.id('password')).sendKeys('pass');
    browser.driver.findElement(by.id('submit')).click();
    browser.waitForAngular();
    const token: string = window.localStorage.getItem('AuthenticationToken');
    browser.get('error page');
    window.localStorage.setItem('AuthenticationToken', token);
    browser.get('home page');
}

我得到这个错误:

  • 失败:未定义窗口
  • 失败:等待 Protractor 与页面同步时出错:angularJS 可测试性和 Angular 可测试性都未定义.这可能是因为这是一个非 Angular 页面,也可能是因为您的测试涉及客户端导航,这可能干扰 Protractor 的引导.

推荐答案

根据@Abhishek 可以使用localStorage.setItem('Auth_token', data)localStorage.getItem(keyName)

According to @Abhishek items of a local storage can be set and gotten using localStorage.setItem('Auth_token', data) and localStorage.getItem(keyName)

但是,据我了解,这只能从浏览器的控制台完成,而不能从量角器脚本完成.因此,如果您想在量角器脚本中完成相同的结果,您可以执行以下操作:

However, from my understanding this can be done only from the browser's console, not from protractor script. Thus, if you want to accomplish the same results in your protractor script you could do the following:

browser.executeScript(`return window.localStorage.getItem(keyName);`);

或者如果您打算大量使用本地存储,请实现一个新的 local-storage.js 模块,如下所示:

Or if you intend to use local storage a lot, implement a new local-storage.js module as follow:

function LocalStorage () {

    this.get = async function () {
        let storageString = await browser.executeScript("return JSON.stringify(window.localStorage);");
        return JSON.parse(storageString);
    };

    this.clear = async function () {

        return browser.executeScript("return window.localStorage.clear();");
    };

    this.getValue = async function (key) {

        return browser.executeScript(`return window.localStorage.getItem('${key}');`);
    };

    this.setValue = async function (key, value) {
        return browser.executeScript(`window.localStorage.setItem('${key}', '"${value}"');`);
    };
}

module.exports = new LocalStorage();

然后在您的规范中,执行以下操作:

And then in your spec, do the following:

localStorage = require("./local-storage.js");

describe(`Suite: Home page`, () => {

    fit("Login to the application", async () => {

        console.log(await localStorage.getValue("calltrk-calltrk_referrer"));
        await localStorage.setValue("key", "value");
        console.log(await localStorage.getValue("key"));
        console.log(await localStorage.get());
    });
});

注意我只处理带有 async/await 关键字的 Promise.所以你的语法可以不同,但​​方法应该保持不变

Note I solely handle Promises with async/await keywords. So your syntax can be different, but the approach should remain the same

这篇关于有没有办法获取令牌并将其设置为本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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