Can people share ways in which they have tested the sorting of data. I don’t want to assert on the data itself as this would cause the tests to be flaky and dependent on the data not changing and always being available.
An example of my scenario. I have a table with a column called ‘Date Received’. I’d like to assert that the items in the table are initially ordered by newest to oldest (ascending order).
I had an idea to;
Get the count of the rows in the tables
Iterate over each row and retrieve the column values (in this case, just the date value). Then add then column value to a list. When the iteration has complete, I will have a list of dates in the order then are displayed in the table
Next I will order the list by ascending order and assign it to a new list variable
Finally, I will compare the 2 list to ensure the items are in the correct order.
My pseudo approach above seems a little long winded. Can anyone share a more simpler approach on asserting the sorting on data in Katalon?
I agree, it does seem like you can simplify the approach, and good on you for wanting to!
I’ve done this exact same thing, and here’s how I approached it:
The trick is to make use of the compareTo() method of the Date class:
Date myDate = ...
Date anotherDate = ...
int comparison = myDate.compareTo(anotherDate)
The value of comparison will be an integer value with the following logic:
If myDate is equal toanotherDate, the value of comparison will be 0.
If myDate is beforeanotherDate, the value of comparison will be a negative integer.
If myDate is afteranotherDate, the value of comparison will be a positive integer.
Let’s assume that your dates are in “MM/dd/yyyy HH:mm” format (you can change the format to suite your needs, see SimpleDateFormat). Then the following code should do what you need in an efficient way:
int rowCount = ...
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm")
for(int i = 2; i <= rowCount; i++) {
String previousCellText = ... // get text from row i - 1
String currentCellText = ... // get text from row i
Date previousDate = format.parse(previousCellText)
Date currentDate = format.parse(currentCellText)
assert currentDate.compareTo(previousDate) >= 0
}
Anywhere you see an ellipses (“…”) in the above code you will need to replace with your method of retrieving values from the page.
For getting the cell from each row, note that we are using the loop index to indicate which row we are looking at during any given iteration. previousCellText will always look at row i - 1, and currentCellText at row i.
Let me know if this makes sense. Hopefully it will work for you
Hi,
@ kazurayam @Brandon_Hein could you pls help here
I tried collections.sort() but i could see the value returned is having some special characters. So, it is failing at assert step.
From UI, my data is like [00MKC,etc]
but the reference values after sorting, it is giving me as values as [,00MKC]
Please help me how to remove this special character.
@eug How about not allowing any of the non-desired characters into the table? Use the replace method to change the comma to blank and then not allow blanks into the table.
there are multiple 11 commas in front of first data “abc”
after collections.sort() it gives result like above. I am comparing whether the data received from UI is in ascending order.
Sorry, my last question was useless. It does not help understand your original problem.
Let me ask you again. I need to reproduce your problem on my PC. I need to see the example set of string you want to sort, full lines of your test script, what you expect to see, and what you actually get.
Whenever you get an error, if you want anyone to assist you, then you need to either tell us about the error, give us the log that contains the error or tell us the information that you know because you can see the error but we can’t.
How about reading this and go from there?