javascript - 保存本地存储是不稳定的 - 赛普拉斯
问题描述
我看到了一些关于本地存储的其他帖子,但它们都与令牌和登录有关。
我们有一个 iframe,它会在第一次访问时从我们网站的右侧创建并弹出,我试图阻止这个 iframe 永远打开。开发人员为我设置了一个标识符,告诉我这是我的赛普拉斯测试并且不触发 iframe,但它很不稳定。
我正在使用插件https://www.npmjs.com/package/cypress-localstorage-commands来处理我的本地存储。
这是在我的命令文件中:
import "cypress-localstorage-commands";
在我的测试中,我有以下内容:
beforeEach(() => {
cy.restoreLocalStorage();
cy.setLocalStorage('is_cypress_test', 'true');
})
afterEach(() => {
cy.saveLocalStorage();
})
但是,这经常会失败并且 iframe 会打开。当它工作时,它还会向控制台打印出检测到赛普拉斯的信息(这是在我们的网站代码中添加的内容,以验证它是否正常工作)。
这是我的测试的基本外观。
/// <reference types="Cypress" />
describe(`it browses to xxxx`, () => {
// sets up service cookie to preserve session
Cypress.Cookies.defaults({
preserve: 'foo',
});
beforeEach(() => {
cy.setLocalStorage('is_cypress_test', 'true');
cy.restoreLocalStorage();
})
afterEach(() => {
cy.saveLocalStorage();
})
it(`should log in via a POST, and browse xxx`, () => {
cy.serviceLoginByCSRF(Cypress.env('user_name'), Cypress.env('password'));
cy.visit('/#/asitepage');
});
describe(`it checks all xxxxx`, () => {
it(`should verify xxxxxx`, () => {
cy.get('h3').should('be.visible').invoke('text').then(data => {
let regex = /\n|\*|Back/g;
cy.textCleanup(data, regex).should('eq', 'bar');
});
});
});
describe(`it checks all yyyy`, () => {
it(`should verify yyyy`, () => {
cy.get('h3').should('be.visible').invoke('text').then(data => {
let regex = /\n|\*|Back/g;
cy.textCleanup(data, regex).should('eq', 'foo');
});
});
});
});
投影仪代码
<!-- Beamer for product updates -->
<script>
var beamer_config = {
product_id: "foobar",
selector: "#beamer",
user_email: 'example@test.blogspot.gov',
user_firstname: 'Hank',
user_lastname: 'Williams',
filter: 'production',
onopen: function(){
// localStorage.setItem("is_cypress_test", "true") -- is test
if(localStorage.getItem("is_cypress_test")){
console.log("Skipping beamer load for Cypress");
return false;
}
}
};
</script>
<script type="text/javascript" src="https://asite.js" defer="defer"></script>
<!-- // Beamer for product updates -->
我想知道我是否以错误的方式或错误的区域设置了这个?
任何帮助,或关于如何最好地使用它的注释,因此在每次测试之前它总是会在 localStorage 中保存,将不胜感激。
想法?
谢谢
解决方案
有同样的问题,为我解决的问题是将以下代码添加到commands.js
:
Cypress.LocalStorage.clear = function (keys, ls, rs) {
return;
}
推荐阅读
- sql-server - MS SQL -- 从第三个表添加另一个限制
- vue.js - v-bind:class 不使用数组变量
- sql-server - 如何在将数据添加到表中时动态插入日期?
- github - 什么是 GraphQL 查询以确定组织中是否存在 GitHub 包?
- servicestack - 是否可以在 Visual Studio 2017 或 2019 中使用 ServiceStack 模板?
- python - 改进 tensorflow 训练模型
- python - 从大流中选择一个随机元素有什么大不了的?
- javascript - 将 Json 数据映射到新的键值对 JavaScript 对象
- c++ - 如何使用 string::replace 方法写入文件?
- python - Match all identifiers in a string