Jared
Jared

Reputation: 3

how to return a data from custom command and to use it again in cypress

const { generateNum } = require("./generate_num")

Cypress.Commands.add('NewUserRegister', () => {
    const { userName, email, password} = generateNum();
        cy.get(':nth-child(3) > .nav-link').should('contain.text', 'Sign up').click()
        cy.get(':nth-child(1) > .form-control').type(userName)
        cy.get(':nth-child(2) > .form-control').type(email)
        cy.get(':nth-child(3) > .form-control').type(password)
        cy.get('.btn').should('contain.text', 'Sign in').click()
});

I'm trying to create a custom command that will login to a user's page using this command's credentials. I have no idea how to do it. The data should be typed in this fields

    cy.get(':nth-child(1) > .form-control').type(?????????)
    cy.get(':nth-child(2) > .form-control').type(?????????)

So i did this

const { generateNum } = require("./generate_num")

Cypress.Commands.add('NewUserRegister', () => {
    const { userName, email, password} = generateNum();
        cy.get(':nth-child(3) > .nav-link').should('contain.text', 'Sign up').click()
        cy.get(':nth-child(1) > .form-control').type(userName)
        cy.get(':nth-child(2) > .form-control').type(email)
        cy.get(':nth-child(3) > .form-control').type(password)
        cy.get('.btn').should('contain.text', 'Sign in').click()
    return cy.wrap(userName, email, password)
});

And this

it('log in register user', () => {
cy.then(data => {
cy.get(':nth-child(2) > .nav-link').should('contain.text', 'Sign in').click()
cy.get(':nth-child(1) > .form-control').type(data.email)
cy.get(':nth-child(2) > .form-control').type(data.password)
cy.get('.btn').should('contain.text', 'Sign in').click()
})

TypeError Cannot read properties of undefined (reading 'email')

Upvotes: 0

Views: 123

Answers (2)

Jared
Jared

Reputation: 3

So idk what is. I just wrote everything that came into my head so here is a custom command that create a new user and log after

its commands.js

const { generateNum } = require("./generate_num");

Cypress.Commands.add('NewUserRegister', () => {
const {userName, email, password} = generateNum() 
cy.visit('/')
const user = ({userName, email, password})
cy.get(':nth-child(3) > .nav-link').should('contain.text', 'Sign up').click();
cy.get(':nth-child(1) > .form-control').type(userName);
cy.get(':nth-child(2) > .form-control').type(email);
cy.get(':nth-child(3) > .form-control').type(password);
cy.get('.btn').should('contain.text', 'Sign in').click();
cy.get(':nth-child(3) > .nav-link').should('contain.text', 'Settings').click();
cy.get('.btn-outline-danger').should('contain.text', 'Or click here to logout').click()
.then(response => ({...user}))

});

So it's a .cy.js bc idk maybe custom command can't work inside other custom anyway

/// <reference types="cypress" />
const { generateNum } = require("../support/generate_num");

describe('', () => {
it('main_page', () => {
cy.NewUserRegister().then(user =>{
cy.get(':nth-child(2) > .nav-link').should('contain.text', 'Sign in').click()
cy.get(':nth-child(1) > .form-control').type(user.email)
cy.get(':nth-child(2) > .form-control').type(user.password)
cy.get('.btn').should('contain.text', 'Sign in').click()
})
});
})

Upvotes: 0

Grainger
Grainger

Reputation: 154

You are missing brackets on the return value.

return cy.wrap({userName, email, password})

This gives you an object with properties that are not undefined.

Upvotes: 2

Related Questions