最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Selenium - Wait until returned javascript script value match value - Stack Overflow

programmeradmin2浏览0评论

I need to wait until the result of a javascript match a string or a boolean value.

With this javascript:

document.getElementById('video_html5').seeking;

I get a "false" / "true" value, and i need to wait until the value is "false", so I'm sure that the video is not seeking, but I only found the way to wait for a javascript mand value and not the way to check that the value matches some text.

new WebDriverWait(driver, 30)
    .until(ExpectedConditions.jsReturnsValue("return document.getElementById('video_html5').seeking;"));

Because in some cases I get a string other than boolean and need to pare those strings.

I have found how to do it in Ruby but not in Java:

wait_element = @wait.until { @driver.execute_script("return document.getElementById('vid1_html5_api_Shaka_api').seeking;").eql? false }

I need to wait until the result of a javascript match a string or a boolean value.

With this javascript:

document.getElementById('video_html5').seeking;

I get a "false" / "true" value, and i need to wait until the value is "false", so I'm sure that the video is not seeking, but I only found the way to wait for a javascript mand value and not the way to check that the value matches some text.

new WebDriverWait(driver, 30)
    .until(ExpectedConditions.jsReturnsValue("return document.getElementById('video_html5').seeking;"));

Because in some cases I get a string other than boolean and need to pare those strings.

I have found how to do it in Ruby but not in Java:

wait_element = @wait.until { @driver.execute_script("return document.getElementById('vid1_html5_api_Shaka_api').seeking;").eql? false }
Share Improve this question asked Jan 9, 2017 at 18:33 Lea2501Lea2501 3618 silver badges25 bronze badges 1
  • Would this work? jsReturnsValue("document.getElementById('video_html5').seeking === 'false' ? 'true' : undefined;") It'll pare the value directly in the JavaScript, returning undefined if seeking is true. – alexhughesking Commented Jan 9, 2017 at 18:54
Add a ment  | 

4 Answers 4

Reset to default 6

You can write your own custom expected conditions.

public class MyCustomConditions {

  public static ExpectedCondition<Boolean> myCustomCondition() {
    return new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
        return (Boolean) ((JavascriptExecutor) driver)
          .executeScript("return document.getElementById('video_html5').seeking === 'string_value' || ... ");
      }
    };
  }
}

And then in your tests you can use the condition as follows.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(MyCustomConditions.myCustomCondition());

A generic way to do it (where mand is the JavaScript you want to run with webdriver in timeout seconds):

public Object executeScriptAndWaitOutput(WebDriver driver, long timeout, final String mand) {
  WebDriverWait wait = new WebDriverWait(driver, timeout);
  wait.withMessage("Timeout executing script: " + mand);

  final Object[] out = new Object[1];
  wait.until(new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver d) {
      try {
        out[0] = executeScript(mand);
      } catch (WebDriverException we) {
        log.warn("Exception executing script", we);
        out[0] = null;
      }
      return out[0] != null;
    }
  });
  return out[0];
}

Finally i did the following and worked, getting ideas from different answers:

public Boolean checkStateSeeking() throws InterruptedException {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        Boolean seekingState = (Boolean) js
                .executeScript("return document.getElementById('vid1_html5').seeking;");

        while (seekingState) {
            // System.out.println(seekingState);
            seekingState = (Boolean) js
                    .executeScript("return document.getElementById('vid1_html5').seeking;");
        }

        return seekingState;
    }

Getting ideas for all the answers I create a function to wait for an element to change each and waiting N seconds, using the xpath of the element and the expected value.

 public static String checkValueChanged(WebDriver driver, String expectedValue, String path, int waitTime) throws InterruptedException {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String elementValue = (String) js
            .executeScript("return document.evaluate(\""+path+"\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;");

    while (!(elementValue.equals(expectedValue)) && (waitTime>0)) {
        Thread.sleep(1000);
        waitTime--;
        elementValue = (String) js
                .executeScript("return document.evaluate(\""+path+"\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;");
    }
    return elementValue;
}
发布评论

评论列表(0)

  1. 暂无评论