Field availability depending on the value. Use If else

Hi, I have a question I would like to add fields depending on myself, can I do something with “if else”?
For example.:
IF (Field A = ‘Value 1’)
Else (complete fields A1, A2, A3)
IF (Field A = ‘Value 2’)
Else (complete fields A4, A5, A6)

Can the Katalon tool make such a dependency? If so, could anyone show a piece of code how to handle it?

Sorry, I don’t quite understand your logic.

Is this what you mean?

if(WebUI.getAttribute(findTestObject("Path/to/fieldA"), "value") == "jacek") {
  WebUI.setText(findTestObject("Path/to/fieldA1"), "Jacek 1")
  WebUI.setText(findTestObject("Path/to/fieldA2"), "Jacek 2")
  WebUI.setText(findTestObject("Path/to/fieldA3"), "Jacek 3")
}

if(WebUI.getAttribute(findTestObject("Path/to/fieldA"), "value") == "russ") {
  WebUI.setText(findTestObject("Path/to/fieldA3"), "Russ 1")
  WebUI.setText(findTestObject("Path/to/fieldA4"), "Russ 2")
  WebUI.setText(findTestObject("Path/to/fieldA5"), "Russ 3")
}

Assuming you have some statements associated with the “if” part, then Yes it can. Many of the statements return a boolean, which can be stand alone as the condition, such as:

if (WebUI.verifyElementChecked(findTestObject(‘My Pages/input_FrenchDoorsNo’),10)) {
// if no french doors
} else {
// if want french doors
}

or when the statement returns a String, then it would need some String to compare to (or you can compare two statements that return Strings or compare statement to a String variable). A sample comparing to a String is below:

if (WebUI.getText(“Path to Element/Add_Property”) == “Perfect”)
{
WebUI.delay(1)
WebUI.click(findTestObject(‘My Page/a_BreadCrumbClient’))
WebUI.waitForPageLoad(10)
blah blah blah
}
else
{
WebUI.click(findTestObject(‘My Pages/input_PropertyYes’))
WebUI.click(findTestObject(‘My Pages/input_WindowsDoublePaneYes’)
WebUI.click(findTestObject(‘My Pages/input_FrenchDoorsNo’)
other blah blah blah
}

if there are no statements associated with the “if” part, then you can just use the negate reference to run the “else” statements.

I use this solution and I have an error. Can you tell me what’s wrong @helpdesk ?

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5’),
‘AR’, true)

If(WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5’)==“AR”)
{
WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_2_c9f1c5’),
‘2’, true)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Araz’)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Maraz’)
}

//in this place i have error “expecting EOF, found ‘else’ @” //else
{

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_2_c9f1c5’),
‘4’, true)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Bara’)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Hara’)}

@jacekjureczko0 Is the capital letter on the “i” of the “if” a copy paste instance or is that what you have in code? It must be a small letter, like if. Can you place your cursor right beside the top bracket. Does the appropriate lower bracket get indicated?

On another topic, I notice you set the boolean for “selectOptionByValue” as true. This should be false unless you need regular expression (since you are comparing simple String “values”, you wouldn’t).

Also note, a “selectOptionByValue” returns a “void”, not a String. You will need to select the option, then add something like:

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5’)

Select select = new Select(DriverFactory.getWebDriver().findElement(By.xpath(“select_1 xpath here”)));

String strContact = select.getFirstSelectedOption().getText();

if (strContact == “AR”) {

@jacekjureczko0 On the “if” line, you are missing a closing parenthesis BEFORE the “==”

if(WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5’)) == “AR”)

Still I have something wrong

WebUI.selectOptionByValue(findTestObject(‘Tpath_to_the_element/select_1_c9f1c5/field1’), ‘AR’, false) – In this step I must use true or false?

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’)
//acutally in this step I have error unexpected token: Select @ //Select select = new Select(DriverFactory.getWebDriver().findElement(By.xpath(‘path_to_the_element/select_1_c9f1c5/field1’)));
String strContact = select.getFirstSelectedOption().getText();
if (strContact == “AR”) {

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5/field2’), ‘5436’)

WebUI.selectOptionByValue(findTestObject('path_to_the_element/select_1_c9f1c5/field3), ‘DT’, true)

} else {
WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘AO’, true)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5/field2’), ‘12345’)

WebUI.selectOptionByValue(findTestObject('path_to_the_element/select_1_c9f1c5/field3), ‘DO’, true)
}

@helpdesk what`s wrong ?

"On the “if” line, you are missing a closing parenthesis BEFORE the “==”

if(WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_1_c9f1c5’)) == “AR”)"

Error:

groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.selectOptionByValue() is applicable for argument types: (null) values: [null]
Possible solutions: selectOptionByValue(com.kms.katalon.core.testobject.TestObject, java.lang.String, boolean), selectOptionByValue(com.kms.katalon.core.testobject.TestObject, java.lang.String, boolean, com.kms.katalon.core.model.FailureHandling), deselectOptionByValue(com.kms.katalon.core.testobject.TestObject, java.lang.String, boolean)
at Use_If_Script.run(Use_If_Script:38)
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 TempTestCase1587538392673.run(TempTestCase1587538392673.groovy:23)

For the Select, you need to add an import statement. If you hit the CTRL + SHIFT + o (that’s oh, not zero; it’s a three key combo), the appropriate import statement will be added:

import org.openqa.selenium.support.ui.Select

For the MissingMethodException, it means you have not formed the WebUI statement correctly. If you hover your mouse over the “selectOptionByValue” statement, you should see a pop-up that shows the proper form it has, such as (TO Object, String, boolean), like below:

WebUI.selectOptionByValue(findTestObject(‘path to element/element’), ‘2’, false)

However, you are using selectOptionByValue, but ‘AR’ is NOT a value, like I have above, so you should use selectOptionByLabel instead. And, you can use verifyOptionSelectedByLabel to ensure it was properly selected AND it returns a boolean that you could use as the condition within an “if” statement. In this case, you would not need to use the SELECT that I mentioned above to do the comparison (selectOptionByValue has verifyOptionSelectedByValue as its companion).

The boolean part in the third position can be true if you need Regular Expression. You need Regular Expression when you don’t have an EXACT quote. (there are lots of info to assist you to read up on RE) If you have an exact quote, such as ‘AR’, you do not need RE, and you should use false.

Sample like this using verifyOptionSelectedByLabel as the condition for the if statement:

WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5’), ‘AR’, false)

if (WebUI.verifyOptionSelectedByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5’), ‘AR’, false, 10))
{

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_2_c9f1c5’), ‘2’, false)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Araz’)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Maraz’)

}
else
{

WebUI.selectOptionByValue(findTestObject(‘path_to_the_element/select_2_c9f1c5’), ‘4’, false)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Bara’)

WebUI.setText(findTestObject(‘path_to_the_element/select_1_c9f1c5’),‘Hara’)

}

In this line i have another error:
if (WebUI.verifyOptionSelectedByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5’), ‘ARG’, false, 10))

2020-04-23 07:47:30.454 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: There is no option with label ‘Arg’ present in object ‘path_to_the_element/select_1_c9f1c5’ (Root cause: com.kms.katalon.core.exception.StepFailedException: There is no option with label ‘Arg’ present in object ‘path_to_the_element/select_1_c9f1c5’
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword$_verifyOptionSelectedByLabel_closure1.doCall(VerifyOptionSelectedByLabelKeyword.groovy:92)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword$_verifyOptionSelectedByLabel_closure1.call(VerifyOptionSelectedByLabelKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.verifyOptionSelectedByLabel(VerifyOptionSelectedByLabelKeyword.groovy:106)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.execute(VerifyOptionSelectedByLabelKeyword.groovy:70)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyOptionSelectedByLabel(WebUiBuiltInKeywords.groovy:2179)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyOptionSelectedByLabel$4.call(Unknown Source)

I use this element:

Xpath element:
/select[@id=‘_com_liferay_login_web_portlet_LoginPortlet_country’]

When i use value i have problem in this line:

WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5’), ‘ARG’, false)

Morning. First, I apologize, our coding standard has value as a reference number. I extrapolated to other companies and I should not have. You could use Value on the ‘AR’. Or Label on the ARG.

When I look at the HTML, I would expect the XPath of the “select_1_c9f1c5” element to be:

/select[@id=‘id_4’]

(the above should have 2 slashes at start) because you are using id, the XPath can be re-written as:

id(“id_4”)

(no slashes needed with this format) So, create a test object with the above and try it out as both a Value on the AR or a Label on the ARG. See what you get.

Hi @helpdesk !!!
Its look better but its work only if Arg=Arg. If i select Arg and verify different label its not work.

WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’),
‘Arg’, false)

WebUI.verifyElementVisible(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’))

if (WebUI.verifyOptionSelectedByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’),
‘Aor’, false, 10)) {
WebUI.selectOptionByValue (findTestObject (ścieżka_do_elementu / select_2_c9f1c5/field2), 2, false)

WebUI.setText (findTestObject (ścieżka_do_elementu / select_1_c9f1c5/field3), Araz)

WebUI.setText (findTestObject (ścieżka_do_elementu / select_1_c9f1c5/field4), Maraz)

} else {
WebUI.selectOptionByValue (findTestObject (ścieżka_do_elementu / select_2_c9f1c5/field2), 4, false)

WebUI.setText (findTestObject (ścieżka_do_elementu / select_1_c9f1c5/field3), Bara)

WebUI.setText (findTestObject (ścieżka_do_elementu / select_1_c9f1c5/field4), Hara)
}

Error
2020-04-24 07:03:48.696 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Only 0 in 1 option(s) with label ‘Aor’ is/are selected in object ‘path_to_the_element/select_1_c9f1c5/field1’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Only 0 in 1 option(s) with label ‘Aor’ is/are selected in object ‘path_to_the_element/select_1_c9f1c5/field1’
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword$_verifyOptionSelectedByLabel_closure1.doCall(VerifyOptionSelectedByLabelKeyword.groovy:94)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword$_verifyOptionSelectedByLabel_closure1.call(VerifyOptionSelectedByLabelKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.verifyOptionSelectedByLabel(VerifyOptionSelectedByLabelKeyword.groovy:106)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.execute(VerifyOptionSelectedByLabelKeyword.groovy:70)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyOptionSelectedByLabel(WebUiBuiltInKeywords.groovy:2179)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyOptionSelectedByLabel$4.call(Unknown Source)
at Use_If_Script.run(Use_If_Script:32)
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 TempTestCase1587704604070.run(TempTestCase1587704604070.groovy:23)
)
2020-04-24 07:03:48.829 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to verify option is selected by label ‘Aor’ in object ‘path_to_the_element/select_1_c9f1c5/field1’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to verify option is selected by label ‘Aor’ in object ‘path_to_the_element/select_1_c9f1c5/field1’
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.verifyOptionSelectedByLabel(VerifyOptionSelectedByLabelKeyword.groovy:106)
at com.kms.katalon.core.webui.keyword.builtin.VerifyOptionSelectedByLabelKeyword.execute(VerifyOptionSelectedByLabelKeyword.groovy:70)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyOptionSelectedByLabel(WebUiBuiltInKeywords.groovy:2179)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyOptionSelectedByLabel$4.call(Unknown Source)
at Use_If_Script.run(Use_If_Script:32)

Yes. You Select the Value or Label, then Verify the item you Selected. If you want to check all the values or labels, then you have multiple statements. The below statements return boolean, so they can be the condition within an if statement.

WebUI.verifyOptionPresentByLabel((findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘ARG’, false)

WebUI.verifyOptionPresentByLabel((findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘AOR’, false)

however, if you want to have the different labels display, then use the select statement (the below statements return void, so they CANNOT be the condition within an if statement):

WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘ARG’, false)

WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘AOR’, false)

and lastly, if you want to ensure that the item that you selected was actually displayed (the below statements return boolean, so they can be the condition within an if statement):
After selecting ARG:

WebUI.verifyOptionSelectedByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘ARG’, false)

After selecting AOR:

WebUI.verifyOptionSelectedByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘AOR’, false)

So, your test could be to “select” a value or label, then use the “verify” the value or label was selected within your “if” statement.
// check label ARG
WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘Arg’, false)

WebUI.verifyElementVisible(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’))

if (WebUI.verifyOptionSelectedByLabel(indTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘Arg’, false)) {

} else {
}

// check label AOR
WebUI.selectOptionByLabel(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘Aor’, false)

WebUI.verifyElementVisible(findTestObject(‘path_to_the_element/select_1_c9f1c5/field1’))

if (WebUI.verifyOptionSelectedByLabel(indTestObject(‘path_to_the_element/select_1_c9f1c5/field1’), ‘Aor’, false)) {

} else {
}