在检索dataStore.js对象时,js文件中抛出了未定义
我已经创建了一个datastore.js
对象,并尝试将下面的字符串传递给对象如下。 在home.js中的console.log时变得不确定,请有人在这里建议您的问题。
let dataStore = {
globaldata1: {},
globaldata2: {}
}
export { dataStore }
// profile.js
let datastore = require("../global/dataStore");
let myStr1 = "This is a string" ;
let myStr2 = "This is another string" ;
datastore.globaldata1 = myStr1;
datastore.globaldata2 = myStr2;
// home.js
let datastore = require("../global/dataStore");
let globalStr1 = datastore.globaldata1 ;
console.log("Print the data::"+globalStr1);
let globalStr2 = datastore.globaldata2 ;
console.log("Print the second data::"+globalStr2);
在我的柏树测试中应用了相同的概念:
柏树版本: v10.2.0
//datastore.js
let dataStore = {
globaldata: {}
}
export { dataStore }
//site.cy.cy。 js file
let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";
describe('Verify the Global data saving', () => {
context('Global data', () => {
beforeEach(() => {
cy.restoreLocalStorage();
cy.visit('/');
});
it('First get the text and Save globally', () => {
cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
dataStore.globaldata = text;
console.log("First test outcome:: " +dataStore.globaldata); // all good here
});
});
it('Use that text in second test', () => {
console.log("Yeh, global text in second test :: " +dataStore.globaldata ); // all good here
});
});
});
//another.cy.js文件
let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";
describe('Retrive another test file', () => {
context('Retrive saved data in another test file', () => {
it('Get the text', () => {
console.log("Yeh, another test file:: " +dataStore.globaldata);
// here I am getting the text as undefined ??
});
});
});
I have created a dataStore.js
object and tried to pass below string to object as below.
Getting undefined while console.log in Home.js, could someone please advise the issue here.
let dataStore = {
globaldata1: {},
globaldata2: {}
}
export { dataStore }
// profile.js
let datastore = require("../global/dataStore");
let myStr1 = "This is a string" ;
let myStr2 = "This is another string" ;
datastore.globaldata1 = myStr1;
datastore.globaldata2 = myStr2;
// home.js
let datastore = require("../global/dataStore");
let globalStr1 = datastore.globaldata1 ;
console.log("Print the data::"+globalStr1);
let globalStr2 = datastore.globaldata2 ;
console.log("Print the second data::"+globalStr2);
Applied the same concept in my Cypress tests:
Cypress version : v10.2.0
//dataStore.js
let dataStore = {
globaldata: {}
}
export { dataStore }
//site.Cy.js file
let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";
describe('Verify the Global data saving', () => {
context('Global data', () => {
beforeEach(() => {
cy.restoreLocalStorage();
cy.visit('/');
});
it('First get the text and Save globally', () => {
cy.get('#companyNameDiv > span').invoke('text').then((text)=>{
dataStore.globaldata = text;
console.log("First test outcome:: " +dataStore.globaldata); // all good here
});
});
it('Use that text in second test', () => {
console.log("Yeh, global text in second test :: " +dataStore.globaldata ); // all good here
});
});
});
//another.Cy.js file
let dataStore = require("../../../support/dataStore");
import "cypress-localstorage-commands";
describe('Retrive another test file', () => {
context('Retrive saved data in another test file', () => {
it('Get the text', () => {
console.log("Yeh, another test file:: " +dataStore.globaldata);
// here I am getting the text as undefined ??
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查柏树测试,并具有一些轻微的文件夹路径和不同的baseurl的mod。
由于赛普拉斯V10不再具有“运行所有”选项,因此我还添加了一个称为 all.cy.js 的枪管规格,该规格允许运行多个规格,并且 也可以执行规格的顺序(这对此很重要)。
该测试在
cypress Open
中均可正常工作,然后选择all.cy.js
,以及cypress run-spec cypress/e2e/e2e/all.cy.js
。all.cy.js
site.cy.js
entere.cy.js
Checking out the Cypress tests, with some slight mods for folder paths and different baseUrl.
Since Cypress v10 no longer has "Run all" option I also added a barrel spec called all.cy.js which allows running multiple specs and also enforces the order of the spec (which would be important for this to work).
The test worked ok in
cypress open
then selectingall.cy.js
and alsocypress run --spec cypress/e2e/all.cy.js
.all.cy.js
site.cy.js
another.cy.js
这里的问题似乎是您没有适当地利用您的进口。
导出是:
s大写的位置,但在您的家中。
您从dataStore.js的
It would seem the issue here is that you didn't properly capitalize your import.
Your export from dataStore.js is:
Where the S is capitalized, but in your Home.js:
It doesn't have a capitalized S which would make the import undefined because dataStore.js doesn't export datastore, it exports dataStore