我是 Cypress 的新手,遇到一个小问题,需要一些帮助。
我的应用程序中有一个输入字段,允许我输入名称。该名称必须是唯一的,并且不能与系统中已有的名称相同。
我当前正在通过以下方式单击此输入字段:
cy.get('input[type="text"].form-control')
如果我使用cy.type()
命令,这将始终输入提供的相同值,但每次测试运行时,我想分配一个不同的值。
// Fill in some details of a new class to proceed with creation
cy.get('.pull-left > h4').contains('Add a new class')
cy.get('input[type="text"].form-control') // Clicks on the field
// Some code to go here to create a random string and use what was created and
type this into the field above
预期
创建一个函数,允许生成随机字符串,然后通过普通的 cypress 命令将其输入到输入字段中。
最佳答案
我刚刚在博客中找到了另一种方法,将其添加到此处以供引用。
const uuid = () => Cypress._.random(0, 1e6)
const id = uuid()
const testname = `testname${id}`
cy.get('input').type(testname);
对我来说效果很好:)
关于javascript - 在 Cypress 中创建随机字符串并将其传递给 cy 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54113654/