A.Joly
A.Joly

Reputation: 2387

cypress - use of a function loses parameter data

I'm using Cypress and, in order to be able to use a request from several files, I want to set it in a function. This request is a login request. At the beginning I have a file xx.js that holds this request, and I want to put it a function called "login", in another file that will contain all 'system' functions (called system.js) and then call it through the function.

In my xx.js file, the code is like this

    console.log(user)
    cy.request({
    method: 'POST',
    url: 'system/http/login', // baseUrl is prepended to url
    form: true, // indicates the body should be form urlencoded and sets Content-Type: application/x-www-form-urlencoded headers
    body: {
      u: user.username,
      p: user.password,
      referer: '/rootapp/index.html'
    },
    failOnStatusCode: true
  })

When executed, the log shows


user :  {username: "myName", password: "myPwd"}
password: "myPwd"
username: "myName"
__proto__: Object

I chose to create a function in my system.js, I import it in my xx.js file and I call it in place of the request, passing user as a parameter

exports.login = function (user) {
    console.log("parameter user : ", user)
    if (user =! undefined)
    {
        console.log("u : ", user.username)
        console.log("p : ", user.password)
        cy.request({
            method: 'POST',
            url: 'system/http/login', // baseUrl is prepended to url
            form: true, // indicates the body should be form urlencoded and sets Content-Type: application/x-www-form-urlencoded headers
            body: {
            u: user.username,
            p: user.password,
            referer: '/rootapp/index.html'
            },
            failOnStatusCode: true
        })
    }
    else
        throw(new Error("no user identified"));
  };

But my request returns 403, when I look at the console log, it shows

parameter user :  {username: "myName", password: "myPwd"}
password: "myPwd"
username: "myName"
__proto__: Object
u :  undefined
p :  undefined

How is it possible that I lose data, is it a question of type of parameter ? I'm a beginner in cypress and javascript

thanks for any clue of what's happening

Upvotes: 0

Views: 200

Answers (1)

user16003578
user16003578

Reputation:

Is the line if (user =! undefined) a typo?

Normally that would be

if (user !== undefined) {

or even just this would work

if (user) {

Upvotes: 3

Related Questions