2

Here is usual javascript, which looks for hidden element.

   private RemoteWebElement getItemInSubmenu(WebElement parent, String itemName) {
        String script = "var el = jQuery(arguments[0].getElementsBySelector('div.rich-menu-list-bg>div.rich-menu-item>.rich-menu-item-label')).filter(function() {"
                + "return jQuery(this).text().trim() === '" + itemName + "'});"
                + "jQuery(el).parents('div.rich-menu-list-border:hide').show();"
                + "return el.get(0);";
        return (RemoteWebElement) browser.executeScript(script, parent);
    }

I want to make it asynchronous. How to implement callback?

10
  • What are you trying to do? Return an element with JavaScript? Any reason you can't get the WebElement directly? Commented Apr 16, 2013 at 11:47
  • And the code above is Java, the JavaScript you are seeing is the value of a Java String variable. Commented Apr 16, 2013 at 11:49
  • @NilsH, elements are not visible. I can not get their text. Commented Apr 16, 2013 at 11:51
  • @Ardesco Yes, indeed, but there's also some JavaScript in there that is executed, as you can see. Commented Apr 16, 2013 at 11:52
  • @ArtemYakovlev Where's the callback? If you need the text of the element, can't you return that from the script instead of the element? Commented Apr 16, 2013 at 11:56

1 Answer 1

1

These are pulled directly from the Selenium JavaDoc which is available here:

http://selenium.googlecode.com/git/docs/api/java/index.html

Example #1: Performing a sleep in the browser under test.

long start = System.currentTimeMillis();
((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
System.out.println("Elapsed time: " + System.currentTimeMillis() - start);

Example #2: Synchronizing a test with an AJAX application

WebElement composeButton = driver.findElement(By.id("compose-button"));
composeButton.click();
((JavascriptExecutor) driver).executeAsyncScript(
  "var callback = arguments[arguments.length - 1];" +
  "mailClient.getComposeWindowWidget().onload(callback);");
driver.switchTo().frame("composeWidget");
driver.findElement(By.id("to")).sendKeys("[email protected]");

Example #3: Injecting a XMLHttpRequest and waiting for the result:

Object response = ((JavascriptExecutor) driver).executeAsyncScript(
    "var callback = arguments[arguments.length - 1];" +
    "var xhr = new XMLHttpRequest();" +
    "xhr.open('GET', '/resource/data.json', true);" +
    "xhr.onreadystatechange = function() {" +
    "  if (xhr.readyState == 4) {" +
    "    callback(xhr.responseText);" +
    "  }" +
    "}" +
    "xhr.send();");
JSONObject json = new JSONObject((String) response);
assertEquals("cheese", json.getString("food"));
Sign up to request clarification or add additional context in comments.

Comments

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.