How to use If...Else... statement to check on different browsers

Hi,

I would like to use If and Else statements for different browsers. My scenario is: If I’m running in Firefox browser then execute these steps. Else, to execute other steps if I’m running the script in Chrome browser.

I try to run this script but it doesn’t run into the If…Else statement at all.

void setBrowser(String browser) {
if (browser.equalsIgnoreCase(‘Firefox’)) {
WebUI.uploadFile(findTestObject(‘Object 1’), ‘C:\\Desktop\\ABC.txt’)

    Thread.sleep(1000)  

    Runtime.getRuntime().exec('C:\\\Desktop\\\AutoIt\\\UploadDataFile.exe')  
}  
 
else (browser.equalsIgnoreCase('Chrome')) {  
    WebUI.uploadFile(findTestObject('Object 1'), 'C:\\\Desktop\\\ABC.txt')  

    WebUI.sendKeys(findTestObject('Manufacturer Preference/input_DataLine'), Keys.chord(Keys.TAB, Keys.ENTER))  

    Thread.sleep(1000)  

    Runtime.getRuntime().exec('C:\\\Desktop\\\AutoIt\\\UploadDataFile.exe')  
}  

}

Why don’t you use Profiles to test in different browsers? You can set in Test Suite Collections in which browser would you like to execute tests?

plaidshirtakos said:

Why don’t you use Profiles to test in different browsers? You can set in Test Suite Collections in which browser would you like to execute tests?

Hi,

Thanks for your reply. The reason is because for the file upload function, it works properly in 3 steps for Firefox browser and 4 steps for Chrome browser (as seen in my If…Else statement above). I was wondering if I can put everything in 1 script and depending on which browser I run the test, the script can be executed successfully.

I see, but it depends on settings of browser. You should set same settings in both browsers, so you can prevent “hit ENTER” step.

Please refer to this article to see, how to reproduce same settings in your browsers: https://docs.katalon.com/display/KD/Execution+Settings

Jacqueline,

I was wondering if I can put everything in 1 script and depending on
which browser I run the test, the script can be executed successfully.

There’s nothing wrong with that, IMO.

That said, I would probably place the decision (the if/else) in a custom Keyword and call that from my test case. That way, the test case will look clean (well, “cleaner”, anyway).

Plus, doing that, you don’t need to choose a profile based on a decision that one test case might need - let the test (or Keyword) decide. The less profiles you create, the better, IMO.

1 Like

Russ Thomas said:

Jacqueline,

I was wondering if I can put everything in 1 script and depending on
which browser I run the test, the script can be executed successfully.

There’s nothing wrong with that, IMO.

That said, I would probably place the decision (the if/else) in a custom Keyword and call that from my test case. That way, the test case will look clean (well, “cleaner”, anyway).

Plus, doing that, you don’t need to choose a profile based on a decision that one test case might need - let the test (or Keyword) decide. The less profiles you create, the better, IMO.

Hi Russ,

I’ve tried to put it this way but it has an error on the custom keyword method upon running the script.

@Keyword
public void setBrowser(String browser) {
if (browser.equalsIgnoreCase(‘Firefox’)) {
WebUI.uploadFile(findTestObject(‘Object 1’), ‘C:\\Desktop\\ABC.txt’)
Thread.sleep(1000)
Runtime.getRuntime().exec(‘C:\\Desktop\\AutoIt\\UploadDataFile.exe’)
}

else (browser.equalsIgnoreCase('Chrome')) {  
    WebUI.uploadFile(findTestObject('Object 1'), 'C:\\\Desktop\\\ABC.txt')  
    WebUI.sendKeys(findTestObject('Object 2'), Keys.chord(Keys.TAB, Keys.ENTER))  
    Thread.sleep(1000)  
    Runtime.getRuntime().exec('C:\\\Desktop\\\AutoIt\\\UploadDataFile.exe')  
}  

}

In the script, I put like this:

CustomKeywords.‘test.HeaderLine.setBrowser’(‘Object 1’)

Error message:

09-03-2018 03:43:45 PM - [ERROR] - groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@76304b46]
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), and(java.lang.Boolean), any(groovy.lang.Closure)
09-03-2018 03:43:45 PM - [END] - End action : test.HeaderLine.setBrowser
09-03-2018 03:43:45 PM - [END] - End Test Case : Test Cases/Create Preference
09-03-2018 03:43:45 PM - [ERROR] - Test Cases/Create Preference FAILED because (of) org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@76304b46]
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), and(java.lang.Boolean), any(groovy.lang.Closure)

Hi Jacqueline,

CustomKeywords.‘test.HeaderLine.setBrowser’(‘Object 1’)

Shouldn’t “Object 1” be the name of the browser? (“Firefox” or “Chrome”).

I’m also not clear why Groovy thinks you’re passing a closure, but maybe that’s benign at this point.

Hi Russ,

Thanks for your reply. I’ve changed the value to the name of the browser. The script runs successfully for “Firefox” browser but not “Chrome” browser. Could you please advise on the error below:. Thanks.

09-05-2018 11:48:12 AM - [ERROR] - groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@5eefa415]
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), and(java.lang.Boolean)
09-05-2018 11:48:12 AM - [ERROR] - Test Cases/Create Preference FAILED because (of) org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@5eefa415]
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), and(java.lang.Boolean)

1 Like

That’s the same error, more or less. It appears the “if” is being passed something it does not understand (perhaps a closure). If you pass a string to your keyword, you should not be seeing that particular error.

It would help if I could see more context - post more lines leading up to the error.

09-07-2018 11:50:44 AM - [INFO] - Uploading file ‘C:\Users\jacqueline.lin\Desktop\With Header.txt’ to object: ‘Object Repository/MP/button_Upload’
09-07-2018 11:50:44 AM - [INFO] - Checking timeout
09-07-2018 11:50:44 AM - [INFO] - Finding web element with id: ‘Object Repository/MP/button_Upload’ located by ‘By.xpath: //[@id=“btnUploadFileConfirm”]/input’ in ‘30’ second(s)
09-07-2018 11:50:44 AM - [INFO] - Found 1 web elements with id: ‘Object Repository/MP/button_Upload’ located by 'By.xpath: //
[@id=“btnUploadFileConfirm”]/input’ in ‘30’ second(s)

09-07-2018 11:50:45 AM - [PASSED] - File ‘C:\Users\gbs.jacqueline.lin\Desktop\With Header.txt’ sent to object: ‘Object Repository/MP/button_Upload’

09-07-2018 11:50:45 AM - [END] - End action : uploadFile
09-07-2018 11:50:45 AM - [START] - Start action : Statement - java.lang.Thread.sleep(1000)
09-07-2018 11:50:46 AM - [END] - End action : Statement - java.lang.Thread.sleep(1000)
09-07-2018 11:50:46 AM - [START] - Start action : test.HeaderLine.setBrowser
09-07-2018 11:50:46 AM - [END] - End action : test.HeaderLine.setBrowser
09-07-2018 11:50:46 AM - [ERROR] - groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@1d8e2eea]
Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), each(groovy.lang.Closure), and(java.lang.Boolean)
09-07-2018 11:50:46 AM - [ERROR] - Test Cases/Create Preference FAILED because (of) (Stack trace: org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@1d8e2eea]
Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), each(groovy.lang.Closure), and(java.lang.Boolean)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:100)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:812)
at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:144)
at test.HeaderLine.invokeMethod(HeaderLine.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script1523844497261.run(Script1523844497261.groovy:108)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1536292192360.run(TempTestCase1536292192360.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@1d8e2eea]
Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), each(groovy.lang.Closure), and(java.lang.Boolean)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at test.HeaderLine.setBrowser(HeaderLine.groovy:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
… 40 more
(caused by: (Stack trace: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (test.HeaderLine$_setBrowser_closure1) values: [test.HeaderLine$_setBrowser_closure1@1d8e2eea]
Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), each(groovy.lang.Closure), and(java.lang.Boolean)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at test.HeaderLine.setBrowser(HeaderLine.groovy:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:812)
at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:144)
at test.HeaderLine.invokeMethod(HeaderLine.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script1523844497261.run(Script1523844497261.groovy:108)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseM09-07-2018 11:50:46 AM - [END] - End Test Case : Test Cases/Create Preference
ain.java:90)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1536292192360.run(TempTestCase1536292192360.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
)))

This is your keyword starting execution…

09-07-2018 11:50:46 AM - [START] - Start action : test.HeaderLine.setBrowser

This is your keyword ending execution…

09-07-2018 11:50:46 AM - [END] - End action : test.HeaderLine.setBrowser

Then…

09-07-2018 11:50:46 AM - [ERROR] - groovy.lang.MissingMethodException: …

So what is the next line AFTER the call to your keyword in you Test Case?

I managed to solve the issue by using SendKeys method instead of UploadFile which is not working after I installed the latest version of Katalon Studio.

My steps are:

1. SendKeys method

2. Run the AutoIT script