Selenium:执行javascript命令“return document.readyState”时测试冻结

时间:2015-08-03 18:05:35

标签: javascript java selenium selenium-webdriver automation

Selenium 2.47.1。 Firefox 39.0。 Java 1.8

有时在测试期间打开新页面时,favicon附近的微调器会无限显示(这是测试应用程序的错误)。

如果页面在一分钟后未加载,我使用等待页面加载或刷新页面的方法。

在大多数情况下,页面在 10-15秒之后加载,并且该方法不是必需的。在其他情况下,此方法成功运行,并在一分钟后刷新页面。

但有时(当微调器没有消失时)测试在执行此命令时冻结

((JavascriptExecutor) d).executeScript("return document['readyState'] ? 'complete' == document.readyState : true")

这是等待

的完整方法
public void waitForPageToLoad(){
  started = System.currentTimeMillis();
WebDriverWait wait = new WebDriverWait(getDriver(), Long.parseLong(getTimeoutForPageLoad()));
  try {
      wait.until(new ExpectedCondition<Boolean>() {
          public Boolean apply(final WebDriver d) {
              if (!(d instanceof JavascriptExecutor)) {
                  return true;
              }
              Object result = ((JavascriptExecutor) d).executeScript("return document['readyState'] ? 'complete' == document.readyState : true");
              if (result != null && result instanceof Boolean && (Boolean) result) {
                long now = System.currentTimeMillis();
                if (now - started > Long.parseLong(getTimeoutForPageLoad())) {
                  return true;
                }else {
                  started = System.currentTimeMillis();
                }
              }
              return false;
          }
      });
  } catch (Exception e) {
    refresh();
    waitForPageToLoad();
  }
}

1 个答案:

答案 0 :(得分:0)

我发现只有一个解决方案:添加等待50秒的线程,如果waitForPageToLoad()冻结,则刷新页面

class NewThread extends Thread {
Thread thread;
boolean isRunning = true;

NewThread() {
  thread = new Thread(this, "new thread");
  System.out.println("2nd thread is created " + thread);
  thread.start();
}

public void setIsRunning(boolean isRunning){
  this.isRunning = isRunning;
}

public boolean getIsRunning(){
  return isRunning;
}

public void run() {
  try {
      Thread.sleep(50000);
    if (getIsRunning()) {
      Logger.getInstance().info("refresh using thread");
      Browser.getInstance().refresh();
      waitForPageToLoad();
    }
  } catch (InterruptedException e) {
    System.out.println("2nd thread is interrupted");
  }
  System.out.println("2nd thread is ended");
}
}

public void waitForPageToLoad(){
NewThread thread1 = new NewThread();
  Logger.getInstance().info("waitForPageToLoad started");
  started = System.currentTimeMillis();
WebDriverWait wait = new WebDriverWait(getDriver(), Long.parseLong(getTimeoutForPageLoad()));
  try {
      wait.until(new ExpectedCondition<Boolean>() {
          public Boolean apply(final WebDriver d) {
              if (!(d instanceof JavascriptExecutor)) {
                  return true;
              }
              Object result = (Boolean) ((JavascriptExecutor) d).executeScript("return document.readyState == 'complete'");
              if (result != null && result instanceof Boolean && (Boolean) result) {
                long now = System.currentTimeMillis();
                if (now - started > Long.parseLong(getTimeoutForPageLoad())) {
                  return true;
                }else {
                  started = System.currentTimeMillis();
                }
              }
              return false;
          }
      });
  } catch (Exception e) {
    refresh();
    waitForPageToLoad();
  }
  Logger.getInstance().info("waitForPageToLoad ended");
thread1.setIsRunning(false);
}