0

I am querying lot of fields from Teradata and validating with the values displayed in UI using Selenium (Java). All my fields have only 3 decimal points.

For Eg,Number in DB = 4.199 is displayed as 4.199 in UI and Number in DB = 4.190 is displayed as 4.19 in UI and Number in DB = 4.100 is displayed as 4.1 in UI

When I assert, 4.190 and 4.19 are considered different by Selenium. I have lot of fields of this kind.How can I solve this?

Is there any other function like cast or round that can format the data in the way I want?

giliev
  • 2,938
  • 4
  • 27
  • 47
Aish
  • 185
  • 1
  • 5
  • 16

4 Answers4

2

The best option you have is this:

boolean areTheyEqual = new BigDecimal(a.toString()).compareTo(new BigDecimal(b.toString())) == 0;

This way you are converting them into BigDecimals, then making use of the compareTo method which disregards scaling (so 1.2 is the same as 1.20).

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
2

I usually use BigDecimal for storing decimal when I want to do such manipulations. Here is an example, using the stripTrailingZeros() method:

BigDecimal b = new BigDecimal("2.210");
System.out.println(b.stripTrailingZeros());
//will print 2.21

You can always convert the values to BigDecimal and then check for equality, show them to UI formatted as you want, etc.

giliev
  • 2,938
  • 4
  • 27
  • 47
  • I am using Java 7 and I guess striptrailingzero feature in bigdecimal is a bug. Its works only in Java 8 is what I read in google. I tried in my code but zeros are not removed. – Aish Aug 20 '15 at 19:24
  • I have used it in both Java 7 and 8, and it works fine. Do you have problems with some specific value? – giliev Aug 20 '15 at 19:39
  • It did not work at all. Even after applying strip trailing zero my value remained as 12.000 and that's when I searched in google and some had written it will not work in Java 7. Not sure what went wrong. – Aish Aug 21 '15 at 20:46
  • Here it is, tested on Ideone Java 7 [link]http://ideone.com/PeqFll. I have also tested it on my Java 7 and Java 8 versions running on Linux. Check your Java version, if it is >= 7 I am quiet sure it should work. – giliev Aug 21 '15 at 21:14
  • Thanks gunner. As of now my code is working fine with regex. However will try this out too. – Aish Aug 25 '15 at 22:48
0

When using Selenium webElement.getText() the return is a String and when you comp 2.19 with 2.190 both are different so Assert will fail.

Can we try converting the String to Integer and perform the Assert action.

int num = Integer.parseInt(webElement.getText());

now the stored value in num is type Int so you can assert with another Int.

Else you can go with the suggestion above ( Use of BigDecimal)

Asit Tripathy
  • 133
  • 1
  • 9
  • These numbers aren't ints... why would you cast them to an int? – JeffC Aug 20 '15 at 02:22
  • Then how we will compare the .getText() with DB value which I assume an int. Sting with int using asset will not match. – Asit Tripathy Aug 20 '15 at 02:36
  • You don't have to assume... if you read his question, it stated the numbers in the db are decimals, e.g. 4.199. If you cast that to an int it will truncate it to 4 and then numbers that aren't equal before will now be equal and you will get false positives. – JeffC Aug 20 '15 at 02:43
  • I am not saying to cast the DB value. I am saying when we do a driver.getText().trim() which return the String. Then we can use the string to convert and match with int. Let know if you have better option. – Asit Tripathy Aug 20 '15 at 02:46
  • Neither one of the numbers are ints... they are both decimals, one happens to be in String form. You can't call `Integer.parseInt(webElement.getText());` when `.getText()` returns "4.190" without throwing a `java.lang.NumberFormatException` because 4.190 is not an Integer. It's either a float or a double. Try `int int1 = Integer.parseInt("4.190");`. You should do some reading on [Java types](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). – JeffC Aug 20 '15 at 02:59
  • Thanks i will read .... it was in my mind somehow that the numbers are int not float. Cheers. – Asit Tripathy Aug 20 '15 at 03:29
0

As big decimal is not working for me I was searching for a regex and found it in the below link Remove trailing zero in Java

Its working perfectly.

Community
  • 1
  • 1
Aish
  • 185
  • 1
  • 5
  • 16