[WebUI] Select Option By Value


This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/webui-select-option-by-value.html

Hello, I am having the same issue with selecting a label using the value in the code. Here is the code snippit

WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/a_Approval Dashboard’))

WebUI.waitForElementVisible(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label’), 30)

WebUI.waitForElementClickable(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label’), 30)

WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label’))

WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/a_Take Action’))

WebUI.delay(15)

WebUI.selectOptionByValue(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_1’), ‘0’, false)

//WebUI.waitForElementClickable(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_1’), 30)
//WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_1’))
WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/a_Accept’))

WebUI.delay(15)

WebUI.selectOptionByValue(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_2’), ‘1’, false)

//WebUI.waitForElementClickable(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_2’), 30)
//WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/label_2’))
WebUI.click(findTestObject(‘Object Repository/Approval Dashboard/Page_MDM Server/a_Accept (1)’))

Here is the screen I am automating:

image

The values in code are = to the approver name. ![image|663x490]

(upload://IDu61S1UlR2FUJ4Sb9Cgu8UM0Q.png)

I tried using the index of the label and the value. Your help is appreciated.

I am going to criticise this document https://docs.katalon.com/katalon-studio/docs/webui-select-option-by-value.html. I think this document is poorly written.

Before going down into detail, I will share a sample Test Case code that I confirmed working:

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject createTestObject(String xpath) {
	TestObject tObj = new TestObject(xpath)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj	
}

// Open browser and navigate to demo AUT site
WebUI.openBrowser('http://demoaut.katalon.com')

// Click the Make Appointment button to navigate to Login form
TestObject btn_make_appointment = createTestObject('//a[@id="btn-make-appointment"]')
WebUI.click(btn_make_appointment)

// Login Form
TestObject input_username = createTestObject('//input[@id="txt-username"]')
TestObject input_password = createTestObject('//input[@id="txt-password"]')
WebUI.waitForElementPresent(input_username, 5)
WebUI.setText(input_username, 'John Doe')
WebUI.setText(input_password, 'ThisIsNotAPassword')
TestObject button_login = createTestObject('//button[@id="btn-login"]')
WebUI.click(button_login)

// Select \"Hongkong CURA Healthcare Center\" option'
TestObject select_combo_facility = createTestObject('//select[@id="combo_facility"]')
WebUI.waitForElementPresent(select_combo_facility, 5)

WebUI.selectOptionByValue(select_combo_facility, '.*Hongkong.*', true)

WebUI.delay(5)
// Close Browser
WebUI.closeBrowser()

=======

Criticism1: which HTML element the 1st argument TestObject should point to?

In the sample code that Katalon Document presents shows the following fragment:

'Select \"Hongkong CURA Healthcare Center\" option'
WebUI.selectOptionByValue(findTestObject('Page_CuraAppointment/lst_Facility'), 'Hongkong CURA Healthcare Center', false)

I do not see which HTML element is pointed by the TestObject named 'Page_CuraAppointment/lst_Facility'.

  • Is it the <select id='combo_facility> element?
  • or is it any one of <option> elements?
  • or the <label>Facility</label> element?
  • or the <form class="form-horizontal> element?

The document does not tell the answer to us at all.

The “Parameters” explanation says that the 1st argument to the WebUI.selectOptionByValue(to, ...) can be a TestObject that points “a web element” as follows:

Are you kidding me?

A beginner, who relies on the “Web Recording” & “Web Spy” tool, will NEVER be able to obtain an appropriate TestObject as the 1st argument of WebUI.selectOptionByValue(testobject, ...) keyword. Why? Because a <select> element is invisible. People can not touch/click an invisible HTML element through the “Web Recording” tool. Therefore all beginners will never be able to use WebUI.selectOptionsByXXX keyword appropriately.

In other words, Katalon Studio implicitly assumes that a user is skilled enough so that he/she can create a TestObject manually for WebUI.selectOptionByXXX keyword without relying on the “Web Recorder” & “Spy” tools. But the document does not mention this assumption clearly. I suppose that the author was reluctant to write “the tools are not almighty”. Due to this conscious neglect, some guys had hard time.

Criticism2: the sample code does not work.

The sample code presented in the document does not work. You can try copy & paste it into a blank Test Case script and run it. You will find this sample does not run. The script opens the 1st “Make Appointment” page, and stops. It never goes into the 2nd Login page. It will never reach to the 3rd input form page where the WebUI.selectOptionByValue() should be demonstrated on. The sample test stops over.

I hope all of the codes presented in the official documentation should work when copied&pasted. The sample codes that do not work are too bad.

Criticism3: how to use Regular Expression?

See my sample code. It has the following line:

WebUI.selectOptionByValue(select_combo_facility, '.*Hongkong.*', true)

This code will succeed. Please note that I gave the 3rd argument “isRegex” a value of true.

However, if I change this code as follows, it will fail.

WebUI.selectOptionByValue(select_combo_facility, 'Hongkong', true)

Why the latter one fails? ---- this is not the point I would like to raise here. Rather I would like to point out that the document explains nothing about how to use Regular Expression as the 2nd argument to the WebUI.selectOptionByValue() keyword. I suppose that anyone would have hard time to find how to make a good use of Regular Expression here.

Request

I would request Katalon to improve the documentation quality.

@ThanhTo, @Shin

That is not the case. On the example website, the select element with id=combo_facility is visible.

In the post you referred to, that select element is invisible because an inline style is used to set it display:none.

@Russ_Thomas

Thank you for your correction.