In a testcase, when calling a custom keyword, how to pass parameters to the custom keyword?

Hello, i have a custom keyword as shown below:

class start_app {
/**

  • Launch application and verify page load successfully
    */
    @Keyword
    def launchApp() {

    def var_forbidden = findTestObject(‘scr_assertionConsumer/h1_Forbidden’)
    def var_serviceTempUnavailable = findTestObject(‘scr_assertionConsumer/h1_Service Temporarily Unavailable’)

    try {
    WebUI.openBrowser(GlobalVariable.var_launchApp)
    if (WebUI.waitForElementVisible(var_forbidden, 1, FailureHandling.OPTIONAL) || WebUI.waitForElementVisible(var_serviceTempUnavailable, 1, FailureHandling.OPTIONAL)) {
    throw new StepErrorException(‘Access Forbidden!’)
    }
    }
    catch (StepErrorException e) {
    WebUI.closeBrowser(FailureHandling.STOP_ON_FAILURE)
    KeywordUtil.markFailedAndStop(‘Error message is shown, because you have not configure your IPs !!’)
    }
    }
    }

In some testcases, the GlobalVariable.var_launchApp need to change to GlobalVariable.var_launchAppWSFed_0.

How can i used the same custom keyword and only change the parameter when creating the testcases where it will used the second variable?

The name of the variable shouldn’t matter if you make the keyword method more generic:

def launchApp(String app) {
...
try {
  WebUI.openBrowser(app)

...

And in my testcase, how will i set it?

I don’t know. How are you selecting which global to use right now?

if(some_condition) {
  launchApp(GlobalVariable.var_launchApp)
} else {
  launchApp(GlobalVariable.var_launchAppWSFed_0)
}

Thank you this solution works for me :slight_smile: appreciated :slight_smile:

1 Like