I have a number of UI-visible amounts which I am comparing with what I have prepared as a part ot my test cases - randomized&mocked test amounts are doubles. What is the usual best practice? Should I convert UI amounts to doubles first (and leave their formatting out) for comparison or it's just better to use amounts as Strings for data comparison that are just formatted in the same way? What is less error prone in the long run?
In UI everything is accessed as string , we use getText() to retrieve the displayed information so it doesn't make sense to complicate it by converting it to double when we have to actually validate the displayed text .
In API test level the json response differentiate values into different types like string , number , null etc . In that case it makes sense to validate it to actual data type . Meaning string to string , and number to number. But not in case if UI
This depends on what are you going to test exactly and on the practices which are adopted in your team.
You need to consider the following when you test "numbers" on UI:
- Representation of a number on UI depends on the locale of the user. For example in some locales fractional part of a number is separated with
.symbol, but in some locales with
- It is always safer to format a number to a string than to parse a string to a number (not all strings are parsed to a number but all numbers are formatted to a string)
- Comparing a fractional quantity to a fractional quantity is often a tricky thing, especially when you deal with primitive types
- When you deal with numbers you can evaluate mathematical expressions which is not possible over the strings.
Personally I prefer to store my golden data as numbers so that I can easily manipulate with them (evaluate expressions). Then when I have the observed quantity on UI I format my golden number according to the used locale and compare two strings (since what you can see on UI is much more a string than a number).