本质上,我想要做的是使用ChanceJS为柏树内的测试用例生成一个名称,但是在一个单独的文件/测试用例中,我希望能够回忆我之前生成的名称,以便选择它/检查它是否存在。
我已经减少了这些代码片段,所以它们不会完全有意义。
我尝试使用自己的文件,其中包含一个常量,名为constants.js。
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
}但我的问题是,它正在产生一个新的随机名称,这意味着我试图选择一些不存在的东西。我不确定是否每次在要使用的新文件上导入它时,都会导致它重新生成吗?
我想我可以用一些建议或者更好的解决方案来解决我的问题。
发布于 2022-06-17 15:10:39
一种方法是第一次生成项目并将其保存在Cypress.env()变量中,然后通过测试使用它。任何测试都可以访问Cypress.env(),而且该执行周期的所有测试都会传递一个值。
import {constants} from '../constants'
//Add Item
it('Add a Item', function () {
Cypress.env('uniqueItem', constants.ITEM) //Random item is set for this test run
cy.pass_credentials()
cy.get('.dx-datagrid-toolbar-button > .dx-button-content > .dx-icon').click({
force: true,
})
cy.get('[id$=_Name]').type(Cypress.env('uniqueItem')) // types in the item
})你在下一个测试套件里测试。您不需要在这里调用导入语句并使用Cypress.env('uniqueItem')直接使用该值。
it('Add Data', function () {
cy.pass_credentials()
cy.get('[id$=_TaskId]').type(Cypress.env('uniqueItem'))
cy.contains(Cypress.env('uniqueItem')).type('{enter}')
})https://stackoverflow.com/questions/72661277
复制相似问题