0

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! :) :)

6
  • I'm not familiar with the library you're using, but how about you move that javascript function into a .js file, load the js file into the JavascriptExecutor, and then do something like js.executeScript(String.format("test(%s);", escape(input_tag))) to invoke it? Commented May 20, 2014 at 6:43
  • Is your script just declaring the function, or is is actually invoking it? Commented May 20, 2014 at 6:45
  • @Chris Martin: I would not want to use an external .js file :) Commented May 20, 2014 at 7:30
  • @Maurice Perry: I don't invoke the function test() directly. I only call the function createHTML(). Commented May 20, 2014 at 7:31
  • Why in the world not? You could load it from the classpath. Commented May 20, 2014 at 7:33

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, sounds like the way to go
@user1121883: I may sound naive, but where do these lines go? In the method nextElement()?
Yes: they replace js.executeScript(createHTML(input_tag));
@user3571573 instead of js.executeScript(createHTML(input_tag)); and input_tag is "javascript_tagname"
@user1121883 : Thanks for the explanation :) But I would need a little more clarification. The blogs and tutorials that I came across mention that the parameter in the engine.eval() method is always a String type declared & assigned a value in the same class. But in my case, I would need the engine.eval() to call another Java method and then invoke the test() method with a parameter. Any help with this? I'm kinda lost! :(
|
0

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

0

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');";

2 Comments

In the line temp += " var x = document.getElementsByTagName('" + tag + "');"; I suppose the parameter still remains unknown to the function test, right?
The function won't neither have nor need any parameter afterwards, because you hard-code the value from Java into JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.