admin管理员组

文章数量:1335438

I'm starting with Cypress and I want to add Faker to generate random values. But I'm getting following results. Could you please help me to troubleshooting that?

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');

Execution Results

TypeError: Cannot read property 'name' of undefined

Because this error occurred during a 'before all' hook we are skipping all of the remaining tests.

I'm starting with Cypress and I want to add Faker to generate random values. But I'm getting following results. Could you please help me to troubleshooting that?

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');

Execution Results

TypeError: Cannot read property 'name' of undefined

Because this error occurred during a 'before all' hook we are skipping all of the remaining tests.
Share Improve this question edited Jun 18, 2021 at 21:12 Richard Matsen 23.6k3 gold badges55 silver badges84 bronze badges asked Feb 29, 2020 at 20:08 Clarissa RodriguesClarissa Rodrigues 511 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Cypress works pretty well with plain javascript, so the simplest way to get your login page working would be the following,

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)
            })
    })
})

本文标签: javascriptUsing Faker with CypressStack Overflow