Reputation: 5396
I am trying to matching total number of records but not able to do it because my one totalcount strong in list type variable and another is getting store in single integer variable.
My Code :
`Base.getdriver().get(Js_constants.DashboardURL);
List<WebElement> totaljobs = Base.getdriver().findElements(By.className(Js_constants.TopsJobsactualtotal));
System.out.println(totaljobs.size()); //OUTPUT 30
//Integer.parseInt(totaljobs); //THIS IS NOT WORKING..WHY?
String Topjobscount = Base.getdriver().findElement(By.xpath(Js_constants.Topsjobscount)).getText();
System.out.println(Topjobscount); //OUTPUT 30
Integer.parseInt(Topjobscount);
//HERE IT IS NOT SUPPORTING CONDITION LIKE THIS.
if(Topjobscount == (totaljobs.size()))
{
System.out.println("Jobs & Count are matching");
Reporter.log("Jobs & Count are matching");
}
else
{
System.out.println("Jobs & Count are matching");
Reporter.log("Jobs & Count are not matching");
}
}`
I am using selenium webdriver & Java. I want to compare both variable count but issue is it does not allow me to convert list type variable to integer.
Upvotes: 2
Views: 1042
Reputation: 148
Java is strictly a types language. That means following.
You can not supply anything into a method, it has to be strictly in the same object hierarchy. It results a compile time error if trying to supply wrong type of value as a parameter.
E.g. in your code List<WebElement> totaljobs
clearly says type of object totaljobs is List.
Where in Integer.parseInt()
takes a parameter type of String. So you can not supply a List type of value where string is expected.
rather you can pass size of list by converting it in string as shown following.
String.valueOf(totaljobs.size());
will return a string representation of size of the list.
However you can convert this string value back to integer by calling Integer.parseInt(String.valueOf(totaljobs.size()));
But above does not make any sense as size() method of List, returns an integer value.
Topjobscount is a String value which can not be compared to int value without converting it to integer.
So you can correct (Topjobscount == (totaljobs.size()))
by converting String into integer. As given below.
if (Integer.parseInt(Topjobscount) == totaljobs.size())
Hope this helps.
Upvotes: 0
Reputation: 4692
Integer.parseInt() documentation states that
Returns: the integer value represented by the argument in decimal.
Throws: NumberFormatException - if the string does not contain a parsable integer.
So you need to save that returned value into Integer variable and use that variable.
int count = Integer.parseInt(Topjobscount);
if(count == (totaljobs.size()))
Upvotes: 1
Reputation: 3274
Topjobscount
varibale is a string and you are trying to compare it with an int value.
Calling Integer.parseInt(Topjobscount)
wont convert your string
to int
.You have to assign the value returned by Integer.parseInt(Topjobscount)
to an int value and then compare.
Try this.
int x = Integer.parseInt(Topjobscount);
if(x == (totaljobs.size()))
Remember to catch NumberFormatException
Upvotes: 3