Creates a new instance of a valid Selenium WebDriver, or if a driver is unavailable on the host platform,
returns a BrowserFactory.UnavailableDriver that includes
the exception that indicated the driver was not supported on the host platform and an appropriate
error message.
Creates a new instance of a valid Selenium WebDriver, or if a driver is unavailable on the host platform,
returns a BrowserFactory.UnavailableDriver that includes
the exception that indicated the driver was not supported on the host platform and an appropriate
error message.
an new instance of a Selenium WebDriver, or a BrowserFactory.UnavailableDriver
if the desired WebDriver is not available on the host platform.
Places the WebDriver provided by webDriver into the ConfigMap under the key
org.scalatestplus.play.webDriver to make
it available to nested suites; calls super.run; and lastly ensures the WebDriver is stopped after
all tests and nested suites have completed.
Places the WebDriver provided by webDriver into the ConfigMap under the key
org.scalatestplus.play.webDriver to make
it available to nested suites; calls super.run; and lastly ensures the WebDriver is stopped after
all tests and nested suites have completed.
an optional name of one test to run. If None, all relevant tests should be run.
I.e., None acts like a wildcard that means run all relevant tests in this Suite.
the Args for this run
a Status object that indicates when all tests and nested suites started by this method have completed, and whether or not a failure occurred.
An implicit instance of WebDriver, created by calling createWebDriver.
An implicit instance of WebDriver, created by calling createWebDriver.
If there is an error when creating the WebDriver, UnavailableDriver will be assigned
instead.
Automatically cancels tests with an appropriate error message when the webDriver field is a UnavailableDriver,
else calls super.withFixture(test)
Automatically cancels tests with an appropriate error message when the webDriver field is a UnavailableDriver,
else calls super.withFixture(test)
Trait that provides a new Selenium
WebDriverinstance per ScalaTestSuite.This
TestSuiteMixintrait's overriddenrunmethod places a reference to theWebDriverprovided bywebDriverunder the keyorg.scalatestplus.play.webDriver. This allows any nestedSuites to access theSuite'sWebDriveras well, most easily by having the nestedSuites mix in the ConfiguredBrowser trait. On the status returned bysuper.run, this trait's overriddenrunmethod registers a block of code to close theWebDriverto be executed when theStatuscompletes, and returns the sameStatus. This ensures theWebDriverwill continue to be available until all nested suites have completed, after which theWebDriverwill be closed. This trait also overridesSuite.withFixtureto cancel tests automatically if the relatedWebDriveris not available on the host platform.This trait's self-type, ServerProvider, will ensure a
TestServerandApplicationare 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 threeServerProviders will determine the extent to which one or moreTestServers 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 play.api.test.Helpers 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(Router.from(TestRoute)) .build() "The OneBrowserPerSuite trait" must { "provide an Application" in { app.configuration.getString("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key) getConfig("ehcacheplugin") mustBe Some("disabled") } "start the Application" in { Play.maybeApplication mustBe Some(app) } "provide the port number" in { port mustBe Helpers.testServerPort } "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"Suiteclasses. Create a master suite that extendsOneServerPerSuiteand declares the nestedSuites. Annotate the nested suites with@DoNotDiscoverand have them extendConfiguredBrowser. Here's an example:package org.scalatestplus.play.examples.onebrowserpersuite import play.api.test._ 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.getString("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key) getConfig("ehcacheplugin") mustBe Some("disabled") } "start the Application" in { Play.maybeApplication mustBe Some(app) } "provide the port number" in { port mustBe Helpers.testServerPort } "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
OneBrowserPerSuiteto 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 thanOneBrowserPerSuiteto 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.getString("ehcacheplugin") mustBe Some("disabled") } "make the Application available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key) getConfig("ehcacheplugin") mustBe Some("disabled") } "start the Application" in { Play.maybeApplication mustBe Some(app) } "provide the port number" in { port mustBe Helpers.testServerPort } "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 HtmlUnitFactoryThe 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:
For comparison, here is what the output would look like if you just selected tests tagged with
FirefoxBrowserin sbt: