-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Specs.scala
103 lines (89 loc) · 3.73 KB
/
Specs.scala
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
/*
* Copyright (C) 2009-2017 Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.test
import org.openqa.selenium.WebDriver
import org.specs2.execute.{ AsResult, Result }
import org.specs2.mutable.Around
import org.specs2.specification.Scope
import play.api.inject.guice.{ GuiceApplicationBuilder, GuiceApplicationLoader }
import play.api.{ Application, ApplicationLoader, Environment, Mode }
import play.core.j.JavaContextComponents
import play.core.server.ServerProvider
// NOTE: Do *not* put any initialisation code in the below classes, otherwise delayedInit() gets invoked twice
// which means around() gets invoked twice and everything is not happy. Only lazy vals and defs are allowed, no vals
// or any other code blocks.
/**
* Used to run specs within the context of a running application loaded by the given `ApplicationLoader`.
*
* @param applicationLoader The application loader to use
* @param context The context supplied to the application loader
*/
abstract class WithApplicationLoader(applicationLoader: ApplicationLoader = new GuiceApplicationLoader(), context: ApplicationLoader.Context = ApplicationLoader.Context.create(new Environment(new java.io.File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test))) extends Around with Scope {
implicit lazy val app = applicationLoader.load(context)
def around[T: AsResult](t: => T): Result = {
Helpers.running(app)(AsResult.effectively(t))
}
}
/**
* Used to run specs within the context of a running application.
*
* @param app The fake application
*/
abstract class WithApplication(val app: Application = GuiceApplicationBuilder().build()) extends Around with Scope {
def this(builder: GuiceApplicationBuilder => GuiceApplicationBuilder) {
this(builder(GuiceApplicationBuilder()).build())
}
implicit def implicitApp = app
implicit def implicitMaterializer = app.materializer
override def around[T: AsResult](t: => T): Result = {
Helpers.running(app)(AsResult.effectively(t))
}
}
/**
* Used to run specs within the context of a running server.
*
* @param app The fake application
* @param port The port to run the server on
* @param serverProvider *Experimental API; subject to change* The type of
* server to use. Defaults to providing a Netty server.
*/
abstract class WithServer(
val app: Application = GuiceApplicationBuilder().build(),
val port: Int = Helpers.testServerPort,
val serverProvider: Option[ServerProvider] = None) extends Around with Scope {
implicit def implicitMaterializer = app.materializer
implicit def implicitApp = app
implicit def implicitPort: Port = port
override def around[T: AsResult](t: => T): Result =
Helpers.running(TestServer(
port = port,
application = app,
serverProvider = serverProvider))(AsResult.effectively(t))
}
/**
* Used to run specs within the context of a running server, and using a web browser
*
* @param webDriver The driver for the web browser to use
* @param app The fake application
* @param port The port to run the server on
*/
abstract class WithBrowser[WEBDRIVER <: WebDriver](
val webDriver: WebDriver = WebDriverFactory(Helpers.HTMLUNIT),
val app: Application = GuiceApplicationBuilder().build(),
val port: Int = Helpers.testServerPort) extends Around with Scope {
def this(
webDriver: Class[WEBDRIVER],
app: Application,
port: Int) = this(WebDriverFactory(webDriver), app, port)
implicit def implicitApp: Application = app
implicit def implicitPort: Port = port
lazy val browser: TestBrowser = TestBrowser(webDriver, Some("http://localhost:" + port))
override def around[T: AsResult](t: => T): Result = {
try {
Helpers.running(TestServer(port, app))(AsResult.effectively(t))
} finally {
browser.quit()
}
}
}