Search code examples
karatenashorn

karate.append not Working with Conditional Check


The docs on this seem really straightforward, but for some reason it's not working for me. I can see that the conditional evaluates correctly, but for some reason the array value doesn't change.

* def requestBody =
    """
    [
        {
            "containerId": "#(randomContainer.containerId)",
            "status": "cancel",
            "closureDetails": {
                "closedDate": "#(closeRemovalDate)",
                "closedReason": "#(closedReason.description)"
            },
            "removeContainerInd": true,
            "containerRemovalDetails": {
                "removalDate": "#(closeRemovalDate)",
                "removalDateChangeReason": "CUSTOMER REQUEST"
            },
            "poNumber": "None",
            "notes": "Moving"
        }
    ]
    """
* def container2 =
    """
    {
        "containerId": "#(randomContainer.odsContainerId)",
        "status": "cancel",
        "closureDetails": {
            "closedDate": "#(closeContainer2)",
            "closedReason": "#(closedReason.description)"
        },
        "removeContainerInd": true,
        "containerRemovalDetails": {
            "removalDate": "#(removalContainer2)",
            "removalDateChangeReason": "Weather"
        },
        "poNumber": "123",
        "notes": "By the gate"
    }
    """

* if (<multiContainer>) {karate.append(requestBody, container2);} multiContainer is set to true.


Solution

  • Use appendTo instead of append.

    Sample Code:

    Feature: Validation
    
    Scenario:
    
        * def requestBody =
            """
            [
                {
                    "containerId": "#(randomContainer.containerId)",
                    "status": "cancel",
                    "closureDetails": {
                        "closedDate": "#(closeRemovalDate)",
                        "closedReason": "#(closedReason.description)"
                    },
                    "removeContainerInd": true,
                    "containerRemovalDetails": {
                        "removalDate": "#(closeRemovalDate)",
                        "removalDateChangeReason": "CUSTOMER REQUEST"
                    },
                    "poNumber": "None",
                    "notes": "Moving"
                }
            ]
            """
        * def container2 =
            """
            {
                "containerId": "#(randomContainer.odsContainerId)",
                "status": "cancel",
                "closureDetails": {
                    "closedDate": "#(closeContainer2)",
                    "closedReason": "#(closedReason.description)"
                },
                "removeContainerInd": true,
                "containerRemovalDetails": {
                    "removalDate": "#(removalContainer2)",
                    "removalDateChangeReason": "Weather"
                },
                "poNumber": "123",
                "notes": "By the gate"
            }
            """
        * def a = 1
        * if (a == 1) {karate.appendTo(requestBody, container2);}
        * print requestBody