authorjapps / zerocode-hello-world

Zerocode YAML and JSON based declarative steps hello world rest api testing example - soap, database
https://github.com/authorjapps/zerocode
MIT License
18 stars 36 forks source link

How to handle dynamic indexed values in array? #16

Closed bc-nikhil closed 4 years ago

bc-nikhil commented 5 years ago

I want to know how to fetch dynamic index values from an array? In my case indexing of the array, values are changed how to fetch values for the next API.

santhoshTpixler commented 5 years ago

Can you provide an example of your use-case? It helps us understand your question better.

vkjha2000 commented 4 years ago

@bc-nikhil , You can use JSON path here. Have you tried to point to the array element and then pick it via JSON path?

I would suggest please put some clarity(bit more details) around your question.

Please visit this guidelines page(for how to raise an issue) : https://github.com/authorjapps/zerocode/wiki/Guidelines-for-raising-issues

bc-nikhil commented 4 years ago

Hi, Thanks for your response.

The problem is - in the response that I get from API, I need to store some value which comes under 0th index and sometimes comes under 1st index. So please let me know how to handle these dynamic index values?

On Sun, 28 Jul, 2019, 07:43 vkjha2000, notifications@github.com wrote:

@bc-nikhil https://github.com/bc-nikhil , You can use JSON path here. Have you tried to point to the array element and then pick it via JSON path?

I would suggest please put some clarity(bit more details) around your question.

Please visit this guidelines page(for how to raise an issue) : https://github.com/authorjapps/zerocode/wiki/Guidelines-for-raising-issues

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTPGXZ5WYZ4K4MWBSB3QBT6FBA5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD26V43Q#issuecomment-515726958, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTO7NAGOTTUVZQYMZ43QBT6FBANCNFSM4IFASBCQ .

authorjapps commented 4 years ago

Sure. Just check the beginning of the README page(not very sure if it helps).

{
    ...
    "verifications": {
        "body": {
        "addresses[?(@.type=='Holiday')].line1.SIZE": 1  // Indeterministic element position in an array
        }
    }
}

The below sections might help here- Short answer:

Wiki:

bc-nikhil commented 4 years ago

Hi,

Thanks for the response, I got the value, but the problem is I am getting [] brackets like "["53ae3289-8d7f-4e57-9cc2-73eb1417835d"]" this. Can you please suggest what should I do for fixing this?

Thanks Nikhil Bhargava

On Sun, Jul 28, 2019 at 8:54 PM authorjapps notifications@github.com wrote:

Sure. Just check the beginning of the README page(not very sure if it helps).

{ ... "verifications": { "body": { "addresses[?(@.type=='Holiday')].line1.SIZE": 1 // Indeterministic element position in an array } } }

The below sections might help here- Short answer:

Wiki:

- https://github.com/authorjapps/zerocode/wiki/Array-assertions-made-easy--e.g.-SIZE,-element-finder

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTML3XXDF6KZ7PJT2Y3QBW227A5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD27AZ4Q#issuecomment-515771634, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTI2V6TTBE6O7TN6FGDQBW227ANCNFSM4IFASBCQ .

-- Thanks & Regards Nikhil Bhargava Digit88 Technologies | www.digit88.com (m) +91-9343505005 | (e) nikhil@digit88.com preethi@digit88.com

vkjha2000 commented 4 years ago

Yes, you are right, that's how JSON Path works, it always returns an array. It's not a Zerocode issue, but Zerocode has tried to solve it by finding the occurances of the element with the exact value.

See the trail section of those above links.

array elem
authorjapps commented 4 years ago

@bc-nikhil , can you put your response array sample here please(you can strip out the business sensitive data if any)? Next, let us know what you want to verify or assert from that array?

That will clarify the objective you want to achieve and you might get a better/precise answer 👍

bc-nikhil commented 4 years ago

Hi,

Basically, I am trying to fetch some value from different API response like this

"${$.Fetch_DoaminList.response.body.successResult.DomainList[(@.name=='Retail_BILLING')].id}"

but, I am getting the value with array brackets like this

"["53ae3289-8d7f-4e57-9cc2-73eb1417835d"]"

So, the problem is I can't use this id with brackets.

On Wed, Jul 31, 2019 at 2:38 PM authorjapps notifications@github.com wrote:

@bc-nikhil https://github.com/bc-nikhil , can you put your response array sample here please(you can strip out the business sensitive data if any)? Next, let us know what you want to verify or assert from that array?

That will clarify the objective you want to achieve and you might get a better/precise answer 👍

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTPS7AGR6TLBDFJUM53QCFJBHA5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3GTNZI#issuecomment-516765413, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTPDT2BIXJPJS7DHXYLQCFJBHANCNFSM4IFASBCQ .

-- Thanks & Regards Nikhil Bhargava Digit88 Technologies | www.digit88.com (m) +91-9343505005 | (e) nikhil@digit88.com preethi@digit88.com

authorjapps commented 4 years ago

Are you on Gitter? Can you ping us there? Here is the link(in case you haven't joined) : https://gitter.im/zerocode-testing/help-and-usage

bc-nikhil commented 4 years ago

I joined and pinged.

On Wed, Jul 31, 2019 at 2:55 PM authorjapps notifications@github.com wrote:

Are you on Gitter? Can you ping us there? Here is the link(in case you haven't joined) : https://gitter.im/zerocode-testing/help-and-usage

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTIGY7CKP6XLMSKUM7DQCFK65A5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3GU44Q#issuecomment-516771442, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTNRXEXWRM2E6EQQ6XTQCFK65ANCNFSM4IFASBCQ .

-- Thanks & Regards Nikhil Bhargava Digit88 Technologies | www.digit88.com (m) +91-9343505005 | (e) nikhil@digit88.com preethi@digit88.com

authorjapps commented 4 years ago

Still, we are looking for the objective you want to achieve.

Ok, let me put an example here-

Assuming your response is like below-

{
    "status": 200,
    "body": {
        "successResult" : {
            "DomainList":[
                {
                    "name":"Retail_BILLING",
                    "id":"53ae3289-8d7f-4e57-9cc2-73eb1417835d"
                },
                {
                    "name":"xyz_BILLING",
                    "id":"53ae3289-8d7f-4e57-9cc2-73eb1417835d"
                },
                {
                    "name":"Retail_FREE",
                    "id":"53ae3289-SOME_OTHER_ID"
                }
            ]
        }
    }
}

The following expressions will return as expected, i.e.

a)
$.body.successResult.DomainList[?(@.name=='Retail_BILLING')].id
will return
[
   "53ae3289-8d7f-4e57-9cc2-73eb1417835d"
]

b)
$.body.successResult.DomainList[?(@.name=='Retail_BILLING' && @.id=='53ae3289-8d7f-4e57-9cc2-73eb1417835d')]
will return 
[
   {
      "name" : "Retail_BILLING",
      "id" : "53ae3289-8d7f-4e57-9cc2-73eb1417835d"
   }
]

We know the above behavior.

Now the question is what do you want to do from the above?

Which one of the below do you want to validate? Q1. If the array contains"name" : "Retail_BILLING", you should have "id" : "53ae3289-8d7f-4e57-9cc2-73eb1417835d" in the same array element?

Q2. At least one element of the array should have "name" : "Retail_BILLING" and if so, then the id should not be null because you don't know what ID will be generated there.

Q3. At least one element of the array should have "name" : "Retail_BILLING" and if so, then the id should be only 53ae3289-8d7f-4e57-9cc2-73eb1417835d, because you already know it's value before you receive the response.

Q4. You want to validate that the response contains only one element with "name" : "Retail_BILLING"

bc-nikhil commented 4 years ago

Hi,

Thanks for the quick response.

a) $.body.successResult.DomainList[?(@.name=='Retail_BILLING')].id will return [ "53ae3289-8d7f-4e57-9cc2-73eb1417835d" ]

This id "53ae3289-8d7f-4e57-9cc2-73eb1417835d" I want to use for different API's. So, I want this id without square brackets.

right now I am getting a response in this manner "["53ae3289-8d7f-4e57-9cc2-73eb1417835d"]"

I want this value "53ae3289-8d7f-4e57-9cc2-73eb1417835d"

On Wed, Jul 31, 2019 at 3:30 PM authorjapps notifications@github.com wrote:

Still, we are looking for the objective you want to achieve.

Ok, let me put an example here-

Assuming your response is like below-

{ "status": 200, "body": { "successResult" : { "DomainList":[ { "name":"Retail_BILLING", "id":"53ae3289-8d7f-4e57-9cc2-73eb1417835d" }, { "name":"xyz_BILLING", "id":"53ae3289-8d7f-4e57-9cc2-73eb1417835d" }, { "name":"Retail_FREE", "id":"53ae3289-SOME_OTHER_ID" } ] } } }

The following expressions will return as expected, i.e.

a) $.body.successResult.DomainList[?(@.name=='Retail_BILLING')].id will return [ "53ae3289-8d7f-4e57-9cc2-73eb1417835d" ]

b) $.body.successResult.DomainList[?(@.name=='Retail_BILLING' && @.id=='53ae3289-8d7f-4e57-9cc2-73eb1417835d')] will return [ { "name" : "Retail_BILLING", "id" : "53ae3289-8d7f-4e57-9cc2-73eb1417835d" } ]

We know the above behavior.

Now the question is what do you want to do from the above?

Which one of the below do you want to validate? Q1. If the array contains"name" : "Retail_BILLING", you should have "id" : "53ae3289-8d7f-4e57-9cc2-73eb1417835d" in the same array element?

Q2. At least one element of the array should have "name" : "Retail_BILLING" and if so, then the id should not be null because you don't know what ID will be generated there.

Q3. At least one element of the array should have "name" : "Retail_BILLING" and if so, then the id should be only 53ae3289-8d7f-4e57-9cc2-73eb1417835d, because you already know it's value before you receive the response.

Q4. You want to validate that the response contains only one element with "name" : "Retail_BILLING"

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTO34PJV2VK3A4TGAZDQCFPC5A5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3GX4SY#issuecomment-516783691, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTIHJADLCJB7ASZESWDQCFPC5ANCNFSM4IFASBCQ .

-- Thanks & Regards Nikhil Bhargava Digit88 Technologies | www.digit88.com (m) +91-9343505005 | (e) nikhil@digit88.com preethi@digit88.com

nirmalchandra commented 4 years ago

PR https://github.com/authorjapps/zerocode/pull/285 has been raised to solve this issue.

Note- This is not a Zerocode issue, it's a JayWay Json Path issue (or a natural behavior when it comes to finding a matching element - The match could be one or more, hence an array is returned always).

Now, Zerocode has provided a solution to pick the field value from the JsonPath matching result array so that automation testers are unblocked when they face this kind of situation.

nirmalchandra commented 4 years ago

Once the PR is merged, you can use pick the value this way by another tiny step. Basically you need to have this step just to pick the value.

{
    "scenarioName": "Pick customer Id dynamically when JSON Path returns an array",
    "steps": [
        {
            "name": "find_matching_value",
            "url": "org.jsmart.zerocode.converter.MimeTypeConverter",  //<--- This is out-of-the-box, but you can create your own converters to customize the solution.
            "operation": "stringToJson",
            "request": "${$.Fetch_DoaminList.response.body.successResult.DomainList[(@.name=='Retail_BILLING')].id}",
            "assertions": ["53ae3289-8d7f-4e57-9cc2-73eb1417835d"]
        },
        {
            "name": "your_next_step",  //<--- This is example step only
            "url": "/api/v1/customers/${$.find_matching_value.response[0]}",
            "operation": "GET",
            "request": {},
            "assertions": {
                "status": 200,
                "body": {
                    "id": "53ae3289-8d7f-4e57-9cc2-73eb1417835d",
                    "name": "Nikhil BC"
                }
            }
        }
    ]
}

Now, ${$.find_matching_value.response[0]} can be used in subsequent steps where you need just the ID.

bc-nikhil commented 4 years ago

Ok, Thank You so much :-)

On Thu, Aug 1, 2019 at 2:44 PM nirmalchandra notifications@github.com wrote:

Once the PR is merged, you can use pick the value this way by another tiny step. Basically you need to have this step just to pick the value.

{ "scenarioName": "Pick customer Id dynamically when JSON Path returns an array", "steps": [ { "name": "find_matching_value", "url": "org.jsmart.zerocode.converter.MimeTypeConverter", //<--- This is out-of-the-box, but you can create your own converters to customize the solution. "operation": "stringToJson", "request": "${$.Fetch_DoaminList.response.body.successResult.DomainList[(@.name=='Retail_BILLING')].id}", "assertions": ["53ae3289-8d7f-4e57-9cc2-73eb1417835d"] }, { "name": "your_next_step", //<--- This is example step only "url": "/api/v1/customers/${$.find_matching_value.response[0]}", "operation": "GET", "request": {}, "assertions": { "status": 200, "body": { "id": "53ae3289-8d7f-4e57-9cc2-73eb1417835d", "name": "Nikhil BC" } } } ] }

Now, ${$.find_matching_value.response[0]} can be used in subsequent steps where you need just the ID.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTO3RQECOO3TWXJM2TLQCKSQFA5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3J5XBQ#issuecomment-517200774, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTN3DOBKJDPN7MGKZW3QCKSQFANCNFSM4IFASBCQ .

-- Thanks & Regards Nikhil Bhargava Digit88 Technologies | www.digit88.com (m) +91-9343505005 | (e) nikhil@digit88.com preethi@digit88.com

authorjapps commented 4 years ago

Available in version 1.3.11 and higher (when released).

@bc-nikhil , is it urgent or can this wait for another week for other PRs to get approval n merging?

bc-nikhil commented 4 years ago

It is little urgent.

If you can release, it will be great for me.

On Fri, 2 Aug, 2019, 06:09 authorjapps, notifications@github.com wrote:

Available in version 1.3.10 and higher (when released).

@bc-nikhil https://github.com/bc-nikhil , is it urgent or can this wait for another week for other PRs to get approval n merging?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTN7XRPZ2A5LFZRY3T3QCN63HA5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3MIASY#issuecomment-517505099, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTNOKV7HMRDTFBD5CUTQCN63HANCNFSM4IFASBCQ .

authorjapps commented 4 years ago

Done already. Please check the Gitter room 👍

bc-nikhil commented 4 years ago

Thanks

On Mon, 5 Aug, 2019, 16:27 authorjapps, notifications@github.com wrote:

Done already. Please check the Gitter room 👍

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/authorjapps/zerocode-hello-world/issues/16?email_source=notifications&email_token=AMMTFTPBFNA6E3RGYYXYLLTQDABQPA5CNFSM4IFASBC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3ROXRA#issuecomment-518187972, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMTFTIPTSKVLWRFERW3QELQDABQPANCNFSM4IFASBCQ .

santhoshTpixler commented 4 years ago

Can we do it a bit simpler using the JSON paths ALWAYS_RETURN_LIST Then we can have a default [0] at the end to get the first element.

authorjapps commented 4 years ago

Yes, Good one. As discussed Yday,

"${$....DomainList[(@.name=='Retail_BILLING')].id.VALUE}"

should be a simple one to overcome the extra step.

Note the .VALUE which will pick the one element(1st one) from the array

Will raise a new issue for this! 👍