Forum Discussion

cccarl's avatar
cccarl
New Contributor
5 years ago

Is there any way to retrieve the database-generated id of a created record during a test case?

Just getting started with ReadyAPI.

 

I have a set of REST Apis that handle CRUD operations on a table. I have a test case in my mind where I create a record, store the id returned, update the record, delete the record and finally use the id to read and assert a not found.

 

I am probably simply using the wrong search terms, but I can't find any way yet to do the crucial first part - store the id after calling the "create" API.

  • Hey cccarl,

    What you need to do is a property transfer. There's several ways to do this, but the easiest i find considering you're gonna reuse the responseId is to save the responseId to a property step. Once the responseId is saved to a property step you can reuse later in your test.

    Some basic steps
    On the Rest request's response, highlight the responseId attribute and click on the 'Transfer to'
    Select 'Add Properties Step'
    Click 'Ok' on 'Create Properties Step' dialogue
    Click 'Ok' on 'Create target property' dialogue
    Click 'Ok' on the 'Transfer to Property' form

    The above generates a 'PropertyTransfer' step and a 'Properties' step.

    The resultant of the above steps is that you can then reuse the responseId property value saved in the Properties step on subsequent steps in your test case.

    Hope this was clear!

    Rich
  • richie's avatar
    richie
    Community Hero
    Hey cccarl,

    What you need to do is a property transfer. There's several ways to do this, but the easiest i find considering you're gonna reuse the responseId is to save the responseId to a property step. Once the responseId is saved to a property step you can reuse later in your test.

    Some basic steps
    On the Rest request's response, highlight the responseId attribute and click on the 'Transfer to'
    Select 'Add Properties Step'
    Click 'Ok' on 'Create Properties Step' dialogue
    Click 'Ok' on 'Create target property' dialogue
    Click 'Ok' on the 'Transfer to Property' form

    The above generates a 'PropertyTransfer' step and a 'Properties' step.

    The resultant of the above steps is that you can then reuse the responseId property value saved in the Properties step on subsequent steps in your test case.

    Hope this was clear!

    Rich
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    [Update] Understood that I did not get initial question right after I read accepted reply by richie.

     

     

    > REST Apis that handle CRUD operations

    This makes me think that this API will serve some end-user interface. If this is a case, then how your end-user is expected to identify just created record? Can you apply the same logic in your test?

    As one of the possible approaches, consider to create the entity with some unique identifier. For example, the Name with date-time timestamp as a prefix or suffix. Preserve this identifier in test property and use it later to retrieve created record.

     

    • cccarl's avatar
      cccarl
      New Contributor

      Thanks for the reply. I am only at the beginning of my investigation into what ReadyAPI can do for me.

       

      It's an API that does CRUD. The basic start point is the Create bit.  There is an API for that which accepts a body in a POST command. That's fine and it returns a database model of the record inserted, one field of which is an auto-generated ID.

       

      The workflow I'm after in my test case in ReadyAPI is:

      • Have a step that calls the "Create" endpoint.
      • Read the returned response body and extract the "ID" field from the json body in there
      • Use that ID to do an update which calls the "Update" endpoint
      • repeat for other steps

      The bit I'm stuck on is how do I get the ID out of the response body. Is this something to do with the PropertyTransfer step I can see in the available steps?

       

      The Json response I get is:

      {
      "id": 2,
      "name": "Australia",
      "routingPlan": "Australia",
      "createdAt": "2019-11-18T11:09:13.7998173Z",
      "createdUser": "User 1",
      "updatedUser": null
      }
      ...so I'd like to be able to store that "id" property after the create step and use it in several further steps.
       
      Edit: Just saw Richie's reply which must have arrived as I was typing this one. I'll look into that richie thanks
      • richie's avatar
        richie
        Community Hero
        Hey cccarl,

        I answered how to extract the value from the response in my last post fella,

        Nice one,

        Rich