IF ELSE is error

When input “Factor_a = 0” the code do not stop and close browser but it go to ELSE

WebUI.openBrowser('')

WebUI.navigateToUrl('http://localhost:8080/SWT_PresentTest/index.html')

WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_a'), factor_a)

WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_b'), factor_b)

WebUI.click(findTestObject('Object Repository/Page_TODO supply a title/input_Result_giaiBtn'))

if (WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_a'), factor_a) == 0) {
	WebUI.verifyElementText(findTestObject('Object Repository/Page_TODO supply a title/span_' + result), result)
	WebUI.closeBrowser()
} else {
	WebUI.verifyElementText(findTestObject('Object Repository/Page_TODO supply a title/span_x  '+result), 'x = '+ result)
	WebUI.closeBrowser()
}

Hi @hotanthanhptg1, welcome to the forum.

The reason you are having trouble is WebUI.setText() does not behave as a boolean and Groovy is treating it as a “falsey” value and always executing the else{} block.

Can you explain in English what you are trying to achieve?

1 Like
  • The conditional if() requires a boolean value or a value that can be treated as a boolean value.

  • The WebUI.setText() return type is void, which, in Groovy code becomes null.

  • When the if() receives a null value it treats it as false.

  • When if() receives a false value, it executes the else{} block (if present).

It’s not easy to see what you’re trying to achieve, but it might be something like this:

if(factor_a == 0) {
  ...
} else {
  ...
}

but that’s just a guess.

I’m sorry to have to say this, but…

It’s quite clear to me that the logical/conditional construct offered by if() is a challenge for you. The code above makes that abundantly clear. Sorry, I am not here to teach you how to write computer code or teach you to become a programmer.

That said, as a first step, here’s a clue: Check your code for typos.

Second step, read the documentation and research logical/conditional statements in Groovy (or any C-like language).

Hover your mouse over the “setText” phrase. A popup should appear giving details about the “setText” statement. Notice the very first word. That is the phrase’s return type (i.e. either String, Boolean, Void or whatever).

Maybe
WebUI.openBrowser('')

WebUI.navigateToUrl('http://localhost:8080/SWT_PresentTest/index.html')

WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_a'), factor_a)

WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_b'), factor_b)

WebUI.click(findTestObject('Object Repository/Page_TODO supply a title/input_Result_giaiBtn'))

WebUI.setText(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_a'), factor_a)

if (WebUI.verifyElementAttributeValue(findTestObject('Object Repository/Page_TODO supply a title/input_Factor a_a'), "value", "0", 10)) {
	WebUI.verifyElementText(findTestObject('Object Repository/Page_TODO supply a title/span_' + result), result)
	WebUI.closeBrowser()
} else {
	WebUI.verifyElementText(findTestObject('Object Repository/Page_TODO supply a title/span_x  '+result), 'x = '+ result)
	WebUI.closeBrowser()
}
1 Like