All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.searls.jasmine.mojo.TestMojo Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 3.0-beta-02
Show newest version
/*-
 * #%L
 * jasmine-maven-plugin
 * %%
 * Copyright (C) 2010 - 2017 Justin Searls
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.github.searls.jasmine.mojo;

import com.github.searls.jasmine.config.ImmutableServerConfiguration;
import com.github.searls.jasmine.config.ImmutableWebDriverConfiguration;
import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.config.ServerConfiguration;
import com.github.searls.jasmine.config.WebDriverConfiguration;
import com.github.searls.jasmine.driver.WebDriverFactory;
import com.github.searls.jasmine.format.JasmineResultLogger;
import com.github.searls.jasmine.model.JasmineResult;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerExecutor;
import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
import com.github.searls.jasmine.server.ServerManager;
import com.google.common.annotations.VisibleForTesting;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.Collections;
import java.util.List;

/**
 * Execute specs using Selenium Web Driver. Uses PhantomJsDriver for head-less execution by default.
 */
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, requiresDependencyResolution = ResolutionScope.TEST)
public class TestMojo extends AbstractJasmineMojo {

  private static final Logger LOGGER = LoggerFactory.getLogger(TestMojo.class);

  /**
   * Determines the Selenium WebDriver class we'll use to execute the tests. See the Selenium documentation for more
   * details. The plugin uses PhantomJSDriver by default.
   * 
*

Some valid examples:

*
    *
  • org.openqa.selenium.chrome.ChromeDriver
  • *
  • org.openqa.selenium.htmlunit.HtmlUnitDriver
  • *
  • org.openqa.selenium.firefox.FirefoxDriver
  • *
  • org.openqa.selenium.ie.InternetExplorerDriver
  • *
*
* See the webDriverCapabilities property for configuring driver specific properties. * * @since 1.1.0 */ @Parameter(defaultValue = "org.openqa.selenium.chrome.ChromeDriver") private String webDriverClassName = ChromeDriver.class.getName(); /** *

Web driver capabilities used to initialize a DesiredCapabilities instance when creating a web driver.

*
*

Capabilities value can be either a String, a List, or a Map.

*
*

Example:

*
   * <webDriverCapabilities>
   *   <capability>
   *     <name>phantomjs.binary.path</name>
   *     <value>/opt/phantomjs/bin/phantomjs</value>
   *   </capability>
   *   <capability>
   *     <name>phantomjs.cli.args</name>
   *     <list>
   *       <value>--disk-cache=true</value>
   *       <value>--max-disk-cache-size=256</value>
   *     </list>
   *   </capability>
   *   <capability>
   *     <name>proxy</name>
   *     <map>
   *       <httpProxy>myproxyserver.com:8000</httpProxy>
   *     </map>
   *   </capability>
   * </webDriverCapabilities>
   * 
* * @since 1.3.1.1 */ @Parameter private List webDriverCapabilities = Collections.emptyList(); /** *

Determines the format that jasmine:test will print to console.

*

Valid options:

*
    *
  • "documentation" - (default) - print specs in a nested format
  • *
  • "progress" - more terse, with a period for a passed specs and an 'F' for failures (e.g. '...F...')
  • *
* * @since 1.1.0 */ @Parameter(defaultValue = "documentation") private String format = "documentation"; /** * Keep the server alive after the jasmine:test goal exists. * Useful if you need to run further analysis on your tests, like collecting code coverage. * * @since 1.3.1.0 */ @Parameter(property = "keepServerAlive", defaultValue = "false") private boolean keepServerAlive = false; /** * Timeout for spec execution in seconds. * * @since 1.1.0 */ @Parameter(defaultValue = "300") private int timeout = 300; /** * Halt the build on test failure. * * @since 1.1.0 */ @Parameter(property = "haltOnFailure", defaultValue = "true") private boolean haltOnFailure = true; /** * True to increase HtmlUnit output and attempt reporting on specs even if a timeout occurred. * * @since 1.1.0 */ @Parameter(defaultValue = "false") private boolean debug = false; /** * Skip execution of tests. * * @see http://maven.apache.org/general.html#skip-test * @since 1.1.0 */ @Parameter(property = "skipTests") private boolean skipTests = false; /** * Skip compilation and execution of tests. * * @see http://maven.apache.org/general.html#skip-test * @since 1.3.1.3 */ @Parameter(property = "maven.test.skip") private boolean mvnTestSkip = false; /** * Skip only jasmine tests * * @since 1.3.1.3 */ @Parameter(property = "skipJasmineTests") private boolean skipJasmineTests = false; @Parameter( defaultValue = "${session}", readonly = true ) private MavenSession mavenSession = null; private final WebDriverFactory webDriverFactory; private final SpecRunnerExecutor specRunnerExecutor; private final JasmineResultLogger jasmineResultLogger; private final ResourceHandlerConfigurator resourceHandlerConfigurator; @Inject public TestMojo(MavenProject mavenProject, ResourceRetriever resourceRetriever, ReporterRetriever reporterRetriever, WebDriverFactory webDriverFactory, SpecRunnerExecutor specRunnerExecutor, JasmineResultLogger jasmineResultLogger, ResourceHandlerConfigurator resourceHandlerConfigurator) { super(mavenProject, ReporterType.JsApiReporter, resourceRetriever, reporterRetriever); this.webDriverFactory = webDriverFactory; this.specRunnerExecutor = specRunnerExecutor; this.jasmineResultLogger = jasmineResultLogger; this.resourceHandlerConfigurator = resourceHandlerConfigurator; } @Override public void execute() throws MojoExecutionException, MojoFailureException { if (!this.isSkipTests()) { super.execute(); } else { LOGGER.info("Skipping Jasmine Specs"); } } @Override public void run(ServerConfiguration serverConfiguration, JasmineConfiguration configuration) throws Exception { ServerManager serverManager = ServerManager.newInstance(); try { int port = serverManager.start(resourceHandlerConfigurator.createHandler(configuration)); setPortProperty(port); LOGGER.info("Executing Jasmine Specs"); JasmineResult result = this.executeSpecs( ImmutableServerConfiguration.copyOf(serverConfiguration).withServerPort(port), configuration ); this.logResults(result); this.throwAnySpecFailures(result); } finally { if (!keepServerAlive) { serverManager.stop(); } } } private void setPortProperty(int port) { getMavenProject().getProperties().setProperty("jasmine.serverPort", String.valueOf(port)); } private JasmineResult executeSpecs(ServerConfiguration serverConfiguration, JasmineConfiguration configuration) throws Exception { WebDriver driver = this.createDriver(); return specRunnerExecutor.execute( serverConfiguration.getServerURL(), driver, this.timeout, this.debug, this.format, configuration.getReporters(), configuration.getFileSystemReporters() ); } private WebDriver createDriver() { return webDriverFactory.createWebDriver(getWebDriverConfiguration()); } private void logResults(JasmineResult result) { jasmineResultLogger.log(result); } private void throwAnySpecFailures(JasmineResult result) throws MojoFailureException { if (this.haltOnFailure && !result.didPass()) { throw new MojoFailureException("There were Jasmine spec failures."); } } private WebDriverConfiguration getWebDriverConfiguration() { return ImmutableWebDriverConfiguration.builder() .debug(this.debug) .webDriverCapabilities(webDriverCapabilities) .webDriverClassName(webDriverClassName) .build(); } private boolean isSkipTests() { return this.skipTests || this.mvnTestSkip || this.skipJasmineTests; } @VisibleForTesting void setSkipTests(boolean skipTests) { this.skipTests = skipTests; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy