Try this:
public class Highlight {
/**
* Highlight an element for five seconds using JavaScript.
* @param selector (String) The CSS selector for the element.
*/
@Keyword
public static void highlightElement(String selector) {
// Triple-quoted string containing JavaScript
String js = '''
function highlightElement(selector) {
$(selector).css("box-shadow", "1px 1px 4px 4px yellow, -1px -1px 4px 4px yellow");
setTimeout(function() {
$(selector).css("box-shadow", "none");
}, 5000);
}
highlightElement(selector);
'''
// execute the JavaScript
WebUI.executeJavaScript(js, Arrays.asList(selector))
}
}
Notes:
- You don’t need to use
@Keywordif you never use Manual View. - I believe a method called
runhas special meaning in script classes - I changed it tohighlightElementand named the classHighlight. - This method does not need Webdriver. It uses JavaScript to directly target the element using a suitable CSS selector.
- Note the JavaScript is a multi-line string surrounded by triple quotes -
''' and '''
Example call:
import static package_name.Highlight.*
//highlight the element with id="username"
highlightElement("#username")