0

I need to pass two parameters in my POST request that should be randomly generated title and description I tried using Cypress._.random(0, 1e6) but had no luck as it generated only a few numbers The requirement for the title field is: 5-20 alphanumeric characters The need for the description field is: 20-200 alphanumeric characters


it('should PAA', () => {

        const uuid = () => Cypress._.random(0, 1e6)
        const title = uuid()
        const description = uuid()
        cy.getLocalStorage("accessToken").should("exist");
        cy.getLocalStorage("accessToken").then(token => {
        cy.request({
            method : 'POST',
            url: 'myURL',
            headers : {"Authorization": "Bearer " + token},
            body: {
              "extra_fields":{
                  "price":5000,
                  "price_type":"price"
              },
              "area":0,
              "type":"general",
              "user_external_id":"0926c075-6afb-4d66-ba17-8a01a6a93e70",
              "purpose":"for-sale",
              "state":"pending",
              "source":"strat",
              "contact_info":{
                  "name":"Muhammad Haris BH",
                  "mobile":"+923234077603",
                  "roles":[
                    "allow_chat_communication",
                    "show_phone_number"
                  ]
              },
              "title": title,
              "description":description,
              "photos":[
                  
              ],
              "location_id":"2-51",
              "category_id":"249"
            }
          }).then( (response) =>{
            expect(`Response.status = ${response.status}`).to.eq('Response.status = 201')
          })
        });
      });

I'm passing the title and description in the body but UUID only generates a few numeric characters I need to pass an alphanumeric string with at least 20 characters.

Bhavesh Dhaduk
  • 1,888
  • 15
  • 30

1 Answers1

1

This question Generate random string/characters in JavaScript has what you need.

But for an extra random dimension, randomize the length between the validation minimum and maximum.

function makeStringOfLength({min, max}) {

  const length = Math.random() * (max - min + 1) + min
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

  let result = '';

  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * characters.length));
  }

  return result;
}

const title = makeStringOfLength({min:5, max:20})
const description = makeStringOfLength({min:20, max:200})
Ellie.K.C
  • 50
  • 5