Variable Arguments

Hi,

The scenario is, the user can select one or multiple item by clicking the associate icon.

I am creating an Keyword to handle the association of one/more items as per the below screenshot.

public static void associateItems(String ... item){

}

My question is : What is best way to handle this scenario. Is it a good idea to use Variable Arguments?

image

@discover.selenium Likely each programmer would have their own opinion, however, I would only pass the list of items if the items selected were not static. If the choice of items is static, then just have the routine select them-no parameters involved.

If you want to have “associateItems” pick a different set of items each time it’s called, then you will have to pass in the references. However, I think a List<String> (or array) would be better so that you could have a variable number of items passed.

Thanks @grylion54

Could you please give an example code.

@discover.selenium You can do some research on Groovy or Java lists on how to add items to the List<String>, so below is a basic concept. I don’t have a select right now that I could test it with so I just used a textbox. This just displays a name for a second and then displays the next name. You will likely want to test/check the names are present in the list and then select them so your keyword will do more.

	@Keyword
	def associateItems(TestObject to, List<String> partNames) {
		for (int cnt = 0; cnt < partNames.size(); cnt++)
		{
			WebUI.setText(to, partNames[cnt])
			WebUI.delay(2)
		}
	}

and to use it:

def names = ["mobile", "smart phone", "digital watch", "smart watch", "keyboard", "wireless mouse"];

CustomKeywords.'my.AddOnKeyWords.associateItems'(findTestObject('myPage/textarea_Notes'), names)

WebUI.delay(1)

names = ["mobile", "wireless mouse", "keyboard", "smart phone"];

CustomKeywords.'my.AddOnKeyWords.associateItems'(findTestObject('myPage/textarea_Notes'), names)
1 Like

Thanks @grylion54 … Appreciate your help. :+1:

I will try your suggestions.