pactumjs / pactum

REST API Testing Tool for all levels in a Test Pyramid
https://pactumjs.github.io
MIT License
544 stars 52 forks source link

How to use function handler for multiple values #362

Closed JyotiPMallick closed 3 months ago

JyotiPMallick commented 4 months ago

I have the following data function handler which is returning a JSON and I want to use it in the next part of my script, it is not working

addDataFuncHandler("randomApplicantInformation",() => {
   <some logic for generating the data>
for eaxmple : 
const employerInfoList = [
    {
      Id: "CON$F{randomApplicantId}",
      name: "Helper",
      Address:
        "8th Floor, Plot No. 8, Baashyaam Willow Square,Chennai, Tamil Nadu, 600032",
    }]
    return {
      "Id: parse('$S{employerInfoList.employeeID}'),
      name: parse('$S{employerInfoList.name}'),
      address: parse('$S{employerInfoList.Address}')
    };
});

And I want to pass it in

"LeadLoan",": {
        "nameOfEmployerOrBusiness": "$F{randomApplicantInformation:${Id}}",
        "employeeID": "$F{randomApplicantInformation.${name}}",
        "addressOfEmployerOrBusiness": "$F{randomApplicantInformation:${address}}",
    }
JyotiPMallick commented 4 months ago

To add a follow up question:

Can we use DataMap |DataTemplate inside a DataFuncHandler ?

ASaiAnudeep commented 4 months ago

Hello @JyotiPMallick , can you share us more details on the actual output and expected output with a minimum reproducible code. For more context, please refer the examples here - https://pactumjs.github.io/api/handlers/addDataFuncHandler.html#adddatafunchandler

JyotiPMallick commented 4 months ago

Apologies for the confusion, to make our test more random we want to use random data payload each time. So solving this we want to use two way approach. For few data we used chancejs to generate random data and for few we created our own random data (note these data are platform specific so cannot be automated with any other way)

sample json payload for automating the test,

"LeadApplicantEmployment": {
        "applicantID": "$F{randomAppId}",
        "occupationTypeID": "$F{randomOccupationTypeId}",
        "nameOfEmployerOrBusiness": "$M{EmployeeNameInformation.name}",
        "designationTypeID": "$F{randomDesignationTypeId}",
        "otherDesignationTypeName": "$F{randomDesignationType}",
        "employeeID": "$M{EmployeeIDInformation.employeeId}",
        "officialEmailID": "$F{getRandomEmail}",
        "phoneNoOfEmployerOrBusiness": "$F{randomLeadPhoneNumber}",
        "addressOfEmployerOrBusiness": "$M{EmployeeAddressInformation.address}",
        "monthlySalaryCurrencyID": "$F{randomSalarycurrencyID}",
        "netMonthlySalaryInRupees": "$F{randomNetMonthlySalary}",
        "otherMonthlyIncome": "$F{randomOtherMonthlyIncome}",
        "noOfYearsInCurrentOrganisation": "$F{randomNoOfYearsInCurrentOrganisation}",
        "totalYearsOfExperience": "$F{randomTotalYearsOfExperience}"
    }

Out off all the values above, three values which are mentioned below are supplied from another json source,

  nameOfEmployerOrBusiness": "$M{EmployeeNameInformation.name}",
"employeeID": "$M{EmployeeIDInformation.employeeId}",
"addressOfEmployerOrBusiness": "$M{EmployeeAddressInformation.address}",

Code Snippet,

addDataFuncHandler("randomApplicantInformation",() => {
const employerInfoList = [
    {
      Id: "CON$F{randomApplicantId}",
      name: "Helper",
      Address:
        "8th Floor, Plot No. 8, Baashyaam Willow Square,Chennai, Tamil Nadu, 600032",
    },
    {
    employeeID: "SUF$F{randomEmployeeId}",
    name: "Suffescom Solutions Inc",
    Address:
      "3rd Floor Phase, D-256, 8-A, Industrial Area, Sector 75, Sahibzada Ajit Singh Nagar, Punjab 160055",
    }..............
    ]
   const employerDetails =
  employerInfoList[Math.floor(Math.random() * employerInfoList.length)];
    return {
      "Id": "EmployeeNameInformation.name",
     "name": employerInfoList.name,
    "address": employerInfoList.Address
    };
});

note: employerInfoList information has been filled with dummy data due to security concern. But pattern is similar.

so randomApplicantInformation is returning 3 values which I want to use,

Approach 1 : I tried with ${randomApplicantInformation:${Id}} , ${randomApplicantInformation:${name}} "Didn't work" I am getting undefined error

Approach 2 : Tried to use dataMap by changing following,

addDataFuncHandler("randomApplicantInformation",() => {
const employerInfoList = [
    {
      Id: "CON$F{randomApplicantId}",
      name: "Helper",
      Address:
        "8th Floor, Plot No. 8, Baashyaam Willow Square,Chennai, Tamil Nadu, 600032",
    },
    {
    employeeID: "SUF$F{randomEmployeeId}",
    name: "Suffescom Solutions Inc",
    Address:
      "3rd Floor Phase, D-256, 8-A, Industrial Area, Sector 75, Sahibzada Ajit Singh Nagar, Punjab 160055",
    }..............
    ]
  const employerDetails =
  employerInfoList[Math.floor(Math.random() * employerInfoList.length)];

 stash.addDataMap({
  'EmployeeInfomation': {
    "Id": "EmployeeNameInformation.name",
    "name": employerInfoList.name,
   "address": employerInfoList.Address
  }
});
});

With this approach I am able to retrieve the first which is Id value for other 2 I am getting undefined I tried to use DataTe,plate I am getting the same error