Search code examples
javalistselenium-webdrivergroovykatalon-studio

groovy/katalon: Validate sequence of list


I have two lists:

def listA = ['2', '3', '4.5', '5', '6.5', '7', '7.5', '8']
def listB = ['10', '11', '13', '1', '2', '2.5']

I want to validate the sequence of both lists. For listA, every current element is smaller than the next one so I have the following loop for that.

double previous = 0.0
for (int i = 0; i < listA.size(); i++) {
    double current = Double.parseDouble(listA.get(i))
    if (previous >= current) {
        KeywordUtil.markFailed("previous=${previous}, current=${current} not in order")
    }
        previous = current
    }

The above code works correctly for listA.

But I am stuck on how to validate the second list. The requirement is that from 10 - 13.5 the elements should be in sequence, but after 13.5, '1' should be there.

The listB is dynamic. Sometimes it would be ['11.5', '2', '3'] and sometimes ['12', '2'] or any other combination. Whatever the list may be, it should always follow the sequence of 10-13.5 and then 1-3

Update I would try to explain the requirement a little better. So basically I am testing the sequence of sizes of a product shown in the image.

Requirement

So I am testing different sites and different products are tested. Each product would have different size availability. Most products would have the sizes in numerical sequence, like current size would be smaller than the next. For them, the above for loop would work.

But for certain categories like 'Little Kids', the requirement is that sizes should be in the order --> 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 1, 1.5, 2, 2.5, 3. Now some of these products might be missing certain sizes, but whatever sizes are available, they should follow the above order. Example, if 12 and 2 sizes are available, then it should be in that order, not 2 and 12. Similarly if 13, 1 and 3 sizes are available, it should be in that order, and not 1, 3, 13


Solution

  • Not sure if I understood your requirements (especially about listB) correctly, but I would implement both checks using sorting like so:

    1. Here I use the toSorted() which creates a new list based on a Comparator which is expressed as a Closure {}. The comparator compares the values converted to double. The sorted list is compared to the original one, and if reordering took place -> the list does not comply with rules:
    boolean checkA( list ){
      list == list.toSorted{ it.toDouble() }
    }
    
    assert checkA( [ '2', '3', '4.5', '5', '6.5', '7', '7.5', '8' ] )
    assert !checkA( [ '2', 1, '3', '4.5', '5', '6.5', '7', '7.5', '8' ] )
    
    1. Here I use the same method, but the comparator works based on index of each element inside the WEIGHTS list, and thus sorts the original list by WEIGHTS.
    def WEIGHTS = [ 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 1, 1.5, 2, 2.5, 3 ]*.toDouble().asImmutable()
    
    def checkB = { list ->
      list == list.toSorted{ WEIGHTS.indexOf it.toDouble() }
    }
    
    assert checkB( ['10', '11', '13', '1', '2', '2.5'] )
    assert checkB( ['11.5', '2', '3'] )
    assert checkB( ['12', '2'] )
    assert !checkB( ['11', '10', '1', '13', '2', '2.5'] )
    assert !checkB( ['1', '12', '3'] )