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! :) :)
3 Answers 3
If you want to bind (hardcode) the value of a variable to script source the other 2 answers are the way to go. You will still have to call the function.
If you want to call the function and pass the argument don't hardcode the parameter in the script and just use invokeFunction method passing as arguments the name of the script function and list of arguments for the functions defined in script:
engine.eval(createHTML());
Invocable invocableEngine = (Invocable) engine;
invocableEngine.invokeFunction("test", "javascript_tagname" );
not sure what you want to do but a short working example:
public void nextElement()
{
try
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
System.out.println("Executing...");
String input_tag = "input";
engine.eval(createHTML(""));
Invocable invocableEngine = (Invocable) engine;
invocableEngine.invokeFunction("test", "javascript_tagname_param" );
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public String createHTML(String tag) //"tag" is not used
{
String temp = "";
temp += "function test(javascript_tagname)"; //"javascript_tagname" should be the value passed in the Java variable "tag"
temp += "{ ";
temp += " println(javascript_tagname);";
temp += "};";
return temp;
}
output:
Executing...
javascript_tagname_param
8 Comments
I think what you want is more like this
public String createHTML(String tag) //"tag" accepts value from "input_tag"
{
String temp = "";
temp += "function test()";
temp += "{ ";
temp += "var javascript_tagname = \"" + jsonencode(tag) + "\";";
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');";
}
But the better solution is to see if there's a way to pass it in executeScript's varargs array, then pick it up in the script.
Comments
As you create the JavaScript-code from scratch out of a String, why don't you simply replace it with the parameter?
String temp = "";
temp += "function test()";
temp += "{ ";
temp += " var x = document.getElementsByTagName('" + tag + "');";
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');";
.jsfile, load the js file into theJavascriptExecutor, and then do something likejs.executeScript(String.format("test(%s);", escape(input_tag)))to invoke it?