1 package com.github.searls.jasmine.runner; 2 3 import com.github.searls.jasmine.io.FileUtilsWrapper; 4 import com.github.searls.jasmine.model.FileSystemReporter; 5 import com.github.searls.jasmine.model.JasmineResult; 6 import com.github.searls.jasmine.model.Reporter; 7 import org.apache.maven.plugin.logging.Log; 8 import org.openqa.selenium.JavascriptExecutor; 9 import org.openqa.selenium.WebDriver; 10 11 import java.io.File; 12 import java.io.IOException; 13 import java.net.URL; 14 import java.util.List; 15 16 public class SpecRunnerExecutor { 17 18 private final FileUtilsWrapper fileUtilsWrapper; 19 private final WebDriverWaiter webDriverWaiter; 20 private final ConsoleErrorChecker consoleErrorChecker; 21 22 public SpecRunnerExecutor(FileUtilsWrapper fileUtilsWrapper, WebDriverWaiter webDriverWaiter, ConsoleErrorChecker consoleErrorChecker) { 23 this.fileUtilsWrapper = fileUtilsWrapper; 24 this.webDriverWaiter = webDriverWaiter; 25 this.consoleErrorChecker = consoleErrorChecker; 26 } 27 28 public SpecRunnerExecutor() { 29 this(new FileUtilsWrapper(), new WebDriverWaiter(), new ConsoleErrorChecker()); 30 } 31 32 33 public JasmineResult execute(final URL runnerUrl, final WebDriver driver, final int timeout, final boolean debug, final Log log, final String format, final List<Reporter> reporters, final List<FileSystemReporter> fileSystemReporters) { 34 try { 35 if (!(driver instanceof JavascriptExecutor)) { 36 throw new RuntimeException("The provided web driver can't execute JavaScript: " + driver.getClass()); 37 } 38 JavascriptExecutor executor = (JavascriptExecutor) driver; 39 driver.get(runnerUrl.toString()); 40 webDriverWaiter.waitForRunnerToFinish(driver, timeout, debug, log); 41 42 consoleErrorChecker.checkForConsoleErrors(driver, log); 43 44 storeFileSystemReports(fileSystemReporters, executor, debug); 45 46 JasmineResult jasmineResult = new JasmineResult(); 47 jasmineResult.setDetails(buildReports(reporters, executor, format)); 48 return jasmineResult; 49 50 } catch (Exception e) { 51 throw new RuntimeException(e); 52 } finally { 53 try { 54 driver.quit(); 55 } catch (Exception e) { 56 log.error("There was an exception quitting WebDriver.", e); 57 } 58 } 59 } 60 61 private void storeFileSystemReports(final List<FileSystemReporter> fileSystemReporters, final JavascriptExecutor executor, final boolean debug) throws IOException { 62 for (FileSystemReporter reporter : fileSystemReporters) { 63 fileUtilsWrapper.writeStringToFile(reporter.file, this.buildFileSystemReport(executor, reporter.reporterFile, debug)); 64 } 65 } 66 67 private String buildFileSystemReport(final JavascriptExecutor driver, final File reporter, final boolean debug) throws IOException { 68 final String command = "return fileSystemReporter.report(window.jsApiReporter," + debug + ");"; 69 return executeReportCommand(driver, reporter, command); 70 } 71 72 private String buildReports(final List<Reporter> reporters, final JavascriptExecutor executor, final String format) throws IOException { 73 final StringBuilder report = new StringBuilder(); 74 for (Reporter reporter : reporters) { 75 report.append(buildReport(executor, reporter.reporterFile, format)); 76 } 77 return report.toString(); 78 } 79 80 private String buildReport(final JavascriptExecutor driver, final File reporter, final String format) throws IOException { 81 final String command = "return jasmineMavenPlugin.printReport(window.jsApiReporter,{format:'" + format + "'});"; 82 return executeReportCommand(driver, reporter, command); 83 } 84 85 private String executeReportCommand(final JavascriptExecutor driver, final File reporter, final String command) throws IOException { 86 final String script = this.fileUtilsWrapper.readFileToString(reporter) + command; 87 final Object report = driver.executeScript(script); 88 return report.toString(); 89 } 90 91 92 }