Forum Discussion

radhika1's avatar
radhika1
Contributor
7 years ago
Solved

array out of bound exception.

Hi,    I need to remove  properties in my property step from my 6th line onwards till end .   While using the below code I am getting array out of bound exception.please suggest.     import co...
  • JHunt's avatar
    7 years ago

    Are there 10 properties or 9? Your for loop is trying to go up to the 10th property (because the range is from 0..9).

     

    The line that says p.removeProperty(f) is also suspect.

     

    One of the nifty ideas with Groovy is that you can avoid writing your own code to iterate over all the items in a collection, which is prone to the above type of logic error. Instead, you can use built in features like each, collect, find, findAll, etc.

     

    Here I have used eachWithIndex to achieve the same thing (and I only use an index because of your specific requirement which was given in terms of an index):

     

    testRunner.testCase.getTestStepByName("Properties").with { testStep ->
        testStep.properties.eachWithIndex { property, int propertyIndex ->
            if (propertyIndex >= 6) testStep.removeProperty (property.key)
        }
    }