1 package com.github.searls.jasmine.runner; 2 3 import com.google.common.base.Predicate; 4 import org.apache.maven.plugin.logging.Log; 5 import org.openqa.selenium.JavascriptExecutor; 6 import org.openqa.selenium.TimeoutException; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.support.ui.WebDriverWait; 9 10 class WebDriverWaiter { 11 static final String EXECUTION_FINISHED_SCRIPT = "return (window.jsApiReporter === undefined) ? false : window.jsApiReporter.finished"; 12 13 void waitForRunnerToFinish(final WebDriver driver, final int timeout, final boolean debug, final Log log) throws InterruptedException { 14 final JavascriptExecutor executor = (JavascriptExecutor) driver; 15 try { 16 new WebDriverWait(driver, timeout, 1000).until(new Predicate<WebDriver>() { 17 @Override 18 public boolean apply(WebDriver input) { 19 return executionFinished(executor); 20 } 21 }); 22 } catch (TimeoutException e) { 23 handleTimeout(timeout, debug, log); 24 } 25 26 } 27 28 private Boolean executionFinished(final JavascriptExecutor driver) { 29 return (Boolean) driver.executeScript(EXECUTION_FINISHED_SCRIPT); 30 } 31 32 private void handleTimeout(final int timeout, final boolean debug, final Log log) { 33 log.warn("Attempted to wait for your specs to finish processing over the course of " + 34 timeout + 35 " seconds, but it still appears to be running."); 36 if (debug) { 37 log.warn("Debug mode: will attempt to parse the incomplete spec runner results"); 38 } else { 39 throw new IllegalStateException("Timeout occurred. Aborting execution of specs. (Try configuring 'debug' to 'true' for more details.)"); 40 } 41 } 42 43 }