Simple if else statement help

I am trying to create a simple if else statement but not sure I understand the tools flow.

Id like to validate I set the username field to say “LoginName” as the text, then check if the text isn’t what is expected to set it to “Username” else to change it to simply “Name”…

Can someone point me in the right direction? Thanks…

@jbullen A sample if statement could be:

WebUI.setText(findTO(input_Sign On_userName, “LoginName”)

if (WebUI.verifyNotMatch(WebUI.getAttribute(findTO(input_Sign On_userName), “value”), “UserName”, false)) {
// if did not match
} else {
// it did match
}

The verifyNotMatch returns a boolean which makes a good “condition” for the if statement. I used getAttribute but the statement could be getText based upon the element.

Sorry for the silly question but how would this look in a manual selection vs code?

There are no silly questions. Silly answers on the other hand… there’s a few of those.

Try the code you were given.

Save the file.

Then switch to Manual view.

Edit the code.

Check Manual view.

Easy?

Ahh okay, thank you… wasn’t aware you could do that and that makes things a lot easier.

Thank you :slight_smile:

1 Like

be sure to SAVE between switching IF YOU MAKE CHANGES. Some users (NOT me) have issues switching between views. :confused:

I tried importing the code and this results in an error so I tried the below and still cant seem to get it to meet the objective.

You have to ensure the code is properly formed: use simple quotes (that don’t have curves), properly placed parenthesis, etc. I copied my text into KS, fixed it up and have put it again below:

WebUI.setText(findTestObject(‘input_Sign On_userName’), ‘LoginName’)

if (WebUI.verifyNotMatch(WebUI.getAttribute(findTestObject(‘input_Sign On_userName’), ‘value’), ‘UserName’, false)) {
WebUI.comment(“if did not match”)
} else {
WebUI.comment(“it did match”)
}

You could copy and paste the above text to Notepad first to remove smart quotes and then copy and paste to KS.

Unable to get attribute (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to get attribute
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘UserName’)

if (WebUI.verifyNotMatch(WebUI.getAttribute(findTestObject(‘input_Sign On_userName’), ‘value’), ‘UserName’, false)) {
WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘1’
)
} else {
WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘2’)
}

Are you familiar with Inspecting the element’s code to see if there are attributes or just text? Right click on the page beside the element and select Inspect. You may have to do this twice to have the code pop to the specific element.

Check if the input_Sign On_userName element has a “value” attribute with any text. If there is no “value” attribute, but the text shows, then change getAttribute to getText and adjust the rest of the statement like deleting the “value” part.

You could also copy and paste the input_Sign On_userName from elsewhere and replace the one in the code I gave to ensure it is spelled correctly, (especially the path structure to the element), like below:

if (WebUI.verifyNotMatch(WebUI.getAttribute(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘value’), ‘UserName’, false)) {

WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘UserName’)

if (WebUI.verifyNotMatch(WebUI.getText(findTestObject(‘input_Sign On_userName’), ‘text’), ‘UserName’, false))

{

WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘1’)

} else {

WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘2’)

}

Is met with the error:

Test Cases/New Test Case FAILED.

Reason:

groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.getText() is applicable for argument types: (null, java.lang.String) values: [null, text]

Possible solutions: getText(com.kms.katalon.core.testobject.TestObject), getText(com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling), setText(com.kms.katalon.core.testobject.TestObject, java.lang.String), setText(com.kms.katalon.core.testobject.TestObject, java.lang.String, com.kms.katalon.core.model.FailureHandling), getAt(java.lang.String), getUrl()

WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘User’)

if (WebUI.verifyNotMatch(WebUI.getText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’),), ‘User’, false))
{
WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘1’)
} else {
WebUI.setText(findTestObject(‘Page_e3 - Login/input_Sign On_userName’), ‘2’)
}

I can only now ever get this to return a value of 1 since adjusting the code regardless of if the username values match or not. It appears there is a real struggle to see the value within the input and after following suggestions I cannot get this to work…

You will find this a lot easier to read and reason about:

TestObject toUserName = findTestObject("Page_e3 - Login/input_Sign On_userName")
WebUI.setText(toUserName, "User")

if (WebUI.verifyNotMatch(WebUI.getText(toUserName), "User", false)) {
  WebUI.setText(toUserName), "1")
} else {
  WebUI.setText(toUserName), "2")
}

In the if() statement you will notice your are using WebUI.getText(). You might expect that getText() allows you to “read” a value and setText() will allow you write a value -

  unfortunately, that is not true.

You need WebUI.getAttribute()

Further, I think you need to manage Failure Handling, and pass FailureHandling.OPTIONAL to WebUI.verifyNotMatch()

Going based off of the help desk comment it appeared get attribute wasn’t going to work, I’ll adjust the code again but it seems like the keyword elements for something like this aren’t helpful.

That’s a person that calls himself helpdesk :confused:

This forum is the “help desk”.