Skip to content

Commit

Permalink
fail when there is a console error; fixes #209
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Jan 10, 2014
1 parent 6e27461 commit a1ef3f6
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
10 changes: 9 additions & 1 deletion features/halt_on_failure.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ Feature: halt the build when a spec failure occurs
Then the build should fail
And I should see "Results: 5 specs, 4 failures"
And I should see "There were Jasmine spec failures"

Scenario: project with syntax error

Given I am currently in the "jasmine-webapp-syntax-error" project
When I run "mvn clean test"
Then the build should fail
And I should see ".*JavaScript Console Errors:"
And I should see ".*SyntaxError: Parse error"

Scenario: project with no failures

Expand All @@ -32,4 +40,4 @@ Feature: halt the build when a spec failure occurs
Given I am currently in the "jasmine-webapp-single-failing" project
When I run "mvn clean test -DhaltOnFailure=false"
Then I should see "Results: 5 specs, 1 failure"
But the build should succeed
But the build should succeed
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.github.searls.jasmine.io.IOUtilsWrapper;
Expand All @@ -29,6 +33,9 @@ public JasmineResult execute(URL runnerUrl, File junitXmlReport, WebDriver drive
JavascriptExecutor executor = (JavascriptExecutor) driver;
driver.get(runnerUrl.toString());
this.waitForRunnerToFinish(driver, timeout, debug, log);

this.checkForConsoleErrors(driver, log);

JasmineResult jasmineResult = new JasmineResult();
jasmineResult.setDetails(this.buildReport(executor,format));
FileUtils.writeStringToFile(junitXmlReport, this.buildJunitXmlReport(executor,debug), "UTF-8");
Expand All @@ -40,6 +47,17 @@ public JasmineResult execute(URL runnerUrl, File junitXmlReport, WebDriver drive
}
}

private void checkForConsoleErrors(WebDriver driver, Log log) {
WebElement head = driver.findElement(By.tagName("head"));
if (head != null) {
String jserrors = head.getAttribute("jmp_jserror");
if (StringUtils.isNotBlank(jserrors)) {
log.warn("JavaScript Console Errors:\n\n * "+jserrors.replaceAll(":!:","\n * ")+"\n\n");
throw new RuntimeException("There were javascript console errors.");
}
}
}

private String buildReport(JavascriptExecutor driver, String format) throws IOException {
String script =
this.ioUtilsWrapper.toString(BUILD_REPORT_JS) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<meta http-equiv="refresh" content="$autoRefreshInterval$">
$endif$
<title>Jasmine Spec Runner</title>
<script type="text/javascript">
window.onerror = function(msg,url,line) {
var jserror = document.head.getAttribute('jmp_jserror') || '';
if (jserror) {
jserror += ':!:';
}
jserror += msg;
document.head.setAttribute('jmp_jserror',jserror);
};
</script>
$cssDependencies$
$javascriptDependencies$
$preloadScriptTags$
Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/jasmine-templates/SpecRunner.htmltemplate
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<meta http-equiv="refresh" content="$autoRefreshInterval$">
$endif$
<title>Jasmine Spec Runner</title>
<script type="text/javascript">
window.onerror = function(msg,url,line) {
var jserror = document.head.getAttribute('jmp_jserror') || '';
if (jserror) {
jserror += ':!:';
}
jserror += msg;
document.head.setAttribute('jmp_jserror',jserror);
};
</script>
$cssDependencies$
$javascriptDependencies$
$allScriptTags$
Expand All @@ -15,7 +25,6 @@
if(window.location.href.indexOf("ManualSpecRunner.html") !== -1) {
document.body.appendChild(document.createTextNode("Warning: opening this HTML file directly from the file system is deprecated. You should instead try running `mvn jasmine:bdd` from the command line, and then visit `http://localhost:8234` in your browser. "))
}


var executeJasmineSpecs = function(){
window.reporter = new jasmine.$reporter$(); jasmine.getEnv().addReporter(reporter);
Expand Down
32 changes: 32 additions & 0 deletions src/test/resources/examples/jasmine-webapp-syntax-error/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-example-superpom</artifactId>
<version>%{project.version}</version>
</parent>
<artifactId>jasmine-webapp-syntax-error</artifactId>
<packaging>war</packaging>
<name>Example Webapp using Jasmine Maven Plugin</name>

<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>%{project.version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var HelloWorld = function() {
this.greeting = function() {
return "Hello, World";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('HelloWorld',function(){

it('should say hello',function(){
var helloWorld = new HelloWorld();
expect(helloWorld.greeting()).toBe("Hello, World");
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
describe('HelloWorld',function {});

0 comments on commit a1ef3f6

Please sign in to comment.