0

If Env-Variable drawingmanage equals false the value gets assigned successfully(else block) whereas if it is true the value doesn't assign(if block)

if(pm.variables.get("drawingmanage") == 'true'){
            var stat=true;
            modelid.map((data)=>{
                data.models.map((modeldata)=>{
                    checkContainsdrawing(data.projectID,modeldata.modelID).then((datas)=>{
                        if(datas.json()._isSucceed && stat){
                            console.log(data)
                                pm.collectionVariables.set('projectid',data.projectID);
                                pm.collectionVariables.set('projectname',data.projectName);
                                pm.collectionVariables.set('modelid',data.models[0].modelID);
                                pm.collectionVariables.set('modelName',data.models[0].name);
                                stat=false;
                        }
                    }).catch((err)=>{
                        console.log('failed');

                })
            })
        })


}else{

    pm.test("Get Project ID and Project Name", function () {
        console.log('No drawing management selected');
        pm.collectionVariables.set('projectid',modelid[0].projectID);
        pm.collectionVariables.set('projectname',modelid[0].projectName);
    });

    pm.test("Get Model ID and Model Name", function () {
        pm.collectionVariables.set('modelid',modelid[0].models[0].modelID);
        pm.collectionVariables.set('modelName',modelid[0].models[0].name);
    });  
}

checkContainsdrawing Function

let checkContainsdrawing=(projectid,modelid)=>{
    const urldata=(projectid,modelid)=>{
    return {
        url:`${pm.variables.get("url")}api/blm/pdm/drawing?pid=${projectid}&mid=${modelid}`,

        method:'GET',
        header: {
            Authorization: pm.collectionVariables.get('authtoken')
        }
    }
    }
    return new Promise((resolve,reject)=>{
        pm.sendRequest(urldata(projectid,modelid),(err,res)=>{
            if(err){
                reject(err);
            }

            resolve(res);
        })    
    })

}

My test scenario

My main aim is to run the API automation without any testdata so i am using API parameritizing same pattern as the website works coming to explain this test scenario below

  1. This API lists all the project where the user has access
  2. Project may or may not contains model
  3. So here i want to filter the projects which has model
  4. And again to the model i want to validate whether the model contains a note sheet
  5. For this i am making request to an API to check whether there is an note sheet to that particular project and model if there i am assigning to a collection variables which will be used in all Test
8
  • What's the actual issue
    – PDHide
    Commented Mar 6, 2020 at 9:48
  • @PDHide Collection variables don't get assigned inside the if block Commented Mar 6, 2020 at 10:02
  • why are you creating a promise ?
    – PDHide
    Commented Mar 6, 2020 at 10:51
  • I want to perform an request to a URL based upon the response the projectid will be updated for the next request. If I perform this without the promise the function gives the result as null Commented Mar 6, 2020 at 10:55
  • why can't you add this code to test script of the next request instead ?
    – PDHide
    Commented Mar 6, 2020 at 10:57

1 Answer 1

2

Problem

THe send requests work asynchronously and hence get resolved only at the end, so to avoid this, use set time out,

Whichever functions that use the specific collection variable, wrap it with set time out.

Fix:

if(pm.variables.get("drawingmanage") == 'true'){
            var stat=true;
            modelid.map((data)=>{
                data.models.map((modeldata)=>{
                    checkContainsdrawing(data.projectID,modeldata.modelID).then((datas)=>{
                        if(datas.json()._isSucceed && stat){
                            console.log(data)
                                pm.collectionVariables.set('projectid',data.projectID);
                                pm.collectionVariables.set('projectname',data.projectName);
                                pm.collectionVariables.set('modelid',data.models[0].modelID);
                                pm.collectionVariables.set('modelName',data.models[0].name);
                                stat=false;
                        }
                    }).catch((err)=>{
                        console.log('failed');

                })
            })
        })


}else{

    pm.test("Get Project ID and Project Name", function () {
        console.log('No drawing management selected');
        pm.collectionVariables.set('projectid',modelid[0].projectID);
        pm.collectionVariables.set('projectname',modelid[0].projectName);
    });

    pm.test("Get Model ID and Model Name", function () {
        pm.collectionVariables.set('modelid',modelid[0].models[0].modelID);
        pm.collectionVariables.set('modelName',modelid[0].models[0].name);
    });  
}

    setTimeout(function() {
     console.log("The collection varaible is")
    console.log(pm.collectionVariables.get('projectid'))

    }, 5000);

OR

 if (pm.variables.get("drawingmanage") == 'true') {
        var stat = true;
        modelid.map((data) => {
            data.models.map((modeldata) => {

                const urldata=(projectid,modelid)=>{
                    return {
                        url:`${pm.variables.get("url")}api/blm/pdm/drawing?pid=${projectid}&mid=${modelid}`,

                        method:'GET',
                        header: {
                            Authorization: pm.collectionVariables.get('authtoken')
                        }
                    }
                    }

                pm.sendRequest(urldata(data.projectID,modeldata.modelID), (err, datas) => {
                    if (datas.json()._isSucceed && stat) {
                        console.log(data)
                        pm.collectionVariables.set('projectid', data.projectID);
                        pm.collectionVariables.set('projectname', data.projectName);
                        pm.collectionVariables.set('modelid', data.models[0].modelID);
                        pm.collectionVariables.set('modelName', data.models[0].name);
                        stat = false;
                    }
                })
            })
        })


    } else {

        pm.test("Get Project ID and Project Name", function () {
            console.log('No drawing management selected');
            pm.collectionVariables.set('projectid', modelid[0].projectID);
            pm.collectionVariables.set('projectname', modelid[0].projectName);
        });

        pm.test("Get Model ID and Model Name", function () {
            pm.collectionVariables.set('modelid', modelid[0].models[0].modelID);
            pm.collectionVariables.set('modelName', modelid[0].models[0].name);
        });
    }

    setTimeout(function() {
     console.log("The collection varaible is")
    console.log(pm.collectionVariables.get('projectid'))

    }, 5000);

You can wrap all the remaining code like

setTimeout(function() {

       pm.test("Status code is 200", function () {
              pm.response.to.have.status(200);
        });

       pm.test("Status code is 200", function () {
          pm.response.to.have.status(200);
       });

}, 5000);

Here, the function is evoked automatically after 5 seconds by the time the callback time gets resolved.

Note: you need to do this only for the particular request because the next request is executed only after the timeout function is complete. SO all other requests will have the updated value.

1
  • I made some workaround and made it work. Anyway thanks for your valuable answer Commented Mar 9, 2020 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.