How to convert String to int in Katalon


#1

Hello,

I am running following code and getting compilation error for the lines where I am parsing String to Int.
List ShiftGrid_Rows= DriverFactory.webDriver.findElements(By.xpath(’//tbody/tr’))
for (WebElement ShiftGridRow:ShiftGrid_Rows)
{
String Guest_to_Shift_Ratio= ShiftGridRow.findElement(By.xpath(’/td[2]’)).getText();
Int Guest_Ratio= Integer.parseInt(Guest_to_Shift_Ratio)
println ('Guest to shift Ratio is: '+Guest_to_Shift_Ratio)

		String Guest_Count= WebUI.getText(findTestObject('Object Repository/Locators/Events/Shift Grid Panel/Guest_Count_Total'))
		Int Guest_Count_Int= Integer.parseInt(Guest_Count)
		println ('Guest Count is: '+Guest_Count)
		 
		Int Expected_Projected_Shifts= Guest_Ratio/Guest_Count_Int
		
		Mod= Guest_to_Shift_Ratio%Guest_Count
		
		if (Mod!=0) {
			Expected_Projected_Shifts= Expected_Projected_Shifts+1
		}
		else {
			Expected_Projected_Shifts= Expected_Projected_Shifts
		}
		
		String Actual_Projected_Shifts = ShiftGridRow.findElement(By.xpath('/td[3]')).getText()
		Int Projected_Shifts= Integer.parseInt(Actual_Projected_Shifts)
	
		//WebUI.verifyEqual(Actual_Projected_Shifts, Expected_Projected_Shifts)
		Assert.assertEquals(Projected_Shifts, Expected_Projected_Shifts);
	}

Because I have to some math operations, I need to convert Guest_to_Shift_Ratio and Guest_Count in Integers. But I get the following Compilation error.

11-26-2017 08:32:57 PM - [ERROR] - Test Cases/Events/Create Event- Valid Inputs FAILED because (of) org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 65: unable to resolve class Int
@ line 65, column 8.
Int Guest_Ratio= Integer.parseInt(Guest_to_Shift_Ratio)
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 69: unable to resolve class Int
@ line 69, column 8.
Int Guest_Count_Int= Integer.parseInt(Guest_Count)
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 72: unable to resolve class Int
@ line 72, column 8.
Int Expected_Projected_Shifts= Guest_Ratio/Guest_Count_Int
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 86: unable to resolve class Int
@ line 86, column 8.
Int Projected_Shifts= Integer.parseInt(Actual_Projected_Shifts)
^

4 errors


#2

I have solved the problem. Thanks


#3

Hi Rani,

able to advise how you do it?


#4

How you solve the problem, can you please advise


#5

Use the Groovy method: .toInteger();

This is my code which is converting string to integer successfully.

def Guest_Count = WebUI.getAttribute(findTestObject(‘Object Repository/Locators/Events/Shift Grid Panel/Guest_Count_Total’),

        'value')



    def Guest\_Count\_int = Guest_Count.toInteger()



    WebUI.println('Guest Count is: ' + Guest_Count)



    WebUI.comment('Verify that projected shifts are equal to as expected. Get the total number of rows in shift grid and calculate the projected shifts and verify with actual projected shifts ')



    for (def index = 1; index <= Number\_of\_Rows; index++) {

        def Guest\_to\_Shift_Ratio = DriverFactory.webDriver.findElement(By.xpath(('//table\[@class=\\'k-selectable\\'\]/tbody/tr\[' + 

                index) + '\]/td\[2\]')).getText()



        if (Guest\_to\_Shift_Ratio == 'complex') {

            WebUI.println('Guest to shift ratio is: ' + Guest\_to\_Shift_Ratio)



            continue

        }

        def Guest\_Ratio = Guest\_to\_Shift\_Ratio.toInteger()



        WebUI.println('Guest to shift Ratio is: ' + Guest_Ratio)



        Expected\_Projected\_Shifts = Guest\_Count\_int.intdiv(Guest_Ratio)

#6

Its not working for me. Any additional package needs to be added. Please suggest.


#7

Try with importing this package
import java.lang.Integer as Integer

You may see strike line on ‘tointeger()’ method which is totally fine. Or if it still doesn’t work, can you post what error are you getting?


#8

Thanks Bhawna,

It worked for me as well.


#9

Hi Bhawna,

I am getting below error while converting string to int:

scaCostInt = scaCosts.toInteger() FAILED.
Reason:
java.lang.NumberFormatException: For input string: “2913013.00”

I already added “import java.lang.Integer as Integer” package.

It would be great if you can help me out on this.

Thanks in advance


#10

Hello,

your input string is not Integer, but number with floating point (Float / Double).

String s = “2913013.00”
Double d = Double.parseDouble(s)

You should work with this data type instead of int.


#11

Thank you very much Marek. It solved my main problem.

But Settext statement is not able to enter this Double value into the desired text box. Neither giving any error nor its entering any value to the corresponding field.
Though I have done some workaround for this, I mean writing that value into my test data sheet(excel), doing needed calculation there and writing back from that excel sheet to the text field. But I think there must be a easy way to entering such types of values.

Thanks and Regards
Anil


#12

WebUI.setText method expects string value, so you get an error. You can convert your double value to string using toString() method.

Double finalResult = // your calculation here
WebUI.setText(testObject, finalResult.toString())

#13

Thank you so much Marek for the prompt reply.

But this conversion (double value to string) not working properly for decimal values as the “.” is removed which makes incorrect value entered into the text field. My final value is $323,344,4.43 but after conversion i am getting $323,344,443


#14

Hi Everyone,

My above problem with decimal values is due to the restrictions on the text field in my app, for entering “.” which I came to know later. It was not related to Katalon Studio.

I am really sorry to bother you.

Regards
Anil