t

org.scalatestplus.play

MixedFixtures

trait MixedFixtures extends TestSuiteMixin with UnitFixture

Trait that helps you provide different fixtures to different tests: a Application, a TestServer, or one of the Selenium WebDriverss.

Trait MixedFixtures can be mixed into any fixture.Suite. For convenience it is mixed into MixedPlaySpec. In a fixture.Suite, tests can take a no-arg function. MixedFixtures provides several no-arg function classes (classes extending Function0) that can be used to provide different fixtures for different tests.

If a test needs a Application, use the App function, like this:

"provide an Application" in new App(fakeApp("ehcacheplugin" -> "disabled")) {
  override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
}

If a test needs an Application and running TestServer, use the Server function, like this:

"send 404 on a bad request" in new Server {
  override def running() = {
    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 an Application, running TestServer, and Selenium driver, use one of functions Chrome, Firefox, HtmlUnit, InternetExplorer, or Safari. If the chosen Selenium driver is unavailable on the host platform, the test will be automatically canceled. Here's an example that uses the Safari function:

"provide a web driver" in new Safari(fakeApp()) {
  override def running() = {
    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.scalatestplus.play._
import play.api.{Play, Application}
import play.api.inject.guice._
import play.api.routing._

class ExampleSpec extends MixedPlaySpec {

  // Some helper methods
  def buildApp[A](elems: (String, String)*) = new GuiceApplicationBuilder()
    .configure(Map(elems:_*))
    .router(TestRoutes.router)
    .build()

  def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key)

  "The App function" must {
    "provide an Application" in new App(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new App(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
  }
  "The Server function" must {
    "provide an Application" in new Server(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new Server(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new Server {
      override def running() = {
        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()
      }
    }
  }
  "The HtmlUnit function" must {
    "provide an Application" in new HtmlUnit(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new HtmlUnit(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new HtmlUnit {
      override def running() = {
        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(buildApp()) {
      override def running() = {
        go to ("http://localhost:" + port + "/testing")
        pageTitle mustBe "Test Page"
        click on find(name("b")).value
        eventually { pageTitle mustBe "scalatest" }
      }
    }
  }
  "The Firefox function" must {
    "provide an Application" in new Firefox(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new Firefox(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new Firefox {
      override def running() = {
        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(buildApp()) {
      override def running() = {
        go to ("http://localhost:" + port + "/testing")
        pageTitle mustBe "Test Page"
        click on find(name("b")).value
        eventually { pageTitle mustBe "scalatest" }
      }
    }
  }
  "The Safari function" must {
    "provide an Application" in new Safari(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new Safari(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new Safari {
      override def running() = {
        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(buildApp()) {
      override def running() = {
        go to ("http://localhost:" + port + "/testing")
        pageTitle mustBe "Test Page"
        click on find(name("b")).value
        eventually { pageTitle mustBe "scalatest" }
      }
    }
  }
  "The Chrome function" must {
    "provide an Application" in new Chrome(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new Chrome(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new Chrome {
      override def running() = {
        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(buildApp()) {
      override def running() = {
        go to ("http://localhost:" + port + "/testing")
        pageTitle mustBe "Test Page"
        click on find(name("b")).value
        eventually { pageTitle mustBe "scalatest" }
      }
    }
  }
  "The InternetExplorer function" must {
    "provide an Application" in new InternetExplorer(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in new InternetExplorer(buildApp("ehcacheplugin" -> "disabled")) {
      override def running() = getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    import Helpers._
    "send 404 on a bad request" in new InternetExplorer {
      override def running() = {
        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(buildApp()) {
      override def running() = {
        go to ("http://localhost:" + port + "/testing")
        pageTitle mustBe "Test Page"
        click on find(name("b")).value
        eventually { pageTitle mustBe "scalatest" }
      }
    }
  }
  "Any old thing" must {
    "be doable without much boilerplate" in { () =>
       1 + 1 mustEqual 2
     }
  }
}

Self Type
MixedFixtures with FixtureTestSuite
Source
MixedFixtures.scala
Linear Supertypes
UnitFixture, TestSuiteMixin, SuiteMixin, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. MixedFixtures
  2. UnitFixture
  3. TestSuiteMixin
  4. SuiteMixin
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

  1. abstract class App extends (MixedFixtures.this)#NoArgHelper

    NoArg subclass that provides an Application fixture.

  2. abstract class Chrome extends (MixedFixtures.this)#NoArgHelper with WebBrowser with ChromeFactory

    NoArg subclass that provides a fixture composed of an Application, running TestServer, and Selenium ChromeDriver.

  3. abstract class Firefox extends (MixedFixtures.this)#NoArgHelper with WebBrowser with FirefoxFactory

    NoArg subclass that provides a fixture composed of a Application, running TestServer, and Selenium FirefoxDriver.

  4. type FixtureParam = Unit
    Definition Classes
    UnitFixture
  5. abstract class HtmlUnit extends (MixedFixtures.this)#NoArgHelper with WebBrowser with HtmlUnitFactory

    NoArg subclass that provides a fixture composed of an Application, running TestServer, and Selenium HtmlUnitDriver.

  6. abstract class InternetExplorer extends (MixedFixtures.this)#NoArgHelper with WebBrowser with InternetExplorerFactory

    NoArg subclass that provides a fixture composed of an Application, running TestServer, and Selenium InternetExplorerDriver.

  7. abstract class NoArgHelper extends NoArg
  8. abstract class Safari extends (MixedFixtures.this)#NoArgHelper with WebBrowser with SafariFactory

    NoArg subclass that provides a fixture composed of an Application, running TestServer, and Selenium SafariDriver.

  9. abstract class Server extends (MixedFixtures.this)#NoArgHelper

    NoArg subclass that provides a fixture composed of a Application and running TestServer.

Abstract Value Members

  1. abstract def expectedTestCount(filter: Filter): Int
    Definition Classes
    SuiteMixin
  2. abstract def nestedSuites: IndexedSeq[Suite]
    Definition Classes
    SuiteMixin
  3. abstract def rerunner: Option[String]
    Definition Classes
    SuiteMixin
  4. abstract def run(testName: Option[String], args: Args): Status
    Definition Classes
    SuiteMixin
  5. abstract def runNestedSuites(args: Args): Status
    Attributes
    protected
    Definition Classes
    SuiteMixin
  6. abstract def runTest(testName: String, args: Args): Status
    Attributes
    protected
    Definition Classes
    SuiteMixin
  7. abstract def runTests(testName: Option[String], args: Args): Status
    Attributes
    protected
    Definition Classes
    SuiteMixin
  8. abstract def suiteId: String
    Definition Classes
    SuiteMixin
  9. abstract def suiteName: String
    Definition Classes
    SuiteMixin
  10. abstract def tags: Map[String, Set[String]]
    Definition Classes
    SuiteMixin
  11. abstract def testDataFor(testName: String, theConfigMap: ConfigMap): TestData
    Definition Classes
    SuiteMixin
  12. abstract def testNames: Set[String]
    Definition Classes
    SuiteMixin
  13. abstract def withFixture(test: (MixedFixtures.this)#NoArgTest): Outcome
    Attributes
    protected
    Definition Classes
    TestSuiteMixin
  14. abstract val styleName: String
    Definition Classes
    SuiteMixin
    Annotations
    @deprecated
    Deprecated

    (Since version 3.1.0) The styleName lifecycle method has been deprecated and will be removed in a future version of ScalaTest with no replacement.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @HotSpotIntrinsicCandidate() @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  9. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  10. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  11. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  12. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  13. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  14. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  15. def toString(): String
    Definition Classes
    AnyRef → Any
  16. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  17. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  18. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  19. def withFixture(test: (MixedFixtures.this)#OneArgTest): Outcome
    Definition Classes
    UnitFixture

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

    (Since version 9)

Inherited from UnitFixture

Inherited from TestSuiteMixin

Inherited from SuiteMixin

Inherited from AnyRef

Inherited from Any

Ungrouped