How to add IE custom keyword in my test case

Hello,

I’ve created a custom keyword for IE as below, but I’m getting the below error message, when I execute the test case in IE.

@Keyword

def static ie(def desiredCapabilities) {

DesiredCapabilities dc

WebUIDriverType executedBrowser = DriverFactory.getExecutedBrowser()

switch (executedBrowser) {

case WebUIDriverType.IE_DRIVER:

System.setProperty(“webdriver.ie.driver”, DriverFactory.getIEDriverPath())

for (item in desiredCapabilities) {

logger.logInfo(“Add " + item + " to IE”)

dc.setCapability(item.key, item.value)

}

dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true)

dc.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true)

dc.setCapability(“ignoreProtectedModeSettings”, true)

WebDriver driver = new InternetExplorerDriver(dc)

//Change current browser’s driver using new desired capabilities

DriverFactory.changeWebDriver(driver)

break

}

}

TEST CASE:

CustomKeywords.‘com.example.WebUICustomKeywords.ie’(null)

WebUI.navigateToUrl(GlobalVariable.G_SiteURL)

LOGS:

Test Cases/Common Test Cases/Login_Page_Open_Browser FAILED because (of) java.lang.NullPointerException: Cannot invoke method setCapability() on null object

com.example.WebUICustomKeywords.ie:112

com.example.WebUICustomKeywords.invokeMethod:0

Test Cases/Common Test Cases/Login_Page_Open_Browser.run:27

Could you please some one help me in order to execute my custom keyword in my test with above configurations?

I found the above code in internet but I do not understand exactly what will be the object and how I can use this keyword in my test cases.

Thanks in advance for your help.

Regards

Sreenivasulu,

I found the above code in internet

Could you share the URL?

Hi Kazurayam,

Please find the url below from where i got the sample code.

kazurayam said:

Sreenivasulu,

I found the above code in internet

Could you share the URL?

Hi Kazurayam,

Please find the url below from where i got the sample code.

https://github.com/katalon-studio-samples/tips-and-tricks/blob/master/Keywords/com/common/utils/SetDesiredCapability.groovy

The line 60 is definitely buggy.

        DesiredCapabilities dc

The dc variable is left null. This is the reason why you got the log message:

Test Cases/Common Test Cases/Login_Page_Open_Browser FAILED because (of) java.lang.NullPointerException: Cannot invoke method setCapability() on null object

kazurayam said:

The line 60 is definitely buggy.

        DesiredCapabilities dc

The dc variable is left null. This is the reason why you got the log message:

Test Cases/Common Test Cases/Login_Page_Open_Browser FAILED because (of) java.lang.NullPointerException: Cannot invoke method setCapability() on null object

Where should be declared this variable ? Is it in keyword class ? Or in test case ? Can you please give me some idea because im very new in programming.

Sreenu said:

kazurayam said:

The line 60 is definitely buggy.

        DesiredCapabilities dc

The dc variable is left null. This is the reason why you got the log message:

Test Cases/Common Test Cases/Login_Page_Open_Browser FAILED because (of) java.lang.NullPointerException: Cannot invoke method setCapability() on null object

Where should be declared this variable ? Is it in keyword class ? Or in test case ? Can you please give me some idea because im very new in programming.

I managed to resolve the problem. Added below line in custom keyword:

dc = DesiredCapabilities.internetExplorer()

Then, added the below line in the test case:

CustomKeywords.‘com.example.WebUICustomKeywords.ie’(‘’)

Thanks