Ask a Question

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

SOLVED
cccarl
New Contributor

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.

5 REPLIES 5
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.

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

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
Community Hero

Hey @cccarl,

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

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
cccarl
New Contributor

Thanks @richie that worked perfectly.

cancel
Showing results for 
Search instead for 
Did you mean: