Ab123
Ab123

Reputation: 433

How to pass variable value into URL using cypress

I am working on Cypress API Automation task and want to pass value to an endpoint. I fetched value from postgress database and stored it in a variable called user_id. But I am facing issue while hitting endpoint.

Value fetched from database:

log[{user_id: 52}]

Issue:

cy.request() failed on:

http://localhost:8080/user/[object%20Object]

The response we received from your web server was:

Below is my Code

it.only('Delete User', ()=>{
   let user_id = cy.task("connectDB","select user_id from user_details where first_name='XYZ'").then(cy.log);
cy.request({
    method:'DELETE',
    url:'localhost:8080/user/'+user_id+'' 
}).then((res) => {
    expect(res.status).to.eq(200);
})
})

I want to pass '52' as a value to the endpoint, Can someone help here ?

Upvotes: 0

Views: 1496

Answers (4)

jjhelguero
jjhelguero

Reputation: 2565

Your error pretty much says it all. user_id is an object. You'll have to access the property to pass the correct value, which would be user_id[0].user_id

it.only('Delete User', ()=>{
   let user_id = cy.task("connectDB","select user_id from user_details where first_name='XYZ'").then(cy.log);
cy.request({
    method:'DELETE',
    url:'localhost:8080/user/'+ user_id[0].user_id
}).then((res) => {
    expect(res.status).to.eq(200);
})
})

Upvotes: 0

Ab123
Ab123

Reputation: 433

I am able to solve my problem using this approach

Below is my Spec File:

    it("Database Test", () => {
  cy.task("READFROMDB",
  {
    dbConfig: Cypress.config('DB'),
    sql:'select * from "user_details"'
  }).then((result) =>
  {
    //console.log(result.rows)
    let user_id = result.rows[0].user_id
    console.log("user id is :"+user_id)
  })
});

Below is my Cypress.config.js File:

const {defineConfig} = require("cypress");
const pg= require("pg")

module.exports = defineConfig({
  e2e:{
    setupNodeEvents(on,config){
        on("task",{
          READFROMDB({dbConfig,sql}){
            const client = new pg.Pool(dbConfig);
            return client.query(sql);
          }
        })
    },
    DB:{
      user: "postgres",
      password: "password",
      host: "localhost",
      database: "postgres",
      port: '5432'
    },
  }
})

Upvotes: 0

Paul Murdin
Paul Murdin

Reputation: 175

cy.task() needs to use .then() to get the value, like this example.

cy.task("connectDB", "select user_id from user_details where first_name='XYZ'")
  .then(user_id => {
    cy.request({
      method: 'DELETE',
      url: 'localhost:8080/user/' + user_id
    }).then((res) => {
      expect(res.status).to.eq(200)                // passes
    })
  })

If you try to assign the return to a variable let user_id = cy.task("connectDB"... you will get a chainer object, but it's not much use without a .then() to extract the value.

Upvotes: 2

Delano van londen
Delano van londen

Reputation: 416

cy.url().should('include', '/user_id') 

docs: https://docs.cypress.io/api/commands/url#Differences

or if that did not help, try looking at these docs: https://docs.cypress.io/guides/guides/environment-variables#Option-1-configuration-file

Upvotes: 0

Related Questions