Else if depending on which button is present - how to reference top level global variable

depending on the environment, there are different buttons present. If in dev or dev2 then button A is present, if in staging or prod, button B is present. I wrote an if else statement, and it’s not working. Suggestions/help needed pls.

The issue I’m having specifically is
if ((internal.GlobalVariable == 'Dev') || (internal.GlobalVariable == 'Dev2')) is not recognized - how do I reference the top level Global Variable, ie Dev, Dev2

if (internal.GlobalVariable == 'Dev' || internal.GlobalVariable == 'Dev2'){
	WebUI.click(findTestObject('Page_TESTPartner Test - Services/a_webapp Guest Admin'))
	} else if (internal.GlobalVariable == Staging) {
	WebUI.click(findTestObject('Supportal/Page_Partner Test - Services/a_webapp Guest Admin_btn btn-default dropdown-toggle'))
	WebUI.click(findTestObject('Supportal/Page_Partner Test - Services/a_webapp (Staging)'))
} else if (internal.GlobalVariable == Prod || internal.GlobalVariable == Prod2){
    	WebUI.click(findTestObject('Supportal/Page_Partner Test - Services/a_webapp Guest Admin_btn btn-default dropdown-toggle'))
	WebUI.click(findTestObject('Supportal/Page_Partner Test - Services/a_webapp (Production)'))
}

You don’t normally need to say internal.GlobalVariable - if you have import internal.GlobalVariable at the top of your script, just use GlobalVariable.whatever.

What does that mean? Is there an error? What does it say?

I’m not referencing a global variable within a profile, instead - I’m trying to reference the profile itself.

I have these global variable profiles:

  • Dev
  • Dev2
  • Staging
  • Prod
  • Prod2

within these profiles exist the actual global variables, for example:

Dev profile: BaseURL: webapp.dev.corp
Dev2 profile: BaseURL: webapp.dev2.corp
and so on.

Going by the code you presented, it looks like you are.

Where exactly? I don’t see you doing that in the code.

Aside: You can make your own files (text, json, whatever) and read them at startup. That would provide a way to control the presence of differences not afforded by Katalon profiles. I do this - some of the values I read in are copied over the values in my profiles. Also, a different system could write to those files, too.

Maybe someone else can see what you’re doing - I’m not getting it. @Brandon_Hein? @Marek_Melocik?

I don’t think you can set the Profile programmatically at runtime. Looking at the RunConfiguration documentation, only a get() method is provided, but no set() method.

A similar question is asked here:

1 Like

Instead of implementing conditional logic around your Profile selection, why not get that information directly from the page itself?

For example, if button x only appears in Dev while button y only appears in Prod, so why not try something like:

if(WebUI.verifyElementPresent(findTestObject("path/to/test/object/for/button/x"))) {
    WebUI.click(findTestObject("path/to/test/object/for/button/x"))
}
else if(WebUI.verifyElementPresent(findTestObject("path/to/test/object/for/button/y"))) {
    WebUI.click(findTestObject("path/to/test/object/for/button/y"))
}

… etc.

On a side note, why would an application have different buttons available depending on which environment it’s deployed to? Seems strange… I would also bet that you could come up with a locator that could identify the button you want to click regardless of which environment (which profile) you are executing against.

Can you share any more info on what you are trying to do? Screenshots of the GUI as well as the HTML for the buttons you are working with would help.

@Brandon_Hein - the buttons are different in dev and prod because…

in devlab, there’s a button to take you to another application - the reference on that button says something like webapp_devlab

in prod, the button says web_appstaging or webapp_prod - this is for testing purposes.

but, I think I might have found a solution.

I think I can do this: if (RunConfiguration.getExecutionProfile() == 'dev') { then click button A for example. going to try that.

1 Like

Wait so it sounds like no matter which environment, there will always be some button, but the button just has different attributes. If this is the case, the simplest and best solution would be to just create a custom locator for the button that identifies it regardless of the environment. Then you could do away with all of the messy if/else logic and just click it…

@Brandon_Hein - not quite. In devlab there’s just one button, a single button.

in staging/prod, the button has a dropdown action on the button, where you can make selection from…

Ok makes sense.