NoArg subclass that provides a FakeApplication fixture.
NoArg subclass that provides a fixture composed of a FakeApplication, running TestServer, and
Selenium ChromeDriver.
NoArg subclass that provides a fixture composed of a FakeApplication, running TestServer, and
Selenium FirefoxDriver.
NoArg subclass that provides a fixture composed of a FakeApplication, running TestServer, and
Selenium HtmlUnitDriver.
NoArg subclass that provides a fixture composed of a FakeApplication, running TestServer, and
Selenium InternetExplorerDriver.
NoArg subclass that provides a fixture composed of a FakeApplication, running TestServer, and
Selenium SafariDriver.
NoArg subclass that provides a fixture composed of a FakeApplication and running TestServer.
Trait that helps you provide different fixtures to different tests: a
FakeApplication, aTestServer, or one of the SeleniumWebDriverss.Trait
MixedFixturescan be mixed into anyfixture.Suite. For convenience it is mixed into MixedPlaySpec. In afixture.Suite, tests can take a no-arg function.MixedFixturesprovides several no-arg function classes (classes extendingFunction0) that can be used to provide different fixtures for different tests.If a test needs a
FakeApplication, use theAppfunction, like this:"provide a FakeApplication" in new App(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") }If a test needs a
FakeApplicationand runningTestServer, use theServerfunction, like this:"send 404 on a bad request" in new Server { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() }If a test needs a
FakeApplication, runningTestServer, and Selenium driver, use one of functionsChrome,Firefox,HtmlUnit,InternetExplorer, orSafari. If the chosen Selenium driver is unavailable on the host platform, the test will be automatically canceled. Here's an example that uses theSafarifunction:"provide a web driver" in new Safari(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } }Here's a complete example:
package org.scalatestplus.play.examples.mixedfixtures import play.api.test._ import org.scalatest._ import org.scalatestplus.play._ import play.api.{Play, Application} class ExampleSpec extends MixedPlaySpec { // Some helper methods def fakeApp[A](elems: (String, String)*) = FakeApplication(additionalConfiguration = Map(elems:_*), withRoutes = { case ("GET", "/testing") => Action( Results.Ok( "<html>" + "<head><title>Test Page</title></head>" + "<body>" + "<input type='button' name='b' value='Click Me' onclick='document.title=\"scalatest\"' />" + "</body>" + "</html>" ).as("text/html") ) } ) def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key) // If a test just needs aFakeApplication, use "new App": "The App function" must { "provide a FakeApplication" in new App(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new App(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new App(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } } // If a test needs aFakeApplicationand runningTestServer, use "new Server": "The Server function" must { "provide a FakeApplication" in new Server(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new Server(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new Server(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new Server { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } // If a test needs aFakeApplication, runningTestServer, and Selenium //HtmlUnitdriver use "new HtmlUnit": "The HtmlUnit function" must { "provide a FakeApplication" in new HtmlUnit(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new HtmlUnit(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new HtmlUnit(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new HtmlUnit { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in new HtmlUnit(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } // If a test needs aFakeApplication, runningTestServer, and Selenium //Firefoxdriver use "new Firefox": "The Firefox function" must { "provide a FakeApplication" in new Firefox(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new Firefox(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new Firefox(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new Firefox { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in new Firefox(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } // If a test needs aFakeApplication, runningTestServer, and Selenium //Safaridriver use "new Safari": "The Safari function" must { "provide a FakeApplication" in new Safari(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new Safari(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new Safari(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new Safari { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in new Safari(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } // If a test needs aFakeApplication, runningTestServer, and Selenium //Chromedriver use "new Chrome": "The Chrome function" must { "provide a FakeApplication" in new Chrome(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new Chrome(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new Chrome(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new Chrome { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in new Chrome(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } // If a test needs aFakeApplication, runningTestServer, and Selenium //InternetExplorerdriver use "new InternetExplorer": "The InternetExplorer function" must { "provide a FakeApplication" in new InternetExplorer(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in new InternetExplorer(fakeApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } "start the FakeApplication" in new InternetExplorer(fakeApp("ehcacheplugin" -> "disabled")) { Play.maybeApplication mustBe Some(app) } import Helpers._ "send 404 on a bad request" in new InternetExplorer { import java.net._ val url = new URL("http://localhost:" + port + "/boom") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in new InternetExplorer(fakeApp()) { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } // If a test does not need any special fixtures, just // write"in { () => ...""Any old thing" must { "be doable without much boilerplate" in { () => 1 + 1 mustEqual 2 } } }