Registers tests "shared" by multiple browsers.
Registers tests "shared" by multiple browsers.
Implement this method by placing tests you wish to run for multiple browsers. This method
will be called during the initialization of this trait once for each browser whose BrowserInfo
appears in the IndexedSeq referenced from the browsers field.
Make sure you append browser.name to each test declared in sharedTests, to ensure they
all have unique names. Here's an example:
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")
}
If you don't append browser.name to each test name you'll likely be rewarded with
a DuplicateTestNameException when you attempt to run the suite.
the passed in BrowserInfo instance
Info for available browsers.
Info for available browsers. Override to add in custom BrowserInfo implementations.
Method to provide FirefoxProfile for creating FirefoxDriver, you can override this method to
provide a customized instance of FirefoxProfile
Method to provide FirefoxProfile for creating FirefoxDriver, you can override this method to
provide a customized instance of FirefoxProfile
an instance of FirefoxProfile
Automatically tag browser tests with browser tags based on the test name: if a test ends in a browser name in square brackets, it will be tagged as using that browser.
Automatically tag browser tests with browser tags based on the test name: if a test ends in a browser
name in square brackets, it will be tagged as using that browser. For example, if a test name
ends in [Firefox], it will be tagged with org.scalatest.tags.FirefoxBrowser. The browser tags will be merged with
tags returned from super.tags, so no existing tags will be lost when the browser tags are added.
super.tags with additional browser tags added for any browser-specific tests
Implicit method to get the WebDriver for the current test.
Inspects the current test name and if it ends with the name of one of the BrowserInfos
mentioned in the browsers IndexedSeq, creates a new web driver by invoking createWebDriver on
that BrowserInfo and, unless it is an UnavailableDriver, installs it so it will be returned by
webDriver during the test.
Inspects the current test name and if it ends with the name of one of the BrowserInfos
mentioned in the browsers IndexedSeq, creates a new web driver by invoking createWebDriver on
that BrowserInfo and, unless it is an UnavailableDriver, installs it so it will be returned by
webDriver during the test. (If the driver is unavailable on the host platform, the createWebDriver
method will return UnavailableDriver, and this withFixture implementation will cancel the test
automatically.) If the current test name does not end in a browser name, this withFixture method
installs BrowserInfo.UnneededDriver as the driver to be returned by webDriver during the test.
If the test is not canceled because of an unavailable driver, this withFixture method invokes
super.withFixture and ensures that the WebDriver is closed after super.withFixture returns.
the no-arg test function to run with a fixture
the Outcome of the test execution
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
AllBrowsersPerSuitewill allow you to write tests that rely on maintaining browser state between the tests. Thus,AllBrowsersPerSuiteis 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'swithFixturelifecycle method to create a newWebDriverinstance before executing each test that needs a browser, closing it after the test completes, and overrides thetagslifecycle method to tag the shared tests so you can filter them by browser type. This trait's self-type, ServerProvider, will ensure aTestServerandApplicationare 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 aTestServeris shared by multiple tests.You'll need to place any tests that you want executed by multiple browsers in a
sharedTestsmethod. Because all tests in a ScalaTestSuitemust have unique names, you'll need to append the browser name (available from theBrowserInfopassed 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
sharedTestswill be registered for each desiredWebDriver, as specified by thebrowsersfield. When running, any tests for browser drivers that are unavailable on the current platform will be canceled. All tests registered undersharedTestswill 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
browsersand excluding itsBrowserInfoin 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 play.api.test._ 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.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(Router.from(TestRoute)) .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.getString("foo") mustBe Some("bar") } "make the FakeApplication available implicitly" in { def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key) getConfig("foo") mustBe Some("bar") } "start the FakeApplication" in { Play.maybeApplication mustBe Some(app) } "provide the port" 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() } } }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:
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: