我从 Cypress 开始,我想添加 Faker 来生成随机值。但我得到以下结果。你能帮我解决这个问题吗?
login_page.js
const faker = require('faker');
before(() => {
let userData = {
randomName: cy.faker.name.findName(),
randomEmail: cy.faker.internet.email(),
randomPassword: cy.faker.random.number()
}
}
describe('Create new user', function () {
it('Create new user via API', function () {
cy.request('POST', '/cadastrarUsuario', {
nome: userData.randomName,
email: userData.randomEmail,
senha: userData.randomPassword
})
.then((resp) => {
expect(resp.status).to.eq(200)
})
})
})
describe('Login with user just created', function () {
it('Login with user just created via API', function () {
cy.request('POST', '/logar', {
email: userData.randomEmail,
senha: userData.randomPassword
})
.then((resp) => {
expect(resp.status).to.eq(200)
})
})
})
index.js
cy.faker = require('faker');
执行结果
TypeError: Cannot read property 'name' of undefined
Because this error occurred during a 'before all' hook we are skipping all of the remaining tests.
最佳答案
Cypress 与普通的 javascript 一起工作得很好,所以让你的登录页面工作的最简单的方法如下,
const faker = require('faker');
let userData = {
randomName: faker.name.findName(),
randomEmail: faker.internet.email(),
randomPassword: faker.random.number()
}
describe('Create new user', function () {
it('Create new user via API', function () {
cy.request('POST', '/cadastrarUsuario', {
nome: userData.randomName,
email: userData.randomEmail,
senha: userData.randomPassword
})
.then((resp) => {
expect(resp.status).to.eq(201)
})
})
})
关于javascript - 将 Faker 与 Cypress 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60469172/