Forum Discussion

RaghavanG's avatar
RaghavanG
Occasional Contributor
10 months ago

How to Generate Data correctly using datasource in readyAPI

I am getting the 2nd key value pair from map multiple times. Please help in it to find what I am doing wrong here.

  • Hello @RaghavanG 

    maybe this code snippet can help (make sure to adjust line 1 to be whatever the name of your datasource is)...

    def row = testRunner.testCase.testSteps["Data Source 2xyz"].currentRow;
    
    def testDataMap = [TC01:'Pass', TC02:'Fail', TC03:'Fail', TCNN:'Pass'];
    
    if ( (row + 1) <= testDataMap.size() ) {
       result["TestCaseName"] = testDataMap.entrySet()[row].getKey();
       result["TestStatus"] = testDataMap.entrySet()[row].getValue();
    };

    Regards,

    Todd

  • nmrao's avatar
    nmrao
    Champion Level 3

    If we use in the normal groovy, we loop immediately and finish like shown here for quick run

    Since using Datasource, it uses row. for each step increments and Datasource loop thru. So, can't use complete loop in this groovy.

  • Hello @RaghavanG 

    maybe this code snippet can help (make sure to adjust line 1 to be whatever the name of your datasource is)...

    def row = testRunner.testCase.testSteps["Data Source 2xyz"].currentRow;
    
    def testDataMap = [TC01:'Pass', TC02:'Fail', TC03:'Fail', TCNN:'Pass'];
    
    if ( (row + 1) <= testDataMap.size() ) {
       result["TestCaseName"] = testDataMap.entrySet()[row].getKey();
       result["TestStatus"] = testDataMap.entrySet()[row].getValue();
    };

    Regards,

    Todd

  • nmrao's avatar
    nmrao
    Champion Level 3

    RaghavanG 

    I haven't tried, the below should help:

    def map = [TC01: 'Pass', TC02: 'Fail']
    def entries = map.collect { key, value -> [key, value] }
    def row=testRunner.testCase.testSteps['Data Source'].currentRow
    if (row < entries.size) {
    	result['TestCaseName'] = entries[row].first()
    	result['TestStatus'] = entries[row].last()
    }
    • nmrao's avatar
      nmrao
      Champion Level 3

      What is there in the 3rd step? I mean how does the test case looks like?

      row is something in-built variable, I believe.

      Otherwise, row needs to be initialized with zero and increment in Loop step.

      • RaghavanG's avatar
        RaghavanG
        Occasional Contributor

        I ran a simple test case to get the testcasename in groovy. It is failing. even with row initialized 

         

      • RaghavanG's avatar
        RaghavanG
        Occasional Contributor

        It is exactly in the same way with the configuration.

  • nmrao's avatar
    nmrao
    Champion Level 3

    I think it can even be simplified to:

    //Please change the test step name Datasource below as per yours
    def row = context.testCase.testSteps['Datasource'].currentRow
    def map = [TC01: 'Pass', TC02: 'Fail']
    def entries = map.collect { key, value -> [TestCaseName: key, TestStatus: value] }
    if (row < entries.size) result = entries[row]

    RaghavanG 

    EDIT:

    Update the script above.

    Please give it a try. 

     

    • RaghavanG's avatar
      RaghavanG
      Occasional Contributor

      nmraoNo error now. But it is not showing any data in the preview set up and nothing is fetched in the next step.

       

      • nmrao's avatar
        nmrao
        Champion Level 3

        RaghavanG 

        Since you don't have any error now, it might be related to how the test case is created / configured. 

         I don't have the tool, so can't try it.

        Can you please show the screen shot of your test case? Which I required for earlier too. 

        I want to see the test steps involved and their types.

        Hope you have at least 3 steps in the test case:

        1. Datasource
        2. Some other request step
        3. Datasource Loop (this points to step #2)

         

        If not, please refer to the video links provided in one of the other responses in the post. 

        The documentation link provided in previous responses point refer as below, may be give it a try though i don't think that makes difference.

        change from:

        def row = context.testCase.testSteps['Data Source'].currentRow

        to

        def row = testRunner.testCase.testSteps['Data Source'].currentRow