首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cypress -使用机会JS生成数据并存储这些数据供以后使用。

问Cypress -使用机会JS生成数据并存储这些数据供以后使用。
EN

Stack Overflow用户
提问于 2022-06-17 14:55:13
回答 1查看 107关注 0票数 0

本质上,我想要做的是使用ChanceJS为柏树内的测试用例生成一个名称,但是在一个单独的文件/测试用例中,我希望能够回忆我之前生成的名称,以便选择它/检查它是否存在。

我已经减少了这些代码片段,所以它们不会完全有意义。

我尝试使用自己的文件,其中包含一个常量,名为constants.js。

代码语言:javascript
复制
    require('chance')
 export const constants = {
    ITEM: chance.word()
  }

然后在测试文件中添加这样的内容,其中我使用随机名称作为其名称添加了一个项。

代码语言:javascript
复制
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
}

然后,我再次尝试使用它来选择一个单独的文件中的项,因为我在前一个文件中添加的数据现在被用作另一个数据的一部分。

代码语言:javascript
复制
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
}

但我的问题是,它正在产生一个新的随机名称,这意味着我试图选择一些不存在的东西。我不确定是否每次在要使用的新文件上导入它时,都会导致它重新生成吗?

我想我可以用一些建议或者更好的解决方案来解决我的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-17 15:10:39

一种方法是第一次生成项目并将其保存在Cypress.env()变量中,然后通过测试使用它。任何测试都可以访问Cypress.env(),而且该执行周期的所有测试都会传递一个值。

代码语言:javascript
复制
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')直接使用该值。

代码语言:javascript
复制
it('Add Data', function () {
  cy.pass_credentials()
  cy.get('[id$=_TaskId]').type(Cypress.env('uniqueItem'))
  cy.contains(Cypress.env('uniqueItem')).type('{enter}')
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72661277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档