Verify the sequence of numbers

image

I am trying to verify the sizes are displayed in the correct sequence. Does someone has some pointers on how this can be achieved.

Each tile is an ‘a’ tag within the main div container.

If all the tiles have the same “class” attribute, VariantTile, then you can collect all of them as a List<WebElement> and then iterate through them in a for loop starting your count at the minimum count on the screen; in the image, it would be like:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement

WebDriver driver = DriverFactory.getWebDriver();

List<WebElement> tileList = driver.getElements(By.xpath('//a[@class="VariantTile"]/span[1]'))

int cnt = 5
for (int i = 0; i < tileList.size(); i++) {
    if (tileList.get(i).getText() == cnt.toString()) {
       WebUI.comment("yea hoo. Found ${cnt}")
    }
    cnt++
}

Whereas I compare the numbers as equal, you may have to throw your exception when they do not equal.

@grylion54

Thank you, it all makes sense but if cnt starts with 5 then tileList.get(cnt).getText() would get the text of 5th element of the list, no?

Also, sometimes the script would run where the size values are like 3, 3.5, 4, 4.5, 5, 5.5 and so on. Also, not always the first size would be 5.

I was thinking of saving all the sizes in an array and then take the first array element as some variable, and then iterate through all the elements of the array using for loop and checking if the next element is greater than the previous one or not.
OR would it be still possible to do it using list?

Your page seems to be an order entry form for a product in an electric commerce site. If you want to verify 100% of “correctness”, then you may want your test to make a query to the backend DB to get the correct list of possible sizes as a list (e.g, [5,6,7,8,9,10,11]) for each individual products to sell.

However, is it necessary for your UI-testing to make queries to DB? Is it worth doing? Possibly not. So you should define a less-strict rules what is “the correct sequence” for your test to verify. The rules should be easier to implement. For example, you may be satisfied if

  • the following rules are applied to all products; regardless types — shoes, hats, socks, shirts, etc
  • all size values should be an positive integer (2, 3, 4, …) or a positive number of one digit after the decimal point (1.5, 2.5, 3.5, …)
  • n1 < n2 < n3 < n4 …
  • the total number of Size values should be greater or equal to 1
  • the total number of Size values should be less or equal to 20

It is up to you what rules you would enforce in the test. Once the rules are defined, then you would be able to implement it as tests. Without the rules defined, nobody can discuss how to write codes.