Create "if" and "else" to continue or restart loop

Hi!

i’m tryng to make a "if and “else” in my loop.
I need make this:

Start the loop
Go to page “x”
submit value “123” in box input
wait page load
IF result is = or >, save the page, ELSE, don’t save.
Restart loop with next value

I have the all code, but now the code save any pages.

My only objective is to tell the code to save only if the value of result is “= or > of 700”, if don’t, restart the loop to the next value and don’t save the page.

@felipe.dks - pLease use below code … Hope it will work for you
for(int i=0; i<=count; i++){
WebUI.naviagtetourl(xpageurl)
WebUI.setText(ele, ‘123’)
WebUI.click(submitelement)
WebUI.waitForPageLoad(30)
if(result>=value){
WebUI.click(saveelement)
}
else{
webUI.click(donsave)
}
}

1 Like

hello,

check this
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

1 Like

Whats Imports i need use? I try here but get error. I thik is missing imports

hello,

please share the error log

1 Like

I can fix the error, but i try and i think i make any thing wrong.

look the code:

WebUI.openBrowser('')

WebUI.navigateToUrl('https://www.4devs.com.br/gerador_de_numeros_aleatorios')

if("//div[@id='resposta']/table/tbody/tr/td[4]">="30"){
Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_P)

robot.keyRelease(KeyEvent.VK_P)
robot.keyRelease(KeyEvent.VK_CONTROL)
}
else{
Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_S)

robot.keyRelease(KeyEvent.VK_S)
robot.keyRelease(KeyEvent.VK_CONTROL)
}

I put a variable to save page or print page, only to i can see if works all right. But in any case, the code only save, never print.

What i do wrong?

I thought you wanted a loop? Where is it?

Save xpath in Object repository and use as web element
Store Test Object - //div[@id=‘resposta’]/table/tbody/tr/td[4] in OR
String value = WebUI.getText(findTestOBject(Test Object)
if (value ==‘30’){
Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_P)

robot.keyRelease(KeyEvent.VK_P)
robot.keyRelease(KeyEvent.VK_CONTROL)
}

You can use your code

1 Like

hi,

you cannot compare String like this way

= “30”
should use
string.equals(“30”)

WebElement element = driver.findElement(By.id(“here your table id”)); //if not id defined then some other locator e.g className or tagName

List rows = element.findElements(By.tagName(“tr”));
for (WebElement row : rows) {
List cols = row.findElements(By.tagName(“td”));
for (WebElement col : cols) {
System.out.print(col.getText() + “\t”);
if (col.getText().equals(“30”){
//do your job here
}
}
System.out.println();
}

something like that

I removed the loop and all others codes, only to i can test this specific code.

All code is very long. I’m making part by part before.

Hi,

I usei this:

WebUI.openBrowser('')

WebUI.navigateToUrl('https://www.4devs.com.br/gerador_de_numeros_aleatorios')

String value = WebUI.getText(findTestObject("teset01"));

if (value<='50'){
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_P)
robot.keyRelease(KeyEvent.VK_P)
robot.keyRelease(KeyEvent.VK_CONTROL)
}
else{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_S)
robot.keyRelease(KeyEvent.VK_S)
robot.keyRelease(KeyEvent.VK_CONTROL)
}

Now all time the code press CRTL+P and never CRTL+S
What i do wrong?

The value of Test Object is //div[@id='resposta']/table/tbody/tr/td[4]

It seems you’re expecting to compare a numeric value. '50' is a String.

What i need to compare the value of string to < or > “50” ?

The code is right. If i put “>” the code save, and if i put “<” the code print.

But the code don’t compare the value of table with number 50.

You can help me? Sorry, i’m noob is katalon yet.

You need to convert your string ‘50’ to an integer 50:

String value = WebUI.getText(findTestObject("teset01"));
Integer intValue = Integer.parseInt(value);
if (intValue <= 50) {
    // Do stuff...
}
else {
    // Do other stuff...
}
1 Like

I think this code is the solution.
But the code return an error:

Test Cases/Testes Apenas FAILED.
Reason:
java.lang.NumberFormatException: For input string: " 26"
	at java_lang_Integer$parseInt.call(Unknown Source)
	at Testes Apenas.run(Testes Apenas:45)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1560981699590.run(TempTestCase1560981699590.groovy:21)

But the code find the correct value, because in this test, the value in the table is 26, and the code says For input string: " 26"

I see the value of table have a space before value. Is not “26”, is " 26". See?

Is this the problem? Have way to remove this space or put space before the ‘50’ ?

Use trim():

String value = WebUI.getText(findTestObject("teset01")).trim();

1 Like

All right, i love you so much <3

If in the string have letters, i can remove all letters and space using que code in next?

Code: String value = WebUI.getText(findTestObject("FULL1")).replaceAll("[^0-9]", "").trim();