4

I need to get an empty array declared in a JavaScript script in a Java variable.

The JavaScript array is declared as so on a web page:

tc_vars["products"] = new Array();

(I only need the products's array, not the tc_vars one).

This is how I try to get this variable in Java :

Object[] value = (Object[]) js.executeScript("return tc_vars['products'];");

(Where js is a JavascriptExecutor associated with a Selenium WebDriver).

I usually have no trouble using this method for getting Strings, but it seems it just won't work for an empty Array. Moreover, I don't have any error message, the WebDriver just crashes.

The executeScript method returns an Object variable. Casting it to a String (when the JS variable is a String) works well, but I had no chance in casting this empty Array to String[] or Object[].

7
  • Be careful how you title your question: you don't want to get it "in Java" (generic), you want to get it in a Selenium test which is actually a very rare case where it is a valid requirement to want to do this. I'd edit that if I were you, the way you put it now is a big lure for people to not read the question and just defensively vote it down.
    – Gimby
    Commented Aug 1, 2014 at 9:50
  • As for the actual question itself: I don't want to jump the gun but this smells like a bug in the driver. I'm assuming there is an issue tracker for it somewhere, did you check if this is perhaps a known bug?
    – Gimby
    Commented Aug 1, 2014 at 9:52
  • @Gimby Thank you for your advice, I edited it. Whereas the behavior of the Driver seems buggy, it actually always crashes when an runtime error occurs. I don't know how an empty Javascript Array would be interpreted by Java.
    – Seeven
    Commented Aug 1, 2014 at 10:00
  • 1
    You can do JSON.stringify in JavaScript side and work on JSON deserialization in Java side.
    – sap1ens
    Commented Aug 2, 2014 at 5:00
  • @sap1ens Thanks for the tip, it will be useful in the future. About my problem, I can't work on it at the moment, but I will try your idea as soon as I can.
    – Seeven
    Commented Aug 13, 2014 at 8:49

1 Answer 1

2

As I mentioned in my comment, you need to make sure you actually have access to tc_vars['products'] at the point which you want to test. E.g., manually open a browser, get to the point in the page where you would ask for that value using selenium, open devtools javascript console and check the value of tc_vars. If it's not available to you, it will not be available to the selenium webdriver.

Second thing is that for arrays, the selenium webdriver (at least the latest version: 2.42.2) returns ArrayList<Object> and not Object[]. so your cast should be:

ArrayList<Object> o = (ArrayList<Object>) js.executeScript("return tc_vars['products'];");
1
  • Thanks! It works great. As I answered to your comment, I can access the tc_vars array from a browser's dev-tools console. I used your example with an ArrayList<String> and I'm able to work with Javascript arrays with Java now.
    – Seeven
    Commented Aug 19, 2014 at 14:36

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.