/
TestMojo.java
304 lines (267 loc) · 10.3 KB
/
TestMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.github.searls.jasmine.mojo;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.locate.RepositoryDetails;
import com.github.searls.jasmine.NullLog;
import com.github.searls.jasmine.driver.WebDriverFactory;
import com.github.searls.jasmine.format.JasmineResultLogger;
import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.model.JasmineResult;
import com.github.searls.jasmine.runner.CreatesRunner;
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 org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
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.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.openqa.selenium.WebDriver;
import javax.inject.Inject;
import java.io.File;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* 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 {
/**
* Determines the Selenium WebDriver class we'll use to execute the tests. See the Selenium documentation for more details.
* The plugin uses <a href="https://github.com/detro/ghostdriver">PhantomJSDriver</a> by default.
* <br>
* <p>Some valid examples:</p>
* <ul>
* <li>org.openqa.selenium.htmlunit.HtmlUnitDriver</li>
* <li>org.openqa.selenium.phantomjs.PhantomJSDriver</li>
* <li>org.openqa.selenium.firefox.FirefoxDriver</li>
* <li>org.openqa.selenium.ie.InternetExplorerDriver</li>
* </ul>
* <br>
* See the webDriverCapabilities property for configuring driver specific properties.
*
* @since 1.1.0
*/
@Parameter(defaultValue = "org.openqa.selenium.phantomjs.PhantomJSDriver")
protected String webDriverClassName;
/**
* <p>Web driver capabilities used to initialize a DesiredCapabilities instance when creating a web driver.</p>
* <br>
* <p>Capabilities value can be either a String, a List, or a Map.</p>
* <br>
* <p>Example:</p>
* <pre>
* <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>
* </pre>
*
* @since 1.3.1.1
*/
@Parameter
protected List<Capability> webDriverCapabilities = Collections.emptyList();
/**
* <p>Determines the browser and version profile that HtmlUnit will simulate. This setting does nothing if the plugin is configured not to use HtmlUnit.
* This maps 1-to-1 with the public static instances found in {@link com.gargoylesoftware.htmlunit.BrowserVersion}.</p>
* <br>
* <p>Some valid examples: CHROME, FIREFOX_17, INTERNET_EXPLORER_9, INTERNET_EXPLORER_10</p>
*
* @since 1.1.0
* @deprecated Use the webDriverCapabilities parameter instead.
*/
@Parameter(defaultValue = "FIREFOX_17")
@Deprecated
protected String browserVersion;
/**
* <p>Determines the format that jasmine:test will print to console.</p>
* <p>Valid options:</p>
* <ul>
* <li>"documentation" - (default) - print specs in a nested format</li>
* <li>"progress" - more terse, with a period for a passed specs and an 'F' for failures (e.g. '...F...')</li>
* </ul>
*
* @since 1.1.0
*/
@Parameter(defaultValue = "documentation")
protected String format;
/**
* <p>Configure which version of PhantomJS should be used and how it should be found. The core of the
* <a href="http://klieber.github.io/phantomjs-maven-plugin">phantomjs-maven-plugin</a> is used to provide this
* functionality and this parameter should match the configuration of the
* <a href="http://kylelieber.com/phantomjs-maven-plugin/install-mojo.html">phantomjs-maven-plugin install</a> goal.</p>
* <br>
* <p>Default Options:</p>
* <pre>
* <phantomjs>
* <version>2.0.0</version>
* <checkSystemPath>true</checkSystemPath>
* <enforceVersion>true</enforceVersion>
* <source>REPOSITORY</source>
* <baseUrl></baseUrl>
* <outputDirectory>target/phantomjs</outputDirectory>
* </phantomjs>
* </pre>
*
* @since 2.0
*/
@Parameter(property = "phantomjs", defaultValue = "${phantomJs}")
protected PhantomJsOptions phantomjs;
/**
* Keep the server alive after the <code>jasmine:test</code> 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")
protected boolean keepServerAlive;
@Parameter(
defaultValue = "${repositorySystemSession}",
readonly = true
)
private RepositorySystemSession repositorySystemSession;
@Parameter(
defaultValue = "${project.remoteProjectRepositories}",
readonly = true
)
private List<RemoteRepository> remoteRepositories;
@Parameter(
defaultValue = "${session}",
readonly = true
)
private MavenSession mavenSession;
private RepositorySystem repositorySystem;
private final RelativizesFilePaths relativizesFilePaths;
@Inject
public TestMojo(RepositorySystem repositorySystem) {
this.repositorySystem = repositorySystem;
this.relativizesFilePaths = new RelativizesFilePaths();
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!this.isSkipTests()) {
super.execute();
} else {
this.getLog().info("Skipping Jasmine Specs");
}
}
@Override
public void run() throws Exception {
ServerManager serverManager = this.getServerManager();
try {
int port = serverManager.start();
setPortProperty(port);
this.getLog().info("Executing Jasmine Specs");
JasmineResult result = this.executeSpecs(new URL(this.uriScheme + "://" + this.serverHostname + ":" + port));
this.logResults(result);
this.throwAnySpecFailures(result);
} finally {
if (!keepServerAlive) {
serverManager.stop();
}
}
}
private ServerManager getServerManager() throws MojoExecutionException {
Log log = this.debug ? this.getLog() : new NullLog();
CreatesRunner createsRunner = new CreatesRunner(
this,
log,
this.specRunnerHtmlFileName,
ReporterType.JsApiReporter);
ResourceHandlerConfigurator configurator = new ResourceHandlerConfigurator(
this,
this.relativizesFilePaths,
createsRunner);
return ServerManager.newInstance(configurator);
}
private void setPortProperty(int port) {
this.mavenProject.getProperties().setProperty("jasmine.serverPort", String.valueOf(port));
}
private JasmineResult executeSpecs(URL runner) throws Exception {
WebDriver driver = this.createDriver();
JasmineResult result = new SpecRunnerExecutor().execute(
runner,
driver,
this.timeout,
this.debug,
this.getLog(),
this.format,
getReporters(),
getFileSystemReporters()
);
return result;
}
private WebDriver createDriver() throws Exception {
RepositoryDetails details = new RepositoryDetails();
details.setRemoteRepositories(remoteRepositories);
details.setRepositorySystem(repositorySystem);
details.setRepositorySystemSession(repositorySystemSession);
configure(mavenSession.getUserProperties());
WebDriverFactory factory = new WebDriverFactory();
factory.setWebDriverCapabilities(webDriverCapabilities);
factory.setWebDriverClassName(webDriverClassName);
factory.setDebug(debug);
factory.setBrowserVersion(browserVersion);
factory.setPhantomJsLocatorOptions(phantomjs);
factory.setRepositoryDetails(details);
return factory.createWebDriver();
}
private void configure(Properties properties) {
phantomjs.setVersion(
properties.getProperty("phantomjs.version", phantomjs.getVersion())
);
phantomjs.setSource(
PhantomJsLocatorOptions.Source.valueOf(
properties.getProperty("phantomjs.source", phantomjs.getSource().toString())
)
);
phantomjs.setOutputDirectory(
new File(properties.getProperty("phantomjs.outputDirectory", phantomjs.getOutputDirectory().toString()))
);
phantomjs.setBaseUrl(
properties.getProperty("phantomjs.baseUrl", phantomjs.getBaseUrl())
);
phantomjs.setCheckSystemPath(
configureBoolean(properties, "phantomjs.checkSystemPath", phantomjs.isCheckSystemPath())
);
phantomjs.setEnforceVersion(
properties.getProperty("phantomjs.enforceVersion", phantomjs.getEnforceVersion())
);
}
private boolean configureBoolean(Properties properties, String property, boolean defaultValue) {
return Boolean.parseBoolean(properties.getProperty(property, Boolean.toString(defaultValue)));
}
private void logResults(JasmineResult result) {
JasmineResultLogger resultLogger = new JasmineResultLogger();
resultLogger.setLog(this.getLog());
resultLogger.log(result);
}
private void throwAnySpecFailures(JasmineResult result) throws MojoFailureException {
if (this.haltOnFailure && !result.didPass()) {
throw new MojoFailureException("There were Jasmine spec failures.");
}
}
}