问题描述
我正在使用量角器在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');
}
我收到此错误:
- 失败:未定义窗口
- 失败:等待量角器与页面同步时发生错误:"angularJS可测试性和角度可测试性均未定义.这可能是因为这是非角度页面,或者因为您的测试涉及客户端导航,所以干扰量角器的引导.
推荐答案
可以使用localStorage.setItem('Auth_token', data)
和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
关键字处理Promises.因此您的语法可以不同,但是方法应保持不变
Note I solely handle Promises with async/await
keywords. So your syntax can be different, but the approach should remain the same
这篇关于有没有办法获取令牌并将令牌设置为本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!