Help creating a custom keyword

Hi, I’m trying to do a pretty simple keyword. Its just to make my scripts a little shorter. when I am creating it I’m told that there is an error on the first bracket on the first line that is stopping me from using this keyword.

public class selectItem(string group,string type)
    WebUI.delay(1)
    WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Group'), group, false)
    WebUI.delay(2)
    WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Type'), type, false)
    WebUI.delay(1)
}

Does anyone happen to know what this problem is and how I might be able to fix it.

Thank you in advance

Hi Jonathan,

use String instead of string. It may cause this error. Also, do not declare method as a class. Use following construction:

public class YourKeyword {	public void selectItem(String group, String type) {
		WebUI.delay(1)
		WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Group'), group, false)
		WebUI.delay(2)
		WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Type'), type, false)
		WebUI.delay(1)
	}
}
2 Likes

Than you for the help. It works correctly if i take out void and just have:

public class newKeyword {
	@Keyword
	def selectItem(String group, String type) {
		WebUI.delay(1)
		WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Group'), group, false)
		WebUI.delay(2)
		WebUI.selectOptionByLabel(findTestObject('Functions/Item Type Location/Info Type'), type, false)
		WebUI.delay(1)
	}
}

Capture.PNG

Indeed. There are multiple ways how to define custom keyword. The advantage of my solution is that you can simply call _YourKeyword.selectItem() _in your test case and avoid using CustomKeywords.‘some.long.method.call.here()’. CustomKeywords class is kind of wrapper around keywords, but both options are ultimately valid. :slight_smile:

2 Likes