Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it with casting. Execute JavaScript to return an array. The JS array must contain only one type, which must be primitive. </p> <p>For example, execute a script which returns an array of Strings:</p> <pre><code>ArrayList&lt;String&gt; strings = (ArrayList&lt;String&gt;) js.executeScript(returnArrayOfStrings); </code></pre> <p>If you need an array of any other type, you can build it from those strings. For example, if you need an array of WebElements, design your JS to return locators, and then iterate through, finding elements and building a new array:</p> <pre><code> ArrayList&lt;String&gt; xpaths = (ArrayList&lt;String&gt;) js.executeScript(getLocators); ArrayList&lt;WebElement&gt; elements = new ArrayList&lt;WebElement&gt;(); for (String xpath: xpaths){ element = driver.findElement(By.xpath(xpath)); elements.add(element); } </code></pre> <p>You have the <code>Array</code> in Java, so you can keep it in memory when your tests go to different pages and still reference the Java <code>Array</code>,</p> <p>The only catch is, if your JS array is changing on the client side, your Java <code>Array</code> won't automatically update itself (the <code>jsexecuter</code> only returns once per execution), but that's not a big deal - instead of referencing the Java <code>Array</code> directly, you can access it via a getter which first executes the JS again to get a new <code>Array</code>, which you can use to replace the previous one, or merge them etc, before returning the new/updated array to your test code. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload