-1

I have CSV file (with around 300 rows) to read values which is passed in Json payload. On executing this test scenario for every CSV file's row, a new ID is generated in Json response i.e. response consist of an ID for each row present in CSV file.

Example: Id = 1 for CSV row 1 Id = 2 for CSV row 2 " " Id = n for CSV row n

Scenario Outline: Create new content value (using CSV file) for movie: <Movie> with Cast: <Cast>
    When json payload = {'attributes':[{'entity_attribute_id': 41, 'value': '<Genre>'},{'entity_attribute_id': 42, 'value': '<Language>'},{'entity_attribute_id': 39, 'value': '<Movie>'},{'entity_attribute_id': 101,'value': '2020-12-03'}],'entity_type_id': '10'}   
    Given url baseUrl + postContentValues   
    And def cookie = read('content-cookie.txt')
    And header cookie = cookie
    And request payload
    When method post
    Then status 200
    And print response
    And def contentId = response.id
    And print contentId
    And def createdContentIdList = karate.append(contentId)
    And print createdContentIdList
#   And def createdContentIdList = karate.append(contentId)
#   And def createdContentIdList = []
#   And def fun = function(x){ karate.appendTo(createdContentIdList, x) }
#   And karate.forEach(contentId, fun)
#    And print createdContentIdList
    Examples:
    |read('RoughTable.csv')|

Scenario Outline: Pass contentID in URL

    Given url baseUrl + getSpecificContentValue +'<contentValueId>' + '?client=web&'
    When method get
    Then status 200
    And print response

    Examples:
    |contentValueId|
    |contentId     |

If I use karate.append() or karate .appendTo(), Following createdContentIdList is generated (which is incorrect):

enter image description here

Now,I want to create an array (named ContentID) which has all row's IDs like ContentID = [1,2,3,....n]

And Pass the content id one by one to next api's url.

How can I do this in Karate ?

Thanks in advance !!

Priyanka B
  • 101
  • 9
  • I want to create an array from the JSON responses obtained on CSV file rows in json payload. Each time a row is fetched and executed > JSON response is obtained which has unique id > I want a list/array which has all the IDs generated from each CSV record – Priyanka B May 21 '20 at 11:20

2 Answers2

1

Solution for above question is as follows:

Feature: Movie List 

Background:
    And configure headers = ('classpath:karate-config.js')

Scenario Outline: Create new content value (using CSV file) for movie: <Movie> with Cast: <Cast>

    When json payload = {'attributes':[{'entity_attribute_id': 41, 'value': '<Genre>'},{'entity_attribute_id': 42, 'value': '<Language>'},{'entity_attribute_id': 39, 'value': '<Movie>'},{'entity_attribute_id': 101,'value': '2020-12-03'}],'entity_type_id': '10'}   
    Given url baseUrl + postContentValues   
    And def cookie = read('content-cookie.txt')
    And header cookie = cookie
    And request payload
    When method post
    Then status 200
    And print response
    And def contentId = response.id

    Given url baseUrl + getSpecificContentValue + contentId + '?client=web&'
    When method get
    Then status 200
    And print response

    Examples:
    |read('RoughTable.csv')|

Step execution of above scenario is as follows: Extract CSV record > Pass in JSON payload > Hit API url to get 1st records response > define a variable to copy ID (from response) > pass ID (obtained in previous step) as a url param of next api > the response will give details of specified ID

Note: This steps will repeat until last record of CSV file.

Priyanka B
  • 101
  • 9
  • 1
    so there are 2 calls made, the second call uses the id returned in the first - this is what the "hello world" example does, so I don't understand what was so confusing, can you explain so I can fix the docs – Peter Thomas May 21 '20 at 15:03
0

Don't use a Scenario Outline it is not designed for sharing data across Scenario-s, you can only use some pre-existing data.

Use a second feature-file and see this example: https://github.com/intuit/karate#data-driven-features

Also please read this: https://stackoverflow.com/a/46080568/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248