Selenium会等待JavaScript完成吗?

ect*_*n84 7 javascript java selenium

我正在使用Java/Selenium测试JavaScript API.

我在Java端发出这些命令,

executor.executeScript("setName('Hello');");
// Thread.sleep(2000);
String output = (String)executor.executeScript("return getName()");
assertEquals(output, "Hello");
Run Code Online (Sandbox Code Playgroud)

在JavaScript方面,这是一个异步函数,因此它需要一些时间并设置变量.

function setName(msg) {
    // simulate async call 
    setName(function(){name = msg;},1000);
}
Run Code Online (Sandbox Code Playgroud)

我需要等待这个异步函数完成,然后再转到Java assertEquals()执行的下一行.

如果不在Thread.sleep()Java端使用,有没有办法实现这一点.

谢谢

Dan*_*tin 2

您可以轻松地要求 Selenium 等待,直到特定条件成立;就你所拥有的而言,一种替代方案是:

new FluentWait<JavascriptExecutor>(executor) {
  protected RuntimeException timeoutException(
      String message, Throwable lastException) {
    Assert.fail("name was never set");
  }
}.withTimeout(10, SECONDS)
.until(new Predicate<JavascriptExecutor>() {
  public boolean apply(JavascriptExecutor e) {
    return (Boolean)executor.executeScript("return ('Hello' === getName());");
  }
});
Run Code Online (Sandbox Code Playgroud)

但是,您基本上是在准确测试刚刚编码的内容,这有一个缺点,即如果在name调用之前设置setName,则不一定要等待setName完成。我过去为类似的事情做过的一件事是:

在我的测试库(用setTimeout垫片替换真正的异步调用)中,我有这个:

window._junit_testid_ = '*none*';
window._junit_async_calls_ = {};
function _setJunitTestid_(testId) {
  window._junit_testid_ = testId;
}
function _setTimeout_(cont, timeout) {
  var callId = Math.random().toString(36).substr(2);
  var testId = window._junit_testid_;
  window._junit_async_calls_[testId] |= {};
  window._junit_async_calls_[testId][callId] = 1;
  window.setTimeout(function(){
    cont();
    delete(window._junit_async_calls_[testId][callId]);
  }, timeout);
}
function _isTestDone_(testId) {
  if (window._junit_async_calls_[testId]) {
    var thing = window._junit_async_calls_[testId];
    for (var prop in thing) {
      if (thing.hasOwnProperty(prop)) return false;
    }
    delete(window._junit_async_calls_[testId]);
  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)

在我的库的其余部分中,每当我需要设置稍后发生的事情时,我都会使用_setTimeout_而不是。window.setTimeout然后,在我的硒测试中,我做了这样的事情:

// First, this routine is in a library somewhere
public void waitForTest(JavascriptExecutor executor, String testId) {
  new FluentWait<JavascriptExecutor>(executor) {
    protected RuntimeException timeoutException(
        String message, Throwable lastException) {
      Assert.fail(testId + " did not finish async calls");
    }
  }.withTimeout(10, SECONDS)
  .until(new Predicate<JavascriptExecutor>() {
    public boolean apply(JavascriptExecutor e) {
      return (Boolean)executor.executeScript(
          "_isTestDone_('" + testId + "');");
    }
  });
}

// Inside an actual test:
@Test public void serverPingTest() {
  // Do stuff to grab my WebDriver instance
  // Do this before any interaction with the app
  driver.executeScript("_setJunitTestid_('MainAppTest.serverPingTest');");
  // Do other stuff including things that fire off what would be async calls
  // but now call stuff in my testing library instead.
  // ...
  // Now I need to wait for all the async stuff to finish:
  waitForTest(driver, "MainAppTest.serverPingTest");
  // Now query stuff about the app, assert things if needed
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果需要,您可以waitForTest多次调用,任何时候您需要暂停测试,直到所有异步操作完成。