I have the following Java code:
public void nextElement()
{
try
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
System.out.println("Executing...");
JavascriptExecutor js = (JavascriptExecutor) driver;
String input_tag = "input";
js.executeScript(createHTML(input_tag));
System.out.println("Completed execution..");
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
I now need the createHTML() method to accept the value of the variable input_tag and pass the same to a JavaScript method embedded within the createHTML() Java method.
The createHTML() method looks a little like this:
public String createHTML(String tag) //"tag" accepts value from "input_tag"
{
String temp = "";
temp += "function test(javascript_tagname)"; //"javascript_tagname" should be the value passed in the Java variable "tag"
temp += "{ ";
temp += " var x = document.getElementsByTagName('javascript_tagname');";
temp += " var i = 0;";
temp += " for (var i=0; i<x.length; i++)";
temp += " {";
temp += " x[i].onclick = function()";
temp += " {";
temp += " var previousStyle = this.style.getAttribute('cssText');";
-----------------
-----------------
}
So what do I need to specify that the argument javascript_tagname in the function test() is actually supposed to extract value from the passed Java argument "tag"?
I know it must be a wee-bit confusing. Kindly let me know for any clarifications.
Any help appreciated! :) :)
.jsfile, load the js file into theJavascriptExecutor, and then do something likejs.executeScript(String.format("test(%s);", escape(input_tag)))to invoke it?