Forum Discussion

Lagencie's avatar
Lagencie
Frequent Contributor
8 months ago

Is there a way to add multiple existence assertions

Hello,

 

I wanted to ask if there is a way - like for example the Smart Assertion - where I can add a single assertion step that checks for 20/30/40 existences of nodes in a response?
Currently the only way I see is to rightclick add existence assertion, multiplied by the amount of nodes I have/need.

In the Smart Assertion I can only add Assertions on the lowest layer


For example here I do not want to test if _master_meta > readPermission > RoleCustomer is valid / true whatever. I want only the master node _master_meta as mandatory, what comes beneath this node does not matter.
And if anyhow possible I want to have one single Assertion step, where I can check

an example like this where i check the existence of all the 4 nodes, but do not care about what is beneath. Or do I have to create 4 assertions with check for existance of _master_meta, _slave_meta, master and slave?

2 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    You might be able to write a custom groovy script for something like this. To me, that tends to be easier than having multiple assertions.

  • nehakakar's avatar
    nehakakar
    Occasional Contributor

    Here's an ex: how you can add multiple existence assertions in JavaScript or a tool-specific scripting..

    // Sample response JSON
    var response = {
        "_master_meta": {},
        "_slave_meta": {},
        "master": {},
        "slave": {}
    };
    
    // Define an array of nodes to check for existence
    var nodesToCheck = ["_master_meta", "_slave_meta", "master", "slave"];
    
    // Create a function to check the existence of nodes
    function checkNodeExistence(nodeName) {
        if (response.hasOwnProperty(nodeName)) {
            // Node exists
            return true;
        } else {
            // Node does not exist
            return false;
        }
    }
    
    // Loop through the nodes and add assertions
    for (var i = 0; i < nodesToCheck.length; i++) {
        var nodeName = nodesToCheck[i];
        var isNodeExist = checkNodeExistence(nodeName);
        
        // Add assertions based on the result
        if (isNodeExist) {
            // Add a pass assertion for node existence
            // You can add this result to your test report
            console.log(nodeName + " exists: PASS");
        } else {
            // Add a fail assertion for node non-existence
            // You can handle this failure in your testing process
            console.error(nodeName + " does not exist: FAIL");
        }
    }

     

    I hope it helps.