package play
- Alphabetic
- Public
- Protected
Package Members
- package components
- package guice
Type Members
- trait AllBrowsersPerSuite extends TestSuiteMixin with WebBrowser with Eventually with IntegrationPatience
Trait that uses a shared test approach to enable you to run the same tests on multiple browsers in a ScalaTest
Suite
, where each kind of browser is started and stopped just once for the wholeSuite
.Trait that uses a shared test approach to enable you to run the same tests on multiple browsers in a ScalaTest
Suite
, where each kind of browser is started and stopped just once for the wholeSuite
.Note: the difference between this trait and AllBrowsersPerTest is that this trait will allow you to write tests that rely on maintaining browser state between the tests. This is a good fit for integration tests in which each test builds on actions taken by the previous tests.
This trait overrides
Suite
'swithFixture
lifecycle method to create a newWebDriver
instance the first time it is needed by each test, and close it the first time it is not needed (thus allowing multiple tests to share the same browser), and overrides thetags
lifecycle method to tag the shared tests so you can filter them by browser type. This trait's self-type, ServerProvider, will ensure aTestServer
andApplication
are available to each test. The self-type will require that you mix in either GuiceOneServerPerSuite, GuiceOneServerPerTest, ConfiguredServer before you mix in this trait. Your choice among these threeServerProvider
s will determine the extent to which aTestServer
is shared by multiple tests.You'll need to place any tests that you want executed by multiple browsers in a
sharedTests
method. Because all tests in a ScalaTestSuite
must have unique names, you'll need to append the browser name (available from theBrowserInfo
passed tosharedTests
) to each test name:def sharedTests(browser: BrowserInfo) { "The blog app home page" must { "have the correct title " + browser.name in { go to (host + "index.html") pageTitle must be ("Awesome Blog") }
All tests registered via
sharedTests
will be registered for each desiredWebDriver
, as specified by thebrowsers
field. When running, any tests for browser drivers that are unavailable on the current platform will be canceled. All tests registered undersharedTests
will be tagged automatically if they end with a browser name in square brackets. For example, if a test name ends with[Firefox]
, it will be automatically tagged with"org.scalatest.tags.FirefoxBrowser"
. This will allow you can include or exclude the shared tests by browser type using ScalaTest's regular tagging feature.You can use tagging to include or exclude browsers that you sometimes want to test with, but not always. If you never want to test with a particular browser, you can prevent tests for it from being registered at all by overriding
browsers
and excluding itsBrowserInfo
in the returnedSeq
. For example, to disable registration of tests forHtmlUnit
, you'd write:override lazy val browsers: IndexedSeq[BrowserInfo] = Vector( FirefoxInfo, SafariInfo, InternetExplorerInfo, ChromeInfo )
Note that this trait can only be mixed into traits that register tests as functions, as the shared tests technique is not possible in style traits that declare tests as methods, such as
org.scalatest.Spec
. Attempting to do so will become a type error once we release ScalaTest 2.2.0.package org.scalatestplus.play.examples.allbrowserspersuite import org.scalatestplus.play._ import org.scalatestplus.play.guice._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ import play.api.cache.ehcache.EhCacheModule class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with AllBrowsersPerSuite { // Override fakeApplication if you need an Application with other than // default parameters. def fakeApplication() = new GuiceApplicationBuilder() .disable[EhCacheModule] .configure("foo" -> "bar") .router(TestRoutes.router) .build() // Place tests you want run in different browsers in the `sharedTests` method: def sharedTests(browser: BrowserInfo) = { "The AllBrowsersPerSuite trait" must { "provide a web driver " + browser.name in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } } // Place tests that don't need a WebDriver outside the `sharedTests` method // in the constructor, the usual place for tests in a `PlaySpec` "The AllBrowsersPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("foo") mustBe Some("bar") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("foo") mustBe Some("bar") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
Here's how the output would look if you ran the above test class in sbt on a platform that did not support Selenium drivers for Internet Explorer or Chrome:
> test-only *allbrowserspersharedsuite* [info] ExampleSpec: [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [Firefox] [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [Safari] [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [InternetExplorer] !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (AllBrowsersPerSuite.scala:257) [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [Chrome] !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (AllBrowsersPerSuite.scala:257) [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [HtmlUnit] [info] The AllBrowsersPerSuite trait [info] - must provide a Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port [info] - must provide an actual running server
Because the shared tests will be tagged according to browser, you can include or exclude tests based on the browser they use. For example, here's how the output would look if you ran the above test class with sbt and ask to include only Firefox:
> test-only *allbrowserspersharedtest* -- -n org.scalatest.tags.FirefoxBrowser [info] ExampleSpec: [info] The AllBrowsersPerSuite trait [info] - must provide a web driver [Firefox] [info] The AllBrowsersPerSuite trait [info] The AllBrowsersPerSuite trait [info] The AllBrowsersPerSuite trait [info] The AllBrowsersPerSuite trait [info] The AllBrowsersPerSuite trait
- trait AllBrowsersPerTest extends TestSuiteMixin with WebBrowser with Eventually with IntegrationPatience
Trait that uses a shared test approach to enable you to run the same tests on multiple browsers in a ScalaTest
Suite
, where a new browser is started before each test that needs a browser, and stopped after.Trait that uses a shared test approach to enable you to run the same tests on multiple browsers in a ScalaTest
Suite
, where a new browser is started before each test that needs a browser, and stopped after.Note: the difference between this trait and AllBrowsersPerSuite is that
AllBrowsersPerSuite
will allow you to write tests that rely on maintaining browser state between the tests. Thus,AllBrowsersPerSuite
is a good fit for integration tests in which each test builds on actions taken by the previous tests. This trait is good if your tests each need a brand new browser.This trait overrides
Suite
'swithFixture
lifecycle method to create a newWebDriver
instance before executing each test that needs a browser, closing it after the test completes, and overrides thetags
lifecycle method to tag the shared tests so you can filter them by browser type. This trait's self-type, ServerProvider, will ensure aTestServer
andApplication
are available to each test. The self-type will require that you mix in either GuiceOneServerPerSuite, GuiceOneServerPerTest, ConfiguredServer before you mix in this trait. Your choice among these threeServerProvider
s will determine the extent to which aTestServer
is shared by multiple tests.You'll need to place any tests that you want executed by multiple browsers in a
sharedTests
method. Because all tests in a ScalaTestSuite
must have unique names, you'll need to append the browser name (available from theBrowserInfo
passed tosharedTests
) to each test name:def sharedTests(browser: BrowserInfo) { "The blog app home page" must { "have the correct title " + browser.name in { go to (host + "index.html") pageTitle must be ("Awesome Blog") }
All tests registered via
sharedTests
will be registered for each desiredWebDriver
, as specified by thebrowsers
field. When running, any tests for browser drivers that are unavailable on the current platform will be canceled. All tests registered undersharedTests
will be tagged automatically if they end with a browser name in square brackets. For example, if a test name ends with[Firefox]
, it will be automatically tagged with"org.scalatest.tags.FirefoxBrowser"
. This will allow you can include or exclude the shared tests by browser type using ScalaTest's regular tagging feature.You can use tagging to include or exclude browsers that you sometimes want to test with, but not always. If you never want to test with a particular browser, you can prevent tests for it from being registered at all by overriding
browsers
and excluding itsBrowserInfo
in the returnedSeq
. For example, to disable registration of tests forHtmlUnit
, you'd write:override lazy val browsers: IndexedSeq[BrowserInfo] = Vector( FirefoxInfo, SafariInfo, InternetExplorerInfo, ChromeInfo )
Note that this trait can only be mixed into traits that register tests as functions, as the shared tests technique is not possible in style traits that declare tests as methods, such as
org.scalatest.Spec
. Attempting to do so will become a type error once we release ScalaTest 2.2.0.package org.scalatestplus.play.examples.allbrowserspertest import org.scalatest._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ import play.api.cache.ehcache.EhCacheModule class ExampleSpec extends PlaySpec with OneServerPerTest with AllBrowsersPerTest { // Override newAppForTest if you need a Application with other than non-default parameters. override def newAppForTest(testData: TestData): Application = new GuiceApplicationBuilder() .disable[EhCacheModule] .configure("foo" -> "bar") .router(TestRoutes.router) .build() // Place tests you want run in different browsers in the `sharedTests` method: def sharedTests(browser: BrowserInfo) = { "The AllBrowsersPerTest trait" must { "provide a web driver " + browser.name in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } } // Place tests you want run just once outside the `sharedTests` method // in the constructor, the usual place for tests in a `PlaySpec` "The AllBrowsersPerTest trait" must { "provide a FakeApplication" in { app.configuration.getOptional[String]("foo") mustBe Some("bar") } "make the FakeApplication available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("foo") mustBe Some("bar") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
Here's how the output would look if you ran the above test class in sbt on a platform that did not support Selenium drivers for Internet Explorer or Chrome:
> test-only *allbrowserspersharedtest* [info] ExampleSpec: [info] The AllBrowsersPerTest trait [info] - must provide a web driver [Firefox] [info] The AllBrowsersPerTest trait [info] - must provide a web driver [Safari] [info] The AllBrowsersPerTest trait [info] - must provide a web driver [InternetExplorer] !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (AllBrowsersPerTest.scala:257) [info] The AllBrowsersPerTest trait [info] - must provide a web driver [Chrome] !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (AllBrowsersPerTest.scala:257) [info] The AllBrowsersPerTest trait [info] - must provide a web driver [HtmlUnit] [info] The AllBrowsersPerTest trait [info] - must provide a Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port [info] - must provide an actual running server
Because the shared tests will be tagged according to browser, you can include or exclude tests based on the browser they use. For example, here's how the output would look if you ran the above test class with sbt and ask to include only Firefox:
> test-only *allbrowserspersharedtest* -- -n org.scalatest.tags.FirefoxBrowser [info] ExampleSpec: [info] The AllBrowsersPerTest trait [info] - must provide a web driver [Firefox] [info] The AllBrowsersPerTest trait [info] The AllBrowsersPerTest trait [info] The AllBrowsersPerTest trait [info] The AllBrowsersPerTest trait [info] The AllBrowsersPerTest trait
- trait AppProvider extends AnyRef
Trait that defines an application as
app
. - trait BaseOneAppPerSuite extends TestSuiteMixin
The base abstract trait for one app per suite.
- trait BaseOneAppPerTest extends TestSuiteMixin with AppProvider
Trait that provides a new
Application
instance for each test.Trait that provides a new
Application
instance for each test.This
TestSuiteMixin
trait's overriddenwithFixture
method creates a newApplication
before each test and ensures it is cleaned up after the test has completed. You can access theApplication
from your tests as methodapp
(which is marked implicit).By default, this trait creates a new
Application
for each test using default parameter values, which is returned by thenewAppForTest
method defined in this trait. If your tests need aApplication
with non-default parameters, overridenewAppForTest
to return it.Here's an example that demonstrates some of the services provided by this trait:
package org.scalatestplus.play.examples.oneapppertest import org.scalatest._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ class ExampleSpec extends PlaySpec with OneAppPerTest { // Override newAppForTest if you need an Application with other than non-default parameters. implicit override def newAppForTest(testData: TestData): Application = new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build() "The OneAppPerTest trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } } }
- trait BaseOneServerPerSuite extends TestSuiteMixin with ServerProvider
Trait that provides a new
Application
and runningTestServer
instance per ScalaTestSuite
.Trait that provides a new
Application
and runningTestServer
instance per ScalaTestSuite
.By default, this trait creates a new
Application
for theSuite
using default parameter values, which is made available via theapp
field defined in this trait and a newTestServer
for theSuite
using the port number provided by itsport
field and theApplication
provided by itsapp
field. If yourSuite
needs aApplication
with non-default parameters, overrideapp
. If it needs a different port number, overrideport
.This
TestSuiteMixin
trait's overriddenrun
method callsstart
on theTestServer
before executing theSuite
via a call tosuper.run
. In addition, it places a reference to theApplication
provided byapp
into theConfigMap
under the keyorg.scalatestplus.play.app
and to the port number provided byport
under the keyorg.scalatestplus.play.port
. This allows any nestedSuite
s to access theSuite
'sApplication
and port number as well, most easily by having the nestedSuite
s mix in the ConfiguredServer trait. On the status returned bysuper.run
, this trait's overriddenrun
method registers a call tostop
on theTestServer
to be executed when theStatus
completes, and returns the sameStatus
. This ensure theTestServer
will continue to execute until all nested suites have completed, after which theTestServer
will be stopped.Here's an example that demonstrates some of the services provided by this trait:
package org.scalatestplus.play.examples.oneserverpersuite import play.api.test._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ class ExampleSpec extends PlaySpec with OneServerPerSuite { // Override fakeApplication() if you need a Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder().configure("ehcacheplugin" -> "disabled").build() "The OneServerPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
If you have many tests that can share the same
Application
andTestServer
, and you don't want to put them all into one test class, you can place them into differentSuite
classes. These will be your nested suites. Create a master suite that extendsOneServerPerSuite
and declares the nestedSuite
s. Annotate the nested suites with@DoNotDiscover
and have them extendConfiguredServer
. Here's an example:package org.scalatestplus.play.examples.oneserverpersuite import org.scalatest._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ // This is the "master" suite class NestedExampleSpec extends Suites( new OneSpec, new TwoSpec, new RedSpec, new BlueSpec ) with GuiceOneServerPerSuite { // Override app if you need an Application with other than non-default parameters. override def fakeApplication(): Application = new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build() } // These are the nested suites @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredServer @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredServer @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredServer @DoNotDiscover class BlueSpec extends PlaySpec with ConfiguredServer { "The OneServerPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
- trait BaseOneServerPerTest extends TestSuiteMixin with ServerProvider
Trait that provides a new
Application
and runningTestServer
instance for each test executed in a ScalaTestSuite
.Trait that provides a new
Application
and runningTestServer
instance for each test executed in a ScalaTestSuite
.This
TestSuiteMixin
trait overrides ScalaTest'swithFixture
method to create a newApplication
andTestServer
before each test, and ensure they are cleaned up after the test has completed. TheApplication
is available (implicitly) from methodapp
. TheTestServer
's port number is available asport
(and implicitly available asportNumber
, wrapped in a PortNumber).By default, this trait creates a new
Application
for each test using default parameter values, which is returned by thenewAppForTest
method defined in this trait. If your tests need anApplication
with non-default parameters, overridenewAppForTest
to return it.Here's an example that demonstrates some of the services provided by this trait:
package org.scalatestplus.play.examples.oneserverpertest import org.scalatest._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ class ExampleSpec extends PlaySpec with OneServerPerTest { // Override newAppForTest or use GuiceOneServerPerTest implicit override def newAppForTest(testData: TestData): Application = new GuiceApplicationBuilder() .configure(Map("ehcacheplugin" -> "disabled")) .router(TestRoutes.router) .build() "The OneServerPerTest trait" must { "provide a FakeApplication" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
- trait BrowserFactory extends AnyRef
Trait that defines an abstract
createWebDriver
method for creating a new SeleniumWebDriver
and an abstractunableToCreateDriverErrorMessage
method that provides an appropriate error message if the driver is not available on the current platform.Trait that defines an abstract
createWebDriver
method for creating a new SeleniumWebDriver
and an abstractunableToCreateDriverErrorMessage
method that provides an appropriate error message if the driver is not available on the current platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits. - abstract class BrowserInfo extends AnyRef
Abstract class that encapsulates a browser name, tag name, and Selenium
WebDriver
factory method.Abstract class that encapsulates a browser name, tag name, and Selenium
WebDriver
factory method.This class is used by AllBrowsersPerSuite and AllBrowsersPerTest: an
IndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use.BrowserInfo
is not sealed so that you can extend it if you need other Browser types, for example, Firefox browsers with different profiles (English, Japanese, etc.). - trait ChromeFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumChromeDriver
, or UnavailableDriver, if Chrome is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumChromeDriver
, or UnavailableDriver, if Chrome is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asChromeFactory
. - case class ChromeInfo(service: ChromeDriverService = ChromeFactory.chromeDriverService, options: ChromeOptions = ChromeFactory.chromeOptions) extends BrowserInfo with Product with Serializable
Chrome browser info, which encapsulates the browser name,
"[Chrome]"
; tag name,org.scalatest.tags.ChromeBrowser
; and a factory method that produces a SeleniumChromeDriver
.Chrome browser info, which encapsulates the browser name,
"[Chrome]"
; tag name,org.scalatest.tags.ChromeBrowser
; and a factory method that produces a SeleniumChromeDriver
.This object's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use. - trait ConfiguredApp extends TestSuiteMixin
Trait that provides a configured
Application
to the suite into which it is mixed.Trait that provides a configured
Application
to the suite into which it is mixed.The purpose of this trait is to allow nested suites of an enclosing suite that extends GuiceOneAppPerSuite to make use of the
Application
provided byGuiceOneAppPerSuite
. TraitGuiceOneAppPerSuite
will ensure theApplication
is placed in theConfigMap
under the keyorg.scalatestplus.play.app
before nested suites are invoked. This represents the "configured application" that is passed from the enclosing suite to the nested suites. TraitConfiguredApp
extracts theApplication
from theConfigMap
and makes it available via theapp
method it provides.To prevent discovery of nested suites you can annotate them with
@DoNotDiscover
. Here's an example, taken fromGuiceOneAppPerSuite
's documentation:package org.scalatestplus.play.examples.oneapppersuite import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ class ExampleSpec extends PlaySpec with GuiceOneAppPerSuite { // Override app if you need an Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build() "The GuiceOneAppPerSuite trait" must { "provide a FakeApplication" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the FakeApplication available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } } }
- trait ConfiguredBrowser extends TestSuiteMixin with WebBrowser with Eventually with IntegrationPatience
Trait that provides a configured
Application
, server port number, and SeleniumWebDriver
to the suite into which it is mixed.Trait that provides a configured
Application
, server port number, and SeleniumWebDriver
to the suite into which it is mixed.The purpose of this trait is to allow nested suites of an enclosing suite that extends OneBrowserPerSuite to make use of the
WebDriver
provided byOneBrowserPerSuite
. TraitOneBrowserPerSuite
will ensure theWebDriver
is placed in theConfigMap
under the keyorg.scalatestplus.play.webDriver
before nested suites are invoked. This information represents the "configured browser" that is passed from the enclosing suite to the nested suites. TraitConfiguredBrowser
extracts this information from from theConfigMap
and makes theWebDriver
available implicitly from thewebDriver
method.This trait's self-type, ServerProvider, will ensure a
TestServer
andApplication
are available to each test. The self-type will require that you mix in either GuiceOneServerPerSuite, GuiceOneServerPerTest, ConfiguredServer before you mix in this trait. Your choice among these threeServerProvider
s will determine the extent to which one or moreTestServer
s are shared by multiple tests.To prevent discovery of nested suites you can annotate them with
@DoNotDiscover
. Here's an example taken from the documentation for trait OneBrowserPerSuite:package org.scalatestplus.play.examples.onebrowserpersuite import org.scalatest.tags.FirefoxBrowser import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ @FirefoxBrowser class ExampleSpec extends PlaySpec with OneServerPerSuite with OneBrowserPerSuite with FirefoxFactory { // Override fakeApplication() if you need a Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder() .configure("foo" -> "bar", "ehcacheplugin" -> "disabled") .router(TestRoutes.router) .build() "The OneBrowserPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } }
- trait ConfiguredServer extends TestSuiteMixin with ServerProvider
Trait that provides a configured
Application
and server port number to the suite into which it is mixed.Trait that provides a configured
Application
and server port number to the suite into which it is mixed.The purpose of this trait is to allow nested suites of an enclosing suite that extends GuiceOneServerPerSuite to make use of the
Application
and port number provided byOneServerPerSuite
. TraitOneServerPerSuite
will ensure theApplication
is placed in theConfigMap
under the keyorg.scalatestplus.play.app
and the port number under the keyorg.scalatestplus.play.port
before nested suites are invoked. This information represents the "configured server" that is passed from the enclosing suite to the nested suites. TraitConfiguredServer
extracts this information from from theConfigMap
and makes theApplication
available via theapp
method, the port number available as anInt
from theport
method, and also the port number wrapped in a PortNumber available as implicit methodportNumber
(for use with trait WsScalaTestClient).To prevent discovery of nested suites you can annotate them with
@DoNotDiscover
. Here's an example, taken fromGuiceOneAppPerSuite
's documentation:package org.scalatestplus.play.examples.oneserverpersuite import play.api.test._ import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite { // Override fakeApplication() if you need a Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder().configure("ehcacheplugin" -> "disabled").build() "The OneServerPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
- trait FakeApplicationFactory extends AnyRef
Trait that provides method that creates a new instance of
Application
to the functional test suite mixins.Trait that provides method that creates a new instance of
Application
to the functional test suite mixins.The
GuiceFakeApplicationFactory
provides aFakeApplicationFactory
that usesGuiceApplicationBuilder
to build an Application for test suites. - trait FirefoxFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumFirefoxDriver
(created using the profile specified byfirefoxProfile
), or UnavailableDriver, if Firefox is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumFirefoxDriver
(created using the profile specified byfirefoxProfile
), or UnavailableDriver, if Firefox is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asFirefoxFactory
. - case class FirefoxInfo(firefoxProfile: FirefoxProfile, firefoxOptions: FirefoxOptions = FirefoxFactory.firefoxOptions) extends BrowserInfo with Product with Serializable
Firefox browser info, which encapsulates the browser name,
"[Firefox]"
; tag name,org.scalatest.tags.FirefoxBrowser
; and a factory method that produces a SeleniumFirefoxDriver
.Firefox browser info, which encapsulates the browser name,
"[Firefox]"
; tag name,org.scalatest.tags.FirefoxBrowser
; and a factory method that produces a SeleniumFirefoxDriver
.This class's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use.- firefoxProfile
the
FirefoxProfile
to use when creating newFirefoxDriver
s in thecreateWebDriver
factory method.
- trait HtmlUnitFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumHtmlUnitDriver
, or UnavailableDriver, if HtmlUnit is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumHtmlUnitDriver
, or UnavailableDriver, if HtmlUnit is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asHtmlUnitFactory
. - case class HtmlUnitInfo(enableJavascript: Boolean) extends BrowserInfo with Product with Serializable
HtmlUnit
browser info, which encapsulates the browser name,"[HtmlUnit]"
; tag name,org.scalatest.tags.HtmlUnitBrowser
; and a factory method that produces a SeleniumHtmlUnitDriver
.HtmlUnit
browser info, which encapsulates the browser name,"[HtmlUnit]"
; tag name,org.scalatest.tags.HtmlUnitBrowser
; and a factory method that produces a SeleniumHtmlUnitDriver
.This object's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use. - trait InternetExplorerFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumInternetExplorerDriver
, or UnavailableDriver, if Internet Explorer is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumInternetExplorerDriver
, or UnavailableDriver, if Internet Explorer is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asInternetExplorerFactory
. - trait MixedFixtures extends TestSuiteMixin with UnitFixture
Trait that helps you provide different fixtures to different tests: a
Application
, aTestServer
, or one of the SeleniumWebDrivers
s.Trait that helps you provide different fixtures to different tests: a
Application
, aTestServer
, or one of the SeleniumWebDrivers
s.Trait
MixedFixtures
can be mixed into anyfixture.Suite
. For convenience it is mixed into MixedPlaySpec. In afixture.Suite
, tests can take a no-arg function.MixedFixtures
provides several no-arg function classes (classes extendingFunction0
) that can be used to provide different fixtures for different tests.If a test needs a
Application
, use theApp
function, like this:"provide an Application" in new App(fakeApp("ehcacheplugin" -> "disabled")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") }
If a test needs an
Application
and runningTestServer
, use theServer
function, 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 an
Application
, 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 theSafari
function:"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.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")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new App(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } } "The Server function" must { "provide an Application" in new Server(buildApp("ehcacheplugin" -> "disabled")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new Server(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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() } } "The HtmlUnit function" must { "provide an Application" in new HtmlUnit(buildApp("ehcacheplugin" -> "disabled")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new HtmlUnit(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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(buildApp()) { 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")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new Firefox(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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(buildApp()) { 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")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new Safari(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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(buildApp()) { 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")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new Chrome(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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(buildApp()) { 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")) { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in new InternetExplorer(buildApp("ehcacheplugin" -> "disabled")) { getConfig("ehcacheplugin") mustBe Some("disabled") } 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(buildApp()) { 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 } } }
- abstract class MixedPlaySpec extends WordSpec with MustMatchers with OptionValues with MixedFixtures with Eventually with IntegrationPatience with WsScalaTestClient
Convenience "super Suite" class for "mixed fixture" Play tests.
Convenience "super Suite" class for "mixed fixture" Play tests.
This class mixes in trait MixedFixtures, and is therefore convenient when different tests in the same test class need different kinds of fixtures. When different tests in the same class need the same fixture, you're probably better of extending PlaySpec instead.
- trait OneBrowserPerSuite extends TestSuiteMixin with WebBrowser with Eventually with IntegrationPatience with BrowserFactory
Trait that provides a new Selenium
WebDriver
instance per ScalaTestSuite
.Trait that provides a new Selenium
WebDriver
instance per ScalaTestSuite
.This
TestSuiteMixin
trait's overriddenrun
method places a reference to theWebDriver
provided bywebDriver
under the keyorg.scalatestplus.play.webDriver
. This allows any nestedSuite
s to access theSuite
'sWebDriver
as well, most easily by having the nestedSuite
s mix in the ConfiguredBrowser trait. On the status returned bysuper.run
, this trait's overriddenrun
method registers a block of code to close theWebDriver
to be executed when theStatus
completes, and returns the sameStatus
. This ensures theWebDriver
will continue to be available until all nested suites have completed, after which theWebDriver
will be closed. This trait also overridesSuite.withFixture
to cancel tests automatically if the relatedWebDriver
is not available on the host platform.This trait's self-type, ServerProvider, will ensure a
TestServer
andApplication
are available to each test. The self-type will require that you mix in either GuiceOneServerPerSuite, OneServerPerTest, ConfiguredServer before you mix in this trait. Your choice among these threeServerProvider
s will determine the extent to which one or moreTestServer
s are shared by multiple tests.Here's an example that shows demonstrates of the services provided by this trait. Note that to use this trait, you must mix in one of the driver factories (this example mixes in FirefoxFactory):
package org.scalatestplus.play.examples.onebrowserpersuite import org.scalatest.tags.FirefoxBrowser import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ @FirefoxBrowser class ExampleSpec extends PlaySpec with OneServerPerSuite with OneBrowserPerSuite with FirefoxFactory { // Override fakeApplication() if you need a Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder() .configure("foo" -> "bar", "ehcacheplugin" -> "disabled") .router(TestRoutes.router) .build() "The OneBrowserPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } }
If you have many tests that can share the same
Application
,TestServer
, andWebDriver
, and you don't want to put them all into one test class, you can place them into different "nested"Suite
classes. Create a master suite that extendsOneServerPerSuite
and declares the nestedSuite
s. Annotate the nested suites with@DoNotDiscover
and have them extendConfiguredBrowser
. Here's an example:package org.scalatestplus.play.examples.onebrowserpersuite import org.scalatest._ import tags.FirefoxBrowser import org.scalatestplus.play._ import play.api.{Play, Application} // This is the "master" suite class NestedExampleSpec extends Suites( new OneSpec, new TwoSpec, new RedSpec, new BlueSpec ) with OneServerPerSuite with OneBrowserPerSuite with FirefoxFactory { // Override fakeApplication() if you need a Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder( additionalConfiguration = Map("ehcacheplugin" -> "disabled"), withRoutes = TestRoute ).build() } // These are the nested suites @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser @DoNotDiscover class BlueSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser { "The OneBrowserPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } } }
It is possible to use
OneBrowserPerSuite
to run the same tests in more than one browser. Nevertheless, you should consider the approach taken by AllBrowsersPerSuite and AllBrowsersPerTest instead, as it requires a bit less boilerplate code thanOneBrowserPerSuite
to test in multiple browsers. If you prefer to useOneBrowserPerSuite
, however, simply place your tests in an abstract superclass, then define concrete subclasses for each browser you wish to test against. Here's an example:package org.scalatestplus.play.examples.onebrowserpersuite import play.api.test._ import org.scalatest._ import tags._ import org.scalatestplus.play._ import play.api.{Play, Application} // Place your tests in an abstract class abstract class MultiBrowserExampleSpec extends PlaySpec with OneServerPerSuite with OneBrowserPerSuite { // Override app if you need an Application with other than non-default parameters. def fakeApplication(): Application = new GuiceApplicationBuilder( additionalConfiguration = Map("ehcacheplugin" -> "disabled"), withRoutes = TestRoute ).build "The OneBrowserPerSuite trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } } // Then make a subclass that mixes in the factory for each // Selenium driver you want to test with. @FirefoxBrowser class FirefoxExampleSpec extends MultiBrowserExampleSpec with FirefoxFactory @SafariBrowser class SafariExampleSpec extends MultiBrowserExampleSpec with SafariFactory @InternetExplorerBrowser class InternetExplorerExampleSpec extends MultiBrowserExampleSpec with InternetExplorerFactory @ChromeBrowser class ChromeExampleSpec extends MultiBrowserExampleSpec with ChromeFactory @HtmlUnitBrowser class HtmlUnitExampleSpec extends MultiBrowserExampleSpec with HtmlUnitFactory
The concrete subclasses include tag annotations describing the browser used to make it easier to include or exclude browsers in specific runs. This is not strictly necessary since if a browser is not supported on the host platform the tests will be automatically canceled. For example, here's how the output would look if you ran the above tests on a platform that did not support Selenium drivers for Chrome or Internet Explorer using sbt:
> test-only *onebrowserpersuite* [info] FirefoxExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port number [info] - must provide an actual running server [info] - must provide a web driver [info] SafariExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port number [info] - must provide an actual running server [info] - must provide a web driver [info] InternetExplorerExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must make the Application available implicitly !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must start the Application !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide the port number !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide an actual running server !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide a web driver !!! CANCELED !!! [info] Was unable to create a Selenium InternetExplorerDriver on this platform. (OneBrowserPerSuite.scala:201) [info] ChromeExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must make the Application available implicitly !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must start the Application !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide the port number !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide an actual running server !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] - must provide a web driver !!! CANCELED !!! [info] Was unable to create a Selenium ChromeDriver on this platform. (OneBrowserPerSuite.scala:201) [info] HtmlUnitExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port number [info] - must provide an actual running server [info] - must provide a web driver
For comparison, here is what the output would look like if you just selected tests tagged with
FirefoxBrowser
in sbt:> test-only *onebrowserpersuite* -- -n org.scalatest.tags.FirefoxBrowser [info] FirefoxExampleSpec: [info] The OneBrowserPerSuite trait [info] - must provide an Application [info] - must make the Application available implicitly [info] - must start the Application [info] - must provide the port number [info] - must provide an actual running server [info] - must provide a web driver [info] SafariExampleSpec: [info] The OneBrowserPerSuite trait [info] InternetExplorerExampleSpec: [info] The OneBrowserPerSuite trait [info] ChromeExampleSpec: [info] The OneBrowserPerSuite trait [info] HtmlUnitExampleSpec: [info] The OneBrowserPerSuite trait
- trait OneBrowserPerTest extends TestSuiteMixin with WebBrowser with Eventually with IntegrationPatience with BrowserFactory
Trait that provides a new Selenium
WebDriver
instance for each test executed in a ScalaTestSuite
.Trait that provides a new Selenium
WebDriver
instance for each test executed in a ScalaTestSuite
.This trait overrides ScalaTest's
withFixture
method to create a newWebDriver
instance before each test, and ensure it is closed after the test has completed. TheWebDriver
is available (implicitly) from methodwebDriver
.This trait's self-type, ServerProvider, will ensure a
TestServer
andApplication
are available to each test. The self-type will require that you mix in either GuiceOneServerPerSuite, OneServerPerTest, ConfiguredServer before you mix in this trait. Your choice among these threeServerProvider
s will determine the extent to which one or moreTestServer
s are shared by multiple tests.Here's an example that shows demonstrates of the services provided by this trait. Note that to use this trait, you must mix in one of the driver factories (this example mixes in FirefoxFactory):
package org.scalatestplus.play.examples.onebrowserpertest import org.scalatest._ import org.scalatest.tags.FirefoxBrowser import org.scalatestplus.play._ import play.api.{Play, Application} import play.api.inject.guice._ import play.api.routing._ @FirefoxBrowser class ExampleSpec extends PlaySpec with OneServerPerTest with OneBrowserPerTest with FirefoxFactory { // Override newAppForTest if you need an Application with other than non-default parameters. override def newAppForTest(testData: TestData): Application = new GuiceApplicationBuilder() .configure(Map("ehcacheplugin" -> "disabled")) .router(TestRoutes.router) .build() "The OneBrowserPerTest trait" must { "provide an Application" in { app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) getConfig("ehcacheplugin") mustBe Some("disabled") } "provide an http endpoint" in { runningServer.endpoints.httpEndpoint must not be empty } "provide an actual running server" in { import Helpers._ import java.net._ val url = new URL("http://localhost:" + port + "/boum") val con = url.openConnection().asInstanceOf[HttpURLConnection] try con.getResponseCode mustBe 404 finally con.disconnect() } "provide a web driver" in { go to ("http://localhost:" + port + "/testing") pageTitle mustBe "Test Page" click on find(name("b")).value eventually { pageTitle mustBe "scalatest" } } } }
- abstract class PlaySpec extends WordSpec with MustMatchers with OptionValues with WsScalaTestClient
Convenience "super Suite" base class for Play tests.
Convenience "super Suite" base class for Play tests.
Extend this class by default for testing Play apps with the ScalaTest + Play library. You can mix other traits into it to access needed fixtures, such as GuiceOneAppPerSuite, GuiceOneAppPerTest, GuiceOneServerPerSuite, GuiceOneServerPerTest, OneBrowserPerSuite, OneBrowserPerTest, AllBrowsersPerSuite, or AllBrowsersPerTest mix If you want to use trait MixedFixtures, extend MixedPlaySpec instead.
- case class PortNumber(value: Int) extends Product with Serializable
Wraps a port number of a provided
TestServer
so that it can be made available as an implicit without making anInt
implicit.Wraps a port number of a provided
TestServer
so that it can be made available as an implicit without making anInt
implicit.An implicit
PortNumber
is made available by traits that provide aplay.api.test.TestServer
: MixedFixtures, OneBrowserPerSuite, OneBrowserPerTest, GuiceOneServerPerSuite, and OneServerPerTest.The implicit
PortNumber
is taken by the methods of WsScalaTestClient. - trait SafariFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumSafariDriver
, or UnavailableDriver, if Safari is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumSafariDriver
, or UnavailableDriver, if Safari is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asSafariFactory
. - trait ServerProvider extends AnyRef
Trait that defines abstract methods that providing a port number and implicit
Application
and a concrete method that provides an implicit PortNumber that wraps the port number.Trait that defines abstract methods that providing a port number and implicit
Application
and a concrete method that provides an implicit PortNumber that wraps the port number.This trait is implemented by OneServerPerSuite, OneServerPerTest, and ConfiguredServer, each of which use a different strategy to provide
TestServer
s to tests. This trait is included in the self-type of OneBrowserPerSuite, and OneBrowserPerTest, and AllBrowsersPerTest, allowing you to select theWebDriver
strategy (i.e., the extent to whichWebDriver
s are shared between tests) independently from theTestServer
strategy (the extent to whichTestServer
s are shared between tests). - trait WsScalaTestClient extends AnyRef
Trait providing convenience methods to create WS requests in tests.
Deprecated Type Members
- trait OneAppPerSuite extends GuiceOneAppPerSuite
Synonym for GuiceOneAppPerSuite.
Synonym for GuiceOneAppPerSuite.
- Annotations
- @deprecated
- Deprecated
(Since version 2.0.0) Use GuiceOneAppPerSuite instead
- trait OneAppPerTest extends GuiceOneAppPerTest
Synonym for GuiceOneAppPerTest
Synonym for GuiceOneAppPerTest
- Annotations
- @deprecated
- Deprecated
(Since version 2.0.0) Use GuiceOneAppPerTest instead
- trait OneServerPerSuite extends GuiceOneServerPerSuite
Synonym for GuiceOneServerPerSuite.
Synonym for GuiceOneServerPerSuite.
- Annotations
- @deprecated
- Deprecated
(Since version 2.0.0) Use GuiceOneServerPerSuite instead
- trait OneServerPerTest extends GuiceOneServerPerTest
Synonym for GuiceOneServerPerTest
Synonym for GuiceOneServerPerTest
- Annotations
- @deprecated
- Deprecated
(Since version 2.0.0) Use GuiceOneServerPerTest instead
- trait PhantomJSFactory extends BrowserFactory
Factory whose
createWebDriver
method will either return a new SeleniumPhantomJSDriver
(created using the capabilities specified byphantomCapabilities
), or UnavailableDriver, if Firefox is not available on the host platform.Factory whose
createWebDriver
method will either return a new SeleniumPhantomJSDriver
(created using the capabilities specified byphantomCapabilities
), or UnavailableDriver, if Firefox is not available on the host platform.Traits OneBrowserPerSuite and OneBrowserPerTest extend
BrowserFactory
and therefore require you to fill in thecreateWebDriver
method, usually by mixing in one of theBrowserFactory
subtraits such asPhantomJSFactory
.- Annotations
- @deprecated
- Deprecated
(Since version 4.0.0) PhantomJS is no longer actively developed, and support will eventually be dropped
- case class PhantomJSInfo(phantomCapabilities: DesiredCapabilities = DesiredCapabilities.phantomjs()) extends BrowserInfo with Product with Serializable
PhantomJS browser info, which encapsulates the browser name,
"[PhantomJS]"
; tag name,org.scalatest.tags.PhantomJS
; and a factory method that produces a Selenium org.openqa.selenium.phantomjs.PhantomJSDriver.PhantomJS browser info, which encapsulates the browser name,
"[PhantomJS]"
; tag name,org.scalatest.tags.PhantomJS
; and a factory method that produces a Selenium org.openqa.selenium.phantomjs.PhantomJSDriver.This class's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use.- phantomCapabilities
the DesiredCapabilities to use when creating new org.openqa.selenium.phantomjs.PhantomJSDriver in the
createWebDriver
factory method.
- Annotations
- @deprecated
- Deprecated
(Since version 4.0.0) PhantomJS is no longer actively developed, and support will eventually be dropped
Value Members
- object BrowserFactory
Companion object to trait
BrowserFactory
that holds aUnavailableDriver
object that implements the SeleniumWebDriver
interface by throwingUnsupportedOperationException
.Companion object to trait
BrowserFactory
that holds aUnavailableDriver
object that implements the SeleniumWebDriver
interface by throwingUnsupportedOperationException
. This is used as a placeholder when a driver is not available on the host platform. - object ChromeFactory extends ChromeFactory
Companion object to trait
ChromeFactory
that mixes in the trait. - object FirefoxFactory extends FirefoxFactory
Companion object to trait
FirefoxFactory
that mixes in the trait. - object HtmlUnitFactory extends HtmlUnitFactory
Companion object to trait
HtmlUnitFactory
that mixes in the trait. - object InternetExplorerFactory extends InternetExplorerFactory
Companion object to trait
InternetExplorerFactory
that mixes in the trait. - object InternetExplorerInfo extends BrowserInfo with Product with Serializable
Internet Explorer browser info, which encapsulates the browser name,
"[InternetExplorer]"
; tag name,org.scalatest.tags.InternetExplorerBrowser
; and a factory method that produces a SeleniumInternetExplorerDriver
.Internet Explorer browser info, which encapsulates the browser name,
"[InternetExplorer]"
; tag name,org.scalatest.tags.InternetExplorerBrowser
; and a factory method that produces a SeleniumInternetExplorerDriver
.This object's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use. - object SafariFactory extends SafariFactory
Companion object to trait
SafariFactory
that mixes in the trait. - object SafariInfo extends BrowserInfo with Product with Serializable
Safari browser info, which encapsulates the browser name,
"[Safari]"
; tag name,org.scalatest.tags.SafariBrowser
; and a factory method that produces a SeleniumSafariDriver
.Safari browser info, which encapsulates the browser name,
"[Safari]"
; tag name,org.scalatest.tags.SafariBrowser
; and a factory method that produces a SeleniumSafariDriver
.This object's superclass,
BrowserInfo
, is used by AllBrowsersPerSuite and AllBrowsersPerTest: anIndexedSeq[BrowserInfo]
is returned from thebrowsers
field of these traits to specify the browsers to share between tests. When tests are registered,AllBrowsersPerSuite
andAllBrowsersPerTest
use the browser name to ensure the tests shared by multiple browsers have unique names (the name of each shared test is appended with a browser name). When the tests run, these traits use theBrowserInfo
's factory method to createWebDriver
s as needed. TheAllBrowsersPerSuite
andAllBrowsersPerTest
traits use the tag name to automatically tag any tests that use a particularWebDriver
with the appropriate tag so that tests can be dynamically filtered by the browser the use.
Deprecated Value Members
- object PhantomJSFactory extends PhantomJSFactory
Companion object to trait
PhantomJSFactory
that mixes in the trait.Companion object to trait
PhantomJSFactory
that mixes in the trait.- Annotations
- @deprecated
- Deprecated
(Since version 4.0.0) PhantomJS is no longer actively developed, and support will eventually be dropped