赛普拉斯 - 使用Chance JS生成数据并存储该数据以供以后使用
从本质上讲,我想做的是生成一个使用Chancejs的名称,用于赛普拉斯中的测试用例,但是在单独的文件 /测试用例中,我希望能够回忆起我之前生成的名称,以便我可以选择 /检查它。
我已经将这些代码片段缩短了一点点,因此它们不会完全有意义。
包含常数为常数
require('chance')
export const constants = {
ITEM: chance.word()
}
我尝试使用自己的文件,其中
import { constants } from '../constants';
//Add Item
it('Add a Item', function(){
cy.pass_credentials()
cy.get('.dx-datagrid-toolbar-button > .dx-button-content > .dx-icon').click({force: true})
cy.get('[id$=_Name]').type(constants.ITEM) // using my random name for the item name
}
。单独文件中的项目是我在上一个文件中添加的数据现在被用作其他数据的一部分,
import { constants } from '../constants';
it('Add Data', function(){
cy.pass_credentials()
cy.get('[id$=_TaskId]').type(constants.ITEM) //typing my random name in so I can select it as its now part of a drop making up this new piece of data
cy.contains(constants.ITEM).type('{enter}') //trying to press enter so I select my item
}
但是我遇到的问题是它正在生成一个新的随机名称不在那里。我不确定是否每次将其导入新文件时,这就是导致它重新生成的原因?
我想我可以使用一些指针或更好的解决方案来解决我的问题。
Essentially what I want to do is generate a name using ChanceJS for a test case within cypress, but in a separate file / test case I want to be able to recall that name that I generated earlier so I can select it / check it exists.
I have cut down these code snippets a fair bit so they wont make total sense.
I have tried having my own file with a constant in it called constants.js
require('chance')
export const constants = {
ITEM: chance.word()
}
and then having something like this in a test file where I add an item using the random name as its name
import { constants } from '../constants';
//Add Item
it('Add a Item', function(){
cy.pass_credentials()
cy.get('.dx-datagrid-toolbar-button > .dx-button-content > .dx-icon').click({force: true})
cy.get('[id$=_Name]').type(constants.ITEM) // using my random name for the item name
}
and then later I try to use it again to select the item in a separate file as the data I added in the previous file is now being used as part of this other piece of data
import { constants } from '../constants';
it('Add Data', function(){
cy.pass_credentials()
cy.get('[id$=_TaskId]').type(constants.ITEM) //typing my random name in so I can select it as its now part of a drop making up this new piece of data
cy.contains(constants.ITEM).type('{enter}') //trying to press enter so I select my item
}
But the issue I am having is that it is generating a new random name which means im trying to select something that is not there. I'm not sure if every time I import it on a new file to be used that's what's causing it to re-generate?
I guess I could do with some pointers or a better solution to my issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是第一次生成项目,然后将其保存在
cypress.env()
变量中,然后在测试中使用它。cypress.env()
可以通过整个测试访问,并且一个值将传递给该执行周期的所有测试。您在下一个测试套件中进行测试。您无需在此处调用导入语句,也不需要使用
cypress.env('uniqueItem')
直接使用该值。One way would be to generate the item the first time and save it in
Cypress.env()
variable and then use it through out your test.Cypress.env()
can be accessed by any tests throughout and also one value would be passed to all the tests for that execution cycle.You test in next test suite. You don't need to call the import statements here and directly use the value using the
Cypress.env('uniqueItem')
.