我需要告诉这个函数返回在这些长度内的每个变量的随机数。
it('dates', ()=>{
cy.description('Leonel','Andress Messi')
cy.get('#mat-input-4').type(dob())
function dob() {
var day = Math.random(10, 31)
var month = Math.random(10, 12)
var year = Math.random(1940, 2000)
return month + '/' + day + '/' + year
}
})发布于 2022-08-05 01:38:18
注意,random()的上限被排除在外。
另外,小心十一月的30天,先算月。
function getRandomArbitrary(min, max) {
const upper = max + 1 // random() upper limit is excluded
return Math.floor(Math.random() * (max - min) + min)
}
function dob() {
const month = getRandomArbitrary(10, 12)
const day = month === 11 ? getRandomArbitrary(10, 30) : getRandomArbitrary(10, 31)
const year = getRandomArbitrary(1940, 2000)
return month + '/' + day + '/' + year
}
cy.log(dob())
cy.log(dob())
cy.log(dob())
cy.log(dob())

发布于 2022-08-05 01:08:50
你好吗?
在本例中,您可以在另一个js文件中创建您的dob函数,示例在您的支持目录中创建了一个名为utils.js的文件,在您的utils.js文件中放置了如下所示的写函数:
export default class Utils {
dob = () => {
var day = Math.random(10, 31)
var month = Math.random(10, 12)
var year = Math.random(1940, 2000)
return month + '/' + day + '/' + year
}
}在您的spec Cypress测试文件中,导入Utils并调用it中的道布方法:
import Utils from '../../support/Utils';
it('dates', ()=>{
utils = new Utils();
cy.description('Leonel','Andress Messi')
cy.get('#mat-input-4').type(dob())
const date = utils.dob()
})https://stackoverflow.com/questions/73241732
复制相似问题