Hello everyone,
I tried to create a method to highlight a text (String) on the current page. I got inspired using the above code but an error occurred when I tried to run it.
The code is the following:
public class HighlightText {
static WebDriver driver = DriverFactory.getWebDriver()
@Keyword
public static void highlightText(String text){
boolean textExist = WebUiCommonHelper.isTextPresent(driver, text, true);
try {
if(textExist == true){
for (int i = 0; i < 5; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver
js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 2px solid red;');",
text)
}
}
} catch (Exception e) {
Assert.fail(e.getMessage())
}
}
}
I got he following error.
com.methods.HighlightText.highlightText("Submitted") FAILED.
Reason:
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.AssertionError: unknown error: arguments[0].setAttribute is not a function
os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181'
Driver info: com.kms.katalon.selenium.driver.CChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.43.600210 (68dcf5eebde371..., userDataDir: C:\Users\WANGHO~1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:52561}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 73.0.3683.103, webStorageEnabled: true}
Session ID: 9da1472f4acba4a20495ebe6e620d6ab
at com.methods.HighlightText.invokeMethod(Methods.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)
at Portal Auto-Approve Normal Case.run(Portal Auto-Approve Normal Case:148)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:129)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:112)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:81)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:149)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1555093574563.run(TempTestSuite1555093574563.groovy:36)
Caused by: java.lang.AssertionError: unknown error: arguments[0].setAttribute is not a function
(Session info: chrome=73.0.3683.103)
(Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:07:36.161Z'
System info: host: 'QCMONTC661582P', ip: '10.135.45.32', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181'
Driver info: com.kms.katalon.selenium.driver.CChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.43.600210 (68dcf5eebde371..., userDataDir: C:\Users\WANGHO~1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:52561}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 73.0.3683.103, webStorageEnabled: true}
Session ID: 9da1472f4acba4a20495ebe6e620d6ab
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert$fail.call(Unknown Source)
at com.methods.HighlightText.highlightText(Methods.groovy:70)
at com.methods.HighlightText.invokeMethod(Methods.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)
at Script1554317572990.run(Script1554317572990.groovy:148)
... 13 more
I read online that "arguments[0].setAttribute()"
can only be used with WebElement objects. I tried looking up online to see how i could use it for String variables, but to no success.
Any help would be appreciated!