├── .git-blame-ignore-revs ├── .github ├── dependabot.yml ├── mergify.yml ├── scala-steward.conf └── workflows │ ├── build-test.yml │ ├── dependency-graph.yml │ ├── publish.yml │ └── release-drafter.yml ├── .gitignore ├── .scalafmt.conf ├── LICENSE ├── README.md ├── build.sbt ├── docs └── manual │ └── working │ └── scalaGuide │ └── main │ └── tests │ ├── ScalaFunctionalTestingWithScalaTest.md │ ├── ScalaTestingWithComponents.md │ ├── ScalaTestingWithScalaTest.md │ └── code │ ├── ExampleControllerSpec.scala │ ├── ExampleEssentialActionSpec.scala │ ├── ExampleMockitoSpec.scala │ ├── ScalaFunctionalTestSpec.scala │ ├── StackSpec.scala │ ├── UserServiceSpec.scala │ ├── allbrowserspersuite │ ├── ExampleOverrideBrowsersSpec.scala │ └── ExampleSpec.scala │ ├── allbrowserspertest │ └── ExampleSpec.scala │ ├── mixedfixtures │ └── ExampleSpec.scala │ ├── models │ └── User.scala │ ├── oneapppersuite │ ├── ExampleComponentsSpec.scala │ ├── ExampleSpec.scala │ └── NestedExampleComponentsSpec.scala │ ├── oneapppertest │ ├── ExampleComponentsSpec.scala │ ├── ExamplePreDefinedComponentsSpec.scala │ ├── ExamplePreDefinedOverrideComponentsSpec.scala │ ├── ExampleSpec.scala │ └── SomeAppComponents.scala │ ├── onebrowserpersuite │ └── ExampleSpec.scala │ ├── onebrowserpertest │ └── ExampleSpec.scala │ ├── oneserverpersuite │ ├── ExampleComponentsSpec.scala │ ├── ExampleSpec.scala │ └── NestedExampleComponentsSpec.scala │ ├── oneserverpertest │ ├── ExampleComponentsSpec.scala │ └── ExampleSpec.scala │ ├── playspec │ └── ExampleSpec.scala │ ├── services │ ├── UserRepository.scala │ └── UserService.scala │ └── views │ └── html │ └── index.scala ├── module └── src │ ├── main │ ├── resources │ │ └── org │ │ │ └── scalatestplus │ │ │ └── play │ │ │ └── ScalaTestPlusPlayBundle.properties │ └── scala │ │ └── org │ │ └── scalatestplus │ │ └── play │ │ ├── AllBrowsersPerSuite.scala │ │ ├── AllBrowsersPerTest.scala │ │ ├── AppProvider.scala │ │ ├── BaseOneAppPerSuite.scala │ │ ├── BaseOneAppPerTest.scala │ │ ├── BaseOneServerPerSuite.scala │ │ ├── BaseOneServerPerTest.scala │ │ ├── BrowserFactory.scala │ │ ├── BrowserInfo.scala │ │ ├── ChromeFactory.scala │ │ ├── ConfiguredApp.scala │ │ ├── ConfiguredBrowser.scala │ │ ├── ConfiguredServer.scala │ │ ├── EdgeFactory.scala │ │ ├── FakeApplicationFactory.scala │ │ ├── FirefoxFactory.scala │ │ ├── HtmlUnitFactory.scala │ │ ├── InternetExplorerFactory.scala │ │ ├── MixedFixtures.scala │ │ ├── MixedPlaySpec.scala │ │ ├── OneAppPerSuite.scala │ │ ├── OneAppPerTest.scala │ │ ├── OneBrowserPerSuite.scala │ │ ├── OneBrowserPerTest.scala │ │ ├── OneServerPerSuite.scala │ │ ├── OneServerPerTest.scala │ │ ├── PlaySpec.scala │ │ ├── PortNumber.scala │ │ ├── Resources.scala │ │ ├── SafariFactory.scala │ │ ├── ServerProvider.scala │ │ ├── WsScalaTestClient.scala │ │ ├── components │ │ ├── OneAppPerSuiteWithComponents.scala │ │ ├── OneAppPerTestWithComponents.scala │ │ ├── OneServerPerSuiteWithComponents.scala │ │ ├── OneServerPerTestWithComponents.scala │ │ └── WithApplicationComponents.scala │ │ └── guice │ │ ├── GuiceFakeApplicationFactory.scala │ │ ├── GuiceOneAppPerSuite.scala │ │ ├── GuiceOneAppPerTest.scala │ │ ├── GuiceOneServerPerSuite.scala │ │ └── GuiceOneServerPerTest.scala │ └── test │ ├── resources │ └── logback.xml │ └── scala │ └── org │ └── scalatestplus │ └── play │ ├── AllBrowsersPerSuiteBehaviorSpec.scala │ ├── AllBrowsersPerTestBehaviorSpec.scala │ ├── AppSpec.scala │ ├── AppSpecSpec.scala │ ├── ChromeFactorySpec.scala │ ├── ConfiguredAppSpec.scala │ ├── ConfiguredBrowserNestedSuite.scala │ ├── ConfiguredServerNestedSuite.scala │ ├── ConfiguredServerSpec.scala │ ├── ConfiguredServerWithAllBrowsersPerSuiteSpec.scala │ ├── ConfiguredServerWithAllBrowsersPerTestSpec.scala │ ├── ConfiguredServerWithConfiguredBrowserSpec.scala │ ├── ConfiguredServerWithOneBrowserPerSuiteSpec.scala │ ├── ConfiguredServerWithOneBrowserPerTestSpec.scala │ ├── EdgeFactorySpec.scala │ ├── EventRecordingReporter.scala │ ├── HtmlUnitFactorySpec.scala │ ├── InternetExplorerFactorySpec.scala │ ├── MixedFixtureSpec.scala │ ├── MixedFixturesWsScalaTestClientSpec.scala │ ├── MixedPlaySpecSpec.scala │ ├── MixedSpec.scala │ ├── OneAppPerSuiteComponentSpec.scala │ ├── OneAppPerSuiteSpec.scala │ ├── OneAppPerTestComponentSpec.scala │ ├── OneAppPerTestSpec.scala │ ├── OneChromeBrowserPerTestSpec.scala │ ├── OneHtmlUnitBrowserPerTestSpec.scala │ ├── OneInternetExplorerBrowserPerTestSpec.scala │ ├── OneSafariBrowserPerTestSpec.scala │ ├── OneServerPerSuiteComponentSpec.scala │ ├── OneServerPerSuiteSpec.scala │ ├── OneServerPerSuiteWithAllBrowsersPerSuiteSpec.scala │ ├── OneServerPerSuiteWithAllBrowsersPerTestSpec.scala │ ├── OneServerPerSuiteWithConfiguredBrowserSpec.scala │ ├── OneServerPerSuiteWithOneBrowserPerSuiteSpec.scala │ ├── OneServerPerSuiteWithOneBrowserPerTestSpec.scala │ ├── OneServerPerTestComponentShutdownSpec.scala │ ├── OneServerPerTestComponentSpec.scala │ ├── OneServerPerTestComponentSynchronizationSpec.scala │ ├── OneServerPerTestSpec.scala │ ├── OneServerPerTestWithAllBrowsersPerSuiteSpec.scala │ ├── OneServerPerTestWithAllBrowsersPerTestSpec.scala │ ├── OneServerPerTestWithConfiguredBrowserSpec.scala │ ├── OneServerPerTestWithOneBrowserPerSuiteSpec.scala │ ├── OneServerPerTestWithOneBrowserPerTestSpec.scala │ ├── PlaySpecSpec.scala │ ├── SafariFactorySpec.scala │ ├── ServerSpec.scala │ ├── ServerSpecSpec.scala │ ├── SilentReporter.scala │ ├── TestRoute.scala │ ├── UnitSpec.scala │ ├── WsScalaTestClientSpec.scala │ └── examples │ ├── components │ ├── SomeAppComponents.scala │ ├── oneapppersuite │ │ ├── ExampleSpec.scala │ │ └── NestedExampleSpec.scala │ ├── oneapppertest │ │ ├── ExamplePreDefinedOverrideSpec.scala │ │ ├── ExamplePreDefinedSpec.scala │ │ └── ExampleSpec.scala │ ├── oneserverpersuite │ │ ├── ExampleSpec.scala │ │ └── NestedExampleSpec.scala │ └── oneserverpertest │ │ └── ExampleSpec.scala │ └── guice │ ├── allbrowserspersuite │ └── ExampleSpec.scala │ ├── allbrowserspertest │ └── ExampleSpec.scala │ ├── mixedfixtures │ └── ExampleSpec.scala │ ├── oneapppersuite │ ├── ExampleSpec.scala │ └── NestedExampleSpec.scala │ ├── oneapppertest │ └── ExampleSpec.scala │ ├── onebrowserpersuite │ ├── ExampleSpec.scala │ ├── MultiBrowserExampleSpec.scala │ └── NestedExampleSpec.scala │ ├── onebrowserpertest │ └── ExampleSpec.scala │ ├── oneserverpersuite │ ├── ExampleSpec.scala │ └── NestedExampleSpec.scala │ └── oneserverpertest │ └── ExampleSpec.scala └── project ├── Omnidoc.scala ├── Playdoc.scala ├── build.properties └── plugins.sbt /.git-blame-ignore-revs: -------------------------------------------------------------------------------- 1 | # scalafmt 2 | 492dac113216acbd8b17c2193ae39092ec676d24 3 | cd1bdd955fbd8472249f930b2ddd971e3ebd853e 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | target-branch: "7.0.x" 12 | commit-message: 13 | prefix: "[7.0.x] " 14 | - package-ecosystem: "github-actions" 15 | directory: "/" 16 | schedule: 17 | interval: "weekly" 18 | target-branch: "6.0.x" 19 | commit-message: 20 | prefix: "[6.0.x] " 21 | -------------------------------------------------------------------------------- /.github/mergify.yml: -------------------------------------------------------------------------------- 1 | extends: .github 2 | -------------------------------------------------------------------------------- /.github/scala-steward.conf: -------------------------------------------------------------------------------- 1 | commits.message = "${artifactName} ${nextVersion} (was ${currentVersion})" 2 | 3 | pullRequests.grouping = [ 4 | { name = "patches", "title" = "Patch updates", "filter" = [{"version" = "patch"}] } 5 | ] 6 | 7 | updates.pin = [ 8 | ] 9 | -------------------------------------------------------------------------------- /.github/workflows/build-test.yml: -------------------------------------------------------------------------------- 1 | name: Check 2 | 3 | on: 4 | pull_request: 5 | 6 | push: 7 | branches: 8 | - main # Check branch after merge 9 | 10 | concurrency: 11 | # Only run once for latest commit per ref and cancel other (previous) runs. 12 | group: ci-${{ github.ref }} 13 | cancel-in-progress: true 14 | 15 | jobs: 16 | check-code-style: 17 | name: Code Style 18 | uses: playframework/.github/.github/workflows/cmd.yml@v4 19 | with: 20 | cmd: sbt validateCode 21 | 22 | check-binary-compatibility: 23 | name: Binary Compatibility 24 | uses: playframework/.github/.github/workflows/binary-check.yml@v4 25 | 26 | check-docs: 27 | name: Docs 28 | uses: playframework/.github/.github/workflows/cmd.yml@v4 29 | with: 30 | cmd: | 31 | sbt docs/validateDocs docs/scalafmtCheckAll docs/scalafmtSbtCheck docs/test 32 | 33 | tests: 34 | name: Tests 35 | needs: 36 | - "check-code-style" 37 | - "check-binary-compatibility" 38 | - "check-docs" 39 | uses: playframework/.github/.github/workflows/cmd.yml@v4 40 | with: 41 | java: 21, 17 42 | scala: 2.13.x, 3.x 43 | cmd: | 44 | export DISPLAY=:99 45 | sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional 46 | export MOZ_HEADLESS=1 47 | sbt ++$MATRIX_SCALA test 48 | 49 | finish: 50 | name: Finish 51 | if: github.event_name == 'pull_request' 52 | needs: # Should be last 53 | - "tests" 54 | uses: playframework/.github/.github/workflows/rtm.yml@v4 55 | -------------------------------------------------------------------------------- /.github/workflows/dependency-graph.yml: -------------------------------------------------------------------------------- 1 | name: Dependency Graph 2 | on: 3 | push: 4 | branches: 5 | - main 6 | 7 | concurrency: 8 | # Only run once for latest commit per ref and cancel other (previous) runs. 9 | group: dependency-graph-${{ github.ref }} 10 | cancel-in-progress: true 11 | 12 | permissions: 13 | contents: write # this permission is needed to submit the dependency graph 14 | 15 | jobs: 16 | dependency-graph: 17 | name: Submit dependencies to GitHub 18 | runs-on: ubuntu-latest 19 | steps: 20 | - uses: actions/checkout@v4 21 | with: 22 | fetch-depth: 0 23 | ref: ${{ inputs.ref }} 24 | - uses: sbt/setup-sbt@v1 25 | - uses: scalacenter/sbt-dependency-submission@v3 26 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | branches: # Snapshots 6 | - main 7 | tags: ["**"] # Releases 8 | 9 | jobs: 10 | publish-artifacts: 11 | name: Publish / Artifacts 12 | uses: playframework/.github/.github/workflows/publish.yml@v4 13 | secrets: inherit 14 | -------------------------------------------------------------------------------- /.github/workflows/release-drafter.yml: -------------------------------------------------------------------------------- 1 | name: Release Drafter 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | update_release_draft: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: release-drafter/release-drafter@v6 13 | with: 14 | name: "ScalaTest+ Play $RESOLVED_VERSION" 15 | config-name: release-drafts/increasing-major-version.yml # located in .github/ in the default branch within this or the .github repo 16 | commitish: ${{ github.ref_name }} 17 | env: 18 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | target/ 3 | .idea 4 | .bsp/ 5 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | # This file was originally copied from https://github.com/playframework/playframework/blob/main/.scalafmt.conf 2 | runner.dialect = Scala213Source3 3 | align.preset = true 4 | assumeStandardLibraryStripMargin = true 5 | danglingParentheses.preset = true 6 | docstrings.style = keep 7 | maxColumn = 120 8 | project.git = true 9 | rewrite.rules = [ AvoidInfix, ExpandImportSelectors, RedundantParens, SortModifiers, PreferCurlyFors ] 10 | rewrite.sortModifiers.order = [ "private", "protected", "final", "sealed", "abstract", "implicit", "override", "lazy" ] 11 | spaces.inImportCurlyBraces = true # more idiomatic to include whitepsace in import x.{ yyy } 12 | trailingCommas = preserve 13 | version = 3.9.4 14 | rewrite.scala3.convertToNewSyntax = true 15 | rewrite.scala3.newSyntax.control = false 16 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/ExampleControllerSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest 5 | 6 | // #scalatest-examplecontrollerspec 7 | import scala.concurrent.Future 8 | 9 | import org.scalatestplus.play.* 10 | 11 | import play.api.mvc.* 12 | import play.api.test.* 13 | import play.api.test.Helpers.* 14 | 15 | class ExampleControllerSpec extends PlaySpec with Results { 16 | 17 | "Example Page#index" should { 18 | "should be valid" in { 19 | val controller = new ExampleController(Helpers.stubControllerComponents()) 20 | val result: Future[Result] = controller.index().apply(FakeRequest()) 21 | val bodyText: String = contentAsString(result) 22 | bodyText mustBe "ok" 23 | } 24 | } 25 | } 26 | // #scalatest-examplecontrollerspec 27 | 28 | // #scalatest-examplecontroller 29 | class ExampleController(val controllerComponents: ControllerComponents) extends BaseController { 30 | def index() = Action { 31 | Ok("ok") 32 | } 33 | } 34 | // #scalatest-examplecontroller 35 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/ExampleEssentialActionSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest 5 | 6 | import org.apache.pekko.stream.Materializer 7 | import org.scalatestplus.play.* 8 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 9 | import play.api.libs.json.Json 10 | import play.api.mvc.Results.* 11 | import play.api.mvc.* 12 | import play.api.test.Helpers.* 13 | import play.api.test.* 14 | 15 | // #scalatest-exampleessentialactionspec 16 | class ExampleEssentialActionSpec extends PlaySpec with GuiceOneAppPerSuite { 17 | 18 | implicit lazy val materializer: Materializer = app.materializer 19 | implicit lazy val Action: DefaultActionBuilder = app.injector.instanceOf(classOf[DefaultActionBuilder]) 20 | 21 | "An essential action" should { 22 | "can parse a JSON body" in { 23 | val action: EssentialAction = Action { request => 24 | val value = (request.body.asJson.get \ "field").as[String] 25 | Ok(value) 26 | } 27 | 28 | val request = FakeRequest(POST, "/").withJsonBody(Json.parse("""{ "field": "value" }""")) 29 | 30 | val result = call(action, request) 31 | 32 | status(result) mustEqual OK 33 | contentAsString(result) mustEqual "value" 34 | } 35 | } 36 | } 37 | // #scalatest-exampleessentialactionspec 38 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/ExampleMockitoSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest 5 | 6 | // #scalatest-mockitosugar 7 | import org.scalatestplus.mockito.MockitoSugar 8 | import org.scalatestplus.play.* 9 | 10 | import org.mockito.Mockito.* 11 | 12 | class ExampleMockitoSpec extends PlaySpec with MockitoSugar { 13 | 14 | "MyService#isDailyData" should { 15 | "return true if the data is from today" in { 16 | val mockDataService = mock[DataService] 17 | when(mockDataService.findData).thenReturn(Data(new java.util.Date())) 18 | 19 | val myService = new MyService() { 20 | override def dataService = mockDataService 21 | } 22 | 23 | val actual = myService.isDailyData 24 | actual mustBe true 25 | } 26 | } 27 | } 28 | // #scalatest-mockitosugar 29 | 30 | // #scalatest-mockito-dataservice 31 | case class Data(retrievalDate: java.util.Date) 32 | 33 | trait DataService { 34 | def findData: Data 35 | } 36 | // #scalatest-mockito-dataservice 37 | 38 | class MyService { 39 | import java.util.* 40 | 41 | def dataService: DataService = null // implementation reference... 42 | 43 | def isDailyData: Boolean = { 44 | val retrievalDate = Calendar.getInstance 45 | retrievalDate.setTime(dataService.findData.retrievalDate) 46 | 47 | val today = Calendar.getInstance() 48 | 49 | (retrievalDate.get(Calendar.YEAR) == today.get(Calendar.YEAR) 50 | && retrievalDate.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/StackSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest 5 | 6 | // #scalatest-stackspec 7 | import org.scalatestplus.play.* 8 | 9 | import scala.collection.mutable 10 | 11 | class StackSpec extends PlaySpec { 12 | 13 | "A Stack" must { 14 | "pop values in last-in-first-out order" in { 15 | val stack = new mutable.Stack[Int] 16 | stack.push(1) 17 | stack.push(2) 18 | stack.pop() mustBe 2 19 | stack.pop() mustBe 1 20 | } 21 | "throw NoSuchElementException if an empty stack is popped" in { 22 | val emptyStack = new mutable.Stack[Int] 23 | a[NoSuchElementException] must be thrownBy { 24 | emptyStack.pop() 25 | } 26 | } 27 | } 28 | } 29 | // #scalatest-stackspec 30 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/UserServiceSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest 5 | 6 | import org.scalatestplus.mockito.MockitoSugar 7 | import org.scalatestplus.play.* 8 | 9 | import org.mockito.Mockito.* 10 | import org.mockito.ArgumentMatchers.* 11 | 12 | import scalaguide.tests.models.* 13 | import scalaguide.tests.services.* 14 | 15 | // #scalatest-userservicespec 16 | class UserServiceSpec extends PlaySpec with MockitoSugar { 17 | 18 | "UserService#isAdmin" should { 19 | "be true when the role is admin" in { 20 | val userRepository = mock[UserRepository] 21 | when(userRepository.roles(any[User])).thenReturn(Set(Role("ADMIN"))) 22 | 23 | val userService = new UserService(userRepository) 24 | 25 | val actual = userService.isAdmin(User("11", "Steve", "user@example.org")) 26 | actual mustBe true 27 | } 28 | } 29 | } 30 | // #scalatest-userservicespec 31 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/allbrowserspersuite/ExampleOverrideBrowsersSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.allbrowserspersuite 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 8 | import play.api.* 9 | import play.api.inject.guice.GuiceApplicationBuilder 10 | import play.api.mvc.DefaultActionBuilder 11 | 12 | // #scalafunctionaltest-allbrowserspersuite 13 | class ExampleOverrideBrowsersSpec extends PlaySpec with GuiceOneServerPerSuite with AllBrowsersPerSuite { 14 | 15 | override lazy val browsers: Vector[BrowserInfo] = 16 | Vector(FirefoxInfo(firefoxProfile), ChromeInfo()) 17 | 18 | // Override app if you need an Application with other than 19 | // default parameters. 20 | override def fakeApplication(): Application = { 21 | import play.api.http.MimeTypes.* 22 | import play.api.mvc.Results.* 23 | 24 | GuiceApplicationBuilder() 25 | .appRoutes(app => { case ("GET", "/testing") => 26 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 27 | Ok(""" 28 | | 29 | | 30 | | Test Page 31 | | 32 | | 33 | | 34 | | 35 | | 36 | """.stripMargin).as(HTML) 37 | } 38 | }) 39 | .build() 40 | } 41 | 42 | def sharedTests(browser: BrowserInfo) = { 43 | "The AllBrowsersPerSuite trait" must { 44 | "provide a web driver" + browser.name in { 45 | go to s"http://localhost:$port/testing" 46 | pageTitle mustBe "Test Page" 47 | click.on(find(name("b")).value) 48 | eventually { pageTitle mustBe "scalatest" } 49 | } 50 | } 51 | } 52 | } 53 | // #scalafunctionaltest-allbrowserspersuite 54 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/allbrowserspersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.allbrowserspersuite 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 8 | import play.api.* 9 | import play.api.inject.guice.GuiceApplicationBuilder 10 | import play.api.mvc.DefaultActionBuilder 11 | 12 | // #scalafunctionaltest-allbrowserspersuite 13 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with AllBrowsersPerSuite { 14 | 15 | // Override app if you need an Application with other than 16 | // default parameters. 17 | override def fakeApplication(): Application = { 18 | import play.api.http.MimeTypes.* 19 | import play.api.mvc.Results.* 20 | 21 | GuiceApplicationBuilder() 22 | .appRoutes(app => { case ("GET", "/testing") => 23 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 24 | Ok(""" 25 | | 26 | | 27 | | Test Page 28 | | 29 | | 30 | | 31 | | 32 | | 33 | """.stripMargin).as(HTML) 34 | } 35 | }) 36 | .build() 37 | } 38 | 39 | def sharedTests(browser: BrowserInfo) = { 40 | "The AllBrowsersPerSuite trait" must { 41 | "provide a web driver " + browser.name in { 42 | go to s"http://localhost:$port/testing" 43 | pageTitle mustBe "Test Page" 44 | click.on(find(name("b")).value) 45 | eventually { pageTitle mustBe "scalatest" } 46 | } 47 | } 48 | } 49 | } 50 | // #scalafunctionaltest-allbrowserspersuite 51 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/allbrowserspertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.allbrowserspertest 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 8 | import play.api.* 9 | import play.api.inject.guice.GuiceApplicationBuilder 10 | import play.api.mvc.DefaultActionBuilder 11 | 12 | // #scalafunctionaltest-allbrowserspertest 13 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with AllBrowsersPerTest { 14 | 15 | // Override app if you need an Application with other than 16 | // default parameters. 17 | override def fakeApplication(): Application = { 18 | import play.api.http.MimeTypes.* 19 | import play.api.mvc.Results.* 20 | 21 | GuiceApplicationBuilder() 22 | .appRoutes(app => { case ("GET", "/testing") => 23 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 24 | Ok(""" 25 | | 26 | | 27 | | Test Page 28 | | 29 | | 30 | | 31 | | 32 | | 33 | """.stripMargin).as(HTML) 34 | } 35 | }) 36 | .build() 37 | } 38 | 39 | def sharedTests(browser: BrowserInfo) = { 40 | "The AllBrowsersPerTest trait" must { 41 | "provide a web driver" + browser.name in { 42 | go to s"http://localhost:$port/testing" 43 | pageTitle mustBe "Test Page" 44 | click.on(find(name("b")).value) 45 | eventually { pageTitle mustBe "scalatest" } 46 | } 47 | } 48 | } 49 | } 50 | // #scalafunctionaltest-allbrowserspertest 51 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/models/User.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests 5 | 6 | package models 7 | 8 | // #scalatest-models 9 | case class Role(name: String) 10 | 11 | case class User(id: String, name: String, email: String) 12 | // #scalatest-models 13 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppersuite/ExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneapppersuite 5 | 6 | import org.scalatest.ConfigMap 7 | import org.scalatest.Outcome 8 | import org.scalatestplus.play.PlaySpec 9 | import org.scalatestplus.play.components.OneAppPerSuiteWithComponents 10 | import play.api.* 11 | import play.api.mvc.Result 12 | import play.api.test.Helpers.* 13 | import play.api.test.FakeRequest 14 | import play.api.test.Helpers 15 | 16 | import scala.concurrent.Future 17 | 18 | // #scalacomponentstest-oneapppersuite 19 | class ExampleComponentsSpec extends PlaySpec with OneAppPerSuiteWithComponents { 20 | 21 | // #scalacomponentstest-inlinecomponents 22 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 23 | 24 | import play.api.mvc.Results 25 | import play.api.routing.Router 26 | import play.api.routing.sird.* 27 | 28 | lazy val router: Router = Router.from { case GET(p"/") => 29 | defaultActionBuilder { 30 | Results.Ok("success!") 31 | } 32 | } 33 | override lazy val configuration: Configuration = 34 | Configuration("foo" -> "bar", "ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 35 | } 36 | // #scalacomponentstest-inlinecomponents 37 | 38 | "The OneAppPerSuiteWithComponents trait" must { 39 | "provide an Application" in { 40 | import play.api.test.Helpers.GET 41 | import play.api.test.Helpers.route 42 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 43 | Helpers.contentAsString(result) must be("success!") 44 | } 45 | "override the configuration" in { 46 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 47 | } 48 | } 49 | } 50 | // #scalacomponentstest-oneapppersuite 51 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneapppersuite 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 8 | import play.api.Application 9 | import play.api.inject.guice.* 10 | 11 | // #scalafunctionaltest-oneapppersuite 12 | class ExampleSpec extends PlaySpec with GuiceOneAppPerSuite { 13 | 14 | // Override fakeApplication if you need a Application with other than 15 | // default parameters. 16 | override def fakeApplication(): Application = { 17 | GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build() 18 | } 19 | 20 | "The GuiceOneAppPerSuite trait" must { 21 | "provide an Application" in { 22 | app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") 23 | } 24 | } 25 | } 26 | // #scalafunctionaltest-oneapppersuite 27 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppersuite/NestedExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package oneapppersuite 5 | 6 | import org.scalatest.DoNotDiscover 7 | import org.scalatest.Suites 8 | import org.scalatest.TestSuite 9 | import org.scalatestplus.play.components.OneAppPerSuiteWithComponents 10 | import org.scalatestplus.play.ConfiguredApp 11 | import org.scalatestplus.play.PlaySpec 12 | import play.api.* 13 | import play.api.mvc.Result 14 | import play.api.test.Helpers.* 15 | import play.api.test.FakeRequest 16 | import play.api.test.Helpers 17 | 18 | import scala.concurrent.Future 19 | 20 | // #scalacomponentstest-nestedsuites 21 | class NestedExampleSpec 22 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 23 | with OneAppPerSuiteWithComponents 24 | with TestSuite { 25 | 26 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 27 | 28 | import play.api.mvc.Results 29 | import play.api.routing.Router 30 | import play.api.routing.sird.* 31 | 32 | lazy val router: Router = Router.from { case GET(p"/") => 33 | defaultActionBuilder { 34 | Results.Ok("success!") 35 | } 36 | } 37 | 38 | override lazy val configuration: Configuration = 39 | Configuration("ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 40 | } 41 | } 42 | 43 | // These are the nested suites 44 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp { 45 | "OneSpec" must { 46 | "make the Application available implicitly" in { 47 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 48 | 49 | getConfig("ehcacheplugin") mustBe Some("disabled") 50 | } 51 | } 52 | 53 | } 54 | 55 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp 56 | 57 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp 58 | 59 | @DoNotDiscover class BlueSpec extends PlaySpec with ConfiguredApp { 60 | 61 | "The NestedExampleSpec" must { 62 | "provide an Application" in { 63 | import play.api.test.Helpers.GET 64 | import play.api.test.Helpers.route 65 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 66 | Helpers.contentAsString(result) must be("success!") 67 | } 68 | } 69 | } 70 | // #scalacomponentstest-nestedsuites 71 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppertest/ExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneapppertest 5 | 6 | import org.scalatestplus.play.PlaySpec 7 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 8 | import play.api.* 9 | import play.api.mvc.Result 10 | import play.api.test.Helpers.* 11 | import play.api.test.FakeRequest 12 | import play.api.test.Helpers 13 | 14 | import scala.concurrent.Future 15 | 16 | class ExampleComponentsSpec extends PlaySpec with OneAppPerTestWithComponents { 17 | 18 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 19 | 20 | import play.api.mvc.Results 21 | import play.api.routing.Router 22 | import play.api.routing.sird.* 23 | 24 | lazy val router: Router = Router.from { case GET(p"/") => 25 | defaultActionBuilder { 26 | Results.Ok("success!") 27 | } 28 | } 29 | 30 | override lazy val configuration: Configuration = 31 | Configuration("foo" -> "bar", "ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 32 | } 33 | 34 | "The OneAppPerTestWithComponents trait" must { 35 | "provide an Application" in { 36 | import play.api.test.Helpers.GET 37 | import play.api.test.Helpers.route 38 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 39 | Helpers.contentAsString(result) must be("success!") 40 | } 41 | "override the configuration" in { 42 | app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppertest/ExamplePreDefinedComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package oneapppertest 5 | 6 | import org.scalatestplus.play.PlaySpec 7 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 8 | import play.api.* 9 | import play.api.mvc.Result 10 | import play.api.test.Helpers.* 11 | import play.api.test.FakeRequest 12 | import play.api.test.Helpers 13 | 14 | import scala.concurrent.Future 15 | 16 | class ExamplePreDefinedComponentsSpec extends PlaySpec with OneAppPerTestWithComponents { 17 | 18 | // #scalacomponentstest-predefinedcomponents 19 | override def components: BuiltInComponents = new SomeAppComponents(context) 20 | // #scalacomponentstest-predefinedcomponents 21 | 22 | "The OneAppPerTestWithComponents trait" must { 23 | "provide an Application" in { 24 | import play.api.test.Helpers.GET 25 | import play.api.test.Helpers.route 26 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 27 | Helpers.contentAsString(result) must be("success!") 28 | } 29 | "define the db" in { 30 | app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppertest/ExamplePreDefinedOverrideComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package oneapppertest 5 | 6 | import org.scalatestplus.play.PlaySpec 7 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 8 | import play.api.* 9 | import play.api.mvc.Result 10 | import play.api.test.Helpers.* 11 | import play.api.test.FakeRequest 12 | import play.api.test.Helpers 13 | 14 | import scala.concurrent.Future 15 | 16 | class ExamplePreDefinedOverrideComponentsSpec extends PlaySpec with OneAppPerTestWithComponents { 17 | 18 | // #scalacomponentstest-predefinedcomponentsoverride 19 | override def components: BuiltInComponents = new SomeAppComponents(context) { 20 | override lazy val configuration: Configuration = 21 | Configuration("ehcacheplugin" -> "enabled").withFallback(context.initialConfiguration) 22 | } 23 | 24 | // #scalacomponentstest-predefinedcomponentsoverride 25 | 26 | "The OneAppPerTestWithComponents trait" must { 27 | "provide an Application" in { 28 | import play.api.test.Helpers.GET 29 | import play.api.test.Helpers.route 30 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 31 | Helpers.contentAsString(result) must be("success!") 32 | } 33 | "override the configuration" in { 34 | app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("enabled") 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneapppertest 5 | 6 | import org.scalatest.* 7 | import org.scalatestplus.play.* 8 | import org.scalatestplus.play.guice.GuiceOneAppPerTest 9 | import play.api.Application 10 | import play.api.inject.guice.* 11 | 12 | // #scalafunctionaltest-oneapppertest 13 | class ExampleSpec extends PlaySpec with GuiceOneAppPerTest { 14 | 15 | // Override newAppForTest if you need an Application with other than 16 | // default parameters. 17 | override def newAppForTest(td: TestData): Application = { 18 | GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build() 19 | } 20 | 21 | "The OneAppPerTest trait" must { 22 | "provide a new Application for each test" in { 23 | app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled") 24 | } 25 | } 26 | } 27 | // #scalafunctionaltest-oneapppertest 28 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneapppertest/SomeAppComponents.scala: -------------------------------------------------------------------------------- 1 | package oneapppertest 2 | 3 | import play.api.ApplicationLoader.Context 4 | import play.api.mvc.Results 5 | import play.api.routing.Router 6 | import play.api.BuiltInComponentsFromContext 7 | import play.api.Configuration 8 | import play.api.NoHttpFiltersComponents 9 | 10 | /** 11 | * Simple components class which instantiates an application with a simple router 12 | * Responding 'Ok' to root level GET requests. 13 | */ 14 | protected class SomeAppComponents(context: Context) 15 | extends BuiltInComponentsFromContext(context) 16 | with NoHttpFiltersComponents { 17 | 18 | import play.api.routing.sird.* 19 | 20 | lazy val router: Router = Router.from { case GET(p"/") => 21 | defaultActionBuilder { 22 | Results.Ok("success!") 23 | } 24 | } 25 | 26 | override lazy val configuration: Configuration = 27 | Configuration("ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 28 | } 29 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/onebrowserpersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.onebrowserpersuite 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 8 | import play.api.* 9 | import play.api.inject.guice.GuiceApplicationBuilder 10 | import play.api.mvc.DefaultActionBuilder 11 | 12 | // #scalafunctionaltest-onebrowserpersuite 13 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with OneBrowserPerSuite with HtmlUnitFactory { 14 | 15 | // Override app if you need an Application with other than 16 | // default parameters. 17 | override def fakeApplication(): Application = { 18 | import play.api.http.MimeTypes.* 19 | import play.api.mvc.Results.* 20 | 21 | GuiceApplicationBuilder() 22 | .appRoutes(app => { case ("GET", "/testing") => 23 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 24 | Ok(""" 25 | | 26 | | 27 | | Test Page 28 | | 29 | | 30 | | 31 | | 32 | | 33 | """.stripMargin).as(HTML) 34 | } 35 | }) 36 | .build() 37 | } 38 | 39 | "The OneBrowserPerTest trait" must { 40 | "provide a web driver" in { 41 | go to s"http://localhost:$port/testing" 42 | pageTitle mustBe "Test Page" 43 | click.on(find(name("b")).value) 44 | eventually { pageTitle mustBe "scalatest" } 45 | } 46 | } 47 | } 48 | // #scalafunctionaltest-onebrowserpersuite 49 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/onebrowserpertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.onebrowserpertest 5 | 6 | import org.scalatest.TestData 7 | import org.scalatestplus.play.* 8 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 9 | import play.api.* 10 | import play.api.inject.guice.GuiceApplicationBuilder 11 | import play.api.mvc.DefaultActionBuilder 12 | 13 | // #scalafunctionaltest-onebrowserpertest 14 | class ExampleSpec extends PlaySpec with GuiceOneServerPerTest with OneBrowserPerTest with HtmlUnitFactory { 15 | 16 | // Override app if you need an Application with other than 17 | // default parameters. 18 | override def newAppForTest(testData: TestData): Application = { 19 | import play.api.http.MimeTypes.* 20 | import play.api.mvc.Results.* 21 | 22 | GuiceApplicationBuilder() 23 | .appRoutes(app => { case ("GET", "/testing") => 24 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 25 | Ok(""" 26 | | 27 | | 28 | | Test Page 29 | | 30 | | 31 | | 32 | | 33 | | 34 | """.stripMargin).as(HTML) 35 | } 36 | }) 37 | .build() 38 | } 39 | 40 | "The OneBrowserPerTest trait" must { 41 | "provide a web driver" in { 42 | go to s"http://localhost:$port/testing" 43 | pageTitle mustBe "Test Page" 44 | click.on(find(name("b")).value) 45 | eventually { pageTitle mustBe "scalatest" } 46 | } 47 | } 48 | } 49 | // #scalafunctionaltest-onebrowserpertest 50 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneserverpersuite/ExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneserverpersuite 5 | 6 | import org.scalatestplus.play.PlaySpec 7 | import org.scalatestplus.play.components.OneServerPerSuiteWithComponents 8 | import play.api.* 9 | import play.api.mvc.Result 10 | import play.api.test.Helpers.* 11 | import play.api.test.FakeRequest 12 | import play.api.test.Helpers 13 | 14 | import scala.concurrent.Future 15 | 16 | class ExampleComponentsSpec extends PlaySpec with OneServerPerSuiteWithComponents { 17 | 18 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 19 | 20 | import play.api.mvc.Results 21 | import play.api.routing.Router 22 | import play.api.routing.sird.* 23 | 24 | lazy val router: Router = Router.from { case GET(p"/") => 25 | defaultActionBuilder { 26 | Results.Ok("success!") 27 | } 28 | } 29 | 30 | override lazy val configuration: Configuration = 31 | Configuration("foo" -> "bar", "ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 32 | } 33 | 34 | "The OneServerPerSuiteWithComponents trait" must { 35 | "provide an Application" in { 36 | import play.api.test.Helpers.GET 37 | import play.api.test.Helpers.route 38 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 39 | Helpers.contentAsString(result) must be("success!") 40 | } 41 | "override the configuration" in { 42 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneserverpersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneserverpersuite 5 | 6 | import org.scalatestplus.play.* 7 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 8 | import play.api.Application 9 | import play.api.inject.guice.* 10 | import play.api.libs.ws.* 11 | import play.api.mvc.Results.* 12 | import play.api.mvc.* 13 | import play.api.test.Helpers.* 14 | 15 | // #scalafunctionaltest-oneserverpersuite 16 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite { 17 | 18 | // Override app if you need an Application with other than 19 | // default parameters. 20 | override def fakeApplication(): Application = { 21 | GuiceApplicationBuilder() 22 | .appRoutes(app => { case ("GET", "/") => 23 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { Ok("ok") } 24 | }) 25 | .build() 26 | } 27 | 28 | "test server logic" in { 29 | val wsClient = app.injector.instanceOf[WSClient] 30 | val myPublicAddress = s"localhost:$port" 31 | val testPaymentGatewayURL = s"http://$myPublicAddress" 32 | // The test payment gateway requires a callback to this server before it returns a result... 33 | val callbackURL = s"http://$myPublicAddress/callback" 34 | // await is from play.api.test.FutureAwaits 35 | val response = 36 | await(wsClient.url(testPaymentGatewayURL).addQueryStringParameters("callbackURL" -> callbackURL).get()) 37 | 38 | response.status mustBe OK 39 | } 40 | } 41 | // #scalafunctionaltest-oneserverpersuite 42 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneserverpersuite/NestedExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package oneserverpersuite 5 | 6 | import org.scalatest.DoNotDiscover 7 | import org.scalatest.Suites 8 | import org.scalatest.TestSuite 9 | import org.scalatestplus.play.components.* 10 | import org.scalatestplus.play.ConfiguredServer 11 | import org.scalatestplus.play.PlaySpec 12 | import play.api.* 13 | import play.api.mvc.Result 14 | import play.api.test.Helpers.* 15 | import play.api.test.FakeRequest 16 | import play.api.test.Helpers 17 | 18 | import scala.concurrent.Future 19 | 20 | class NestedExampleSpec 21 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 22 | with OneServerPerSuiteWithComponents 23 | with TestSuite { 24 | // Override fakeApplication if you need an Application with other than non-default parameters. 25 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 26 | 27 | import play.api.mvc.Results 28 | import play.api.routing.Router 29 | import play.api.routing.sird.* 30 | 31 | lazy val router: Router = Router.from { case GET(p"/") => 32 | defaultActionBuilder { 33 | Results.Ok("success!") 34 | } 35 | } 36 | 37 | override lazy val configuration: Configuration = 38 | Configuration("ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 39 | 40 | } 41 | } 42 | 43 | // These are the nested suites 44 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredServer { 45 | "OneSpec" must { 46 | "make the Application available implicitly" in { 47 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 48 | 49 | getConfig("ehcacheplugin") mustBe Some("disabled") 50 | } 51 | } 52 | 53 | } 54 | 55 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredServer 56 | 57 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredServer 58 | 59 | @DoNotDiscover class BlueSpec extends PlaySpec with ConfiguredServer { 60 | 61 | "The NestedExampleSpeccc" must { 62 | "provide an Application" in { 63 | import play.api.test.Helpers.GET 64 | import play.api.test.Helpers.route 65 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 66 | Helpers.contentAsString(result) must be("success!") 67 | } 68 | "provide an actual running server" in { 69 | import java.net.* 70 | val url = new URI("http://localhost:" + port + "/boum").toURL 71 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 72 | try con.getResponseCode mustBe 404 73 | finally con.disconnect() 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneserverpertest/ExampleComponentsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneserverpertest 5 | 6 | import org.scalatestplus.play.PlaySpec 7 | import org.scalatestplus.play.components.OneServerPerTestWithComponents 8 | import play.api.* 9 | import play.api.mvc.Result 10 | import play.api.test.Helpers.* 11 | import play.api.test.FakeRequest 12 | import play.api.test.Helpers 13 | 14 | import scala.concurrent.Future 15 | 16 | class ExampleComponentsSpec extends PlaySpec with OneServerPerTestWithComponents { 17 | 18 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 19 | 20 | import play.api.mvc.Results 21 | import play.api.routing.Router 22 | import play.api.routing.sird.* 23 | 24 | lazy val router: Router = Router.from { case GET(p"/") => 25 | defaultActionBuilder { 26 | Results.Ok("success!") 27 | } 28 | } 29 | 30 | override lazy val configuration: Configuration = 31 | Configuration("foo" -> "bar", "ehcacheplugin" -> "disabled").withFallback(context.initialConfiguration) 32 | } 33 | 34 | "The OneServerPerTestWithComponents trait" must { 35 | "provide an Application" in { 36 | import play.api.test.Helpers.GET 37 | import play.api.test.Helpers.route 38 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 39 | Helpers.contentAsString(result) must be("success!") 40 | } 41 | "override the configuration" in { 42 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/oneserverpertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.oneserverpertest 5 | 6 | import org.scalatest.* 7 | import org.scalatestplus.play.* 8 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 9 | import play.api.Application 10 | import play.api.inject.guice.* 11 | import play.api.libs.ws.* 12 | import play.api.mvc.Results.* 13 | import play.api.mvc.* 14 | import play.api.test.Helpers.* 15 | 16 | // #scalafunctionaltest-oneserverpertest 17 | class ExampleSpec extends PlaySpec with GuiceOneServerPerTest { 18 | 19 | // Override newAppForTest or mixin GuiceFakeApplicationFactory and use fakeApplication() for an Application 20 | override def newAppForTest(testData: TestData): Application = { 21 | GuiceApplicationBuilder() 22 | .appRoutes(app => { case ("GET", "/") => 23 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 24 | Ok("ok") 25 | } 26 | }) 27 | .build() 28 | } 29 | 30 | "The OneServerPerTest trait" must { 31 | "test server logic" in { 32 | val wsClient = app.injector.instanceOf[WSClient] 33 | val myPublicAddress = s"localhost:$port" 34 | val testPaymentGatewayURL = s"http://$myPublicAddress" 35 | // The test payment gateway requires a callback to this server before it returns a result... 36 | val callbackURL = s"http://$myPublicAddress/callback" 37 | // await is from play.api.test.FutureAwaits 38 | val response = 39 | await(wsClient.url(testPaymentGatewayURL).addQueryStringParameters("callbackURL" -> callbackURL).get()) 40 | 41 | response.status mustBe OK 42 | } 43 | } 44 | } 45 | // #scalafunctionaltest-oneserverpertest 46 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/playspec/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests.scalatest.playspec 5 | 6 | import org.scalatest.concurrent.IntegrationPatience 7 | import org.scalatest.concurrent.ScalaFutures 8 | import org.scalatestplus.play.* 9 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 10 | import play.api.* 11 | import play.api.inject.guice.GuiceApplicationBuilder 12 | import play.api.libs.ws.WSClient 13 | import play.api.mvc.* 14 | 15 | // #scalafunctionaltest-playspec 16 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with ScalaFutures with IntegrationPatience { 17 | 18 | // Override app if you need an Application with other than 19 | // default parameters. 20 | override def fakeApplication(): Application = { 21 | 22 | import play.api.http.MimeTypes.* 23 | import play.api.mvc.Results.* 24 | 25 | GuiceApplicationBuilder() 26 | .appRoutes(app => { case ("GET", "/testing") => 27 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 28 | Ok(""" 29 | | 30 | | 31 | | Test Page 32 | | 33 | | 34 | | 35 | | 36 | |""".stripMargin).as(HTML) 37 | } 38 | }) 39 | .build() 40 | } 41 | 42 | "WsScalaTestClient's" must { 43 | 44 | "wsUrl works correctly" in { 45 | implicit val ws: WSClient = app.injector.instanceOf(classOf[WSClient]) 46 | val futureResult = wsUrl("/testing").get() 47 | val body = futureResult.futureValue.body 48 | val expectedBody = 49 | """ 50 | | 51 | | 52 | | Test Page 53 | | 54 | | 55 | | 56 | | 57 | |""".stripMargin 58 | assert(body == expectedBody) 59 | } 60 | 61 | "wsCall works correctly" in { 62 | implicit val ws: WSClient = app.injector.instanceOf(classOf[WSClient]) 63 | val futureResult = wsCall(Call("get", "/testing")).get() 64 | val body = futureResult.futureValue.body 65 | val expectedBody = 66 | """ 67 | | 68 | | 69 | | Test Page 70 | | 71 | | 72 | | 73 | | 74 | |""".stripMargin 75 | assert(body == expectedBody) 76 | } 77 | } 78 | } 79 | // #scalafunctionaltest-playspec 80 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/services/UserRepository.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests 5 | 6 | package services 7 | 8 | import models.* 9 | 10 | // #scalatest-repository 11 | trait UserRepository { 12 | def roles(user: User): Set[Role] 13 | } 14 | // #scalatest-repository 15 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/services/UserService.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package scalaguide.tests 5 | 6 | package services 7 | 8 | import models.* 9 | 10 | // #scalatest-userservice 11 | class UserService(userRepository: UserRepository) { 12 | 13 | def isAdmin(user: User): Boolean = { 14 | userRepository.roles(user).contains(Role("ADMIN")) 15 | } 16 | } 17 | // #scalatest-userservice 18 | -------------------------------------------------------------------------------- /docs/manual/working/scalaGuide/main/tests/code/views/html/index.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. 3 | */ 4 | package views.html 5 | 6 | import play.api.mvc.* 7 | 8 | import scala.concurrent.ExecutionContext 9 | import scala.concurrent.Future 10 | import ExecutionContext.Implicits.global 11 | 12 | object index extends Results { 13 | 14 | def apply(input: String): Future[Result] = { 15 | Future(Ok("Hello Coco").as("text/html")) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /module/src/main/resources/org/scalatestplus/play/ScalaTestPlusPlayBundle.properties: -------------------------------------------------------------------------------- 1 | cantCreateHtmlUnitDriver=Was unable to create a Selenium HtmlUnitDriver on this platform: {0} 2 | cantCreateFirefoxDriver=Was unable to create a Selenium FirefoxDriver on this platform: {0} 3 | cantCreateSafariDriver=Was unable to create a Selenium SafariDriver on this platform: {0} 4 | cantCreateInternetExplorerDriver=Was unable to create a Selenium InternetExplorerDriver on this platform: {0} 5 | cantCreateEdgeDriver=Was unable to create a Selenium EdgeDriver on this platform: {0} 6 | cantCreateChromeDriver=Was unable to create a Selenium ChromeDriver on this platform: {0} 7 | and={0} and {1} 8 | webDriverUsedFromUnsharedTest=This test attempted to use a WebDriver, but the test is not one of the tests that are executed (shared) with multiple browsers. Did you forget to place this test inside the sharedTests method? 9 | webDriverUninitialized=Attempted to use a WebDriver before it was initialized. 10 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/AppProvider.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.Application 20 | 21 | /** 22 | * Trait that defines an application as `app`. 23 | */ 24 | trait AppProvider { 25 | 26 | implicit def app: Application 27 | 28 | } 29 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/BaseOneAppPerSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.Args 20 | import org.scalatest.Status 21 | import org.scalatest.TestSuite 22 | import org.scalatest.TestSuiteMixin 23 | import play.api.Application 24 | import play.api.Play 25 | 26 | /** 27 | * The base abstract trait for one app per suite. 28 | */ 29 | trait BaseOneAppPerSuite extends TestSuiteMixin { this: TestSuite & FakeApplicationFactory => 30 | 31 | /** 32 | * An implicit instance of `Application`. 33 | */ 34 | implicit lazy val app: Application = fakeApplication() 35 | 36 | /** 37 | * Invokes `Play.start`, passing in the `Application` provided by `app`, and places 38 | * that same `Application` into the `ConfigMap` under the key `org.scalatestplus.play.app` to make it available 39 | * to nested suites; calls `super.run`; and lastly ensures `Play.stop` is invoked after all tests and nested suites have completed. 40 | * 41 | * @param testName an optional name of one test to run. If `None`, all relevant tests should be run. 42 | * I.e., `None` acts like a wildcard that means run all relevant tests in this `Suite`. 43 | * @param args the `Args` for this run 44 | * @return a `Status` object that indicates when all tests and nested suites started by this method have completed, and whether or not a failure occurred. 45 | */ 46 | abstract override def run(testName: Option[String], args: Args): Status = { 47 | Play.start(app) 48 | try { 49 | val newConfigMap = args.configMap + ("org.scalatestplus.play.app" -> app) 50 | val newArgs = args.copy(configMap = newConfigMap) 51 | val status = super.run(testName, newArgs) 52 | status.whenCompleted { _ => 53 | Play.stop(app) 54 | } 55 | status 56 | } catch { // In case the suite aborts, ensure the app is stopped 57 | case ex: Throwable => 58 | Play.stop(app) 59 | throw ex 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/EdgeFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.openqa.selenium.WebDriver 20 | import org.openqa.selenium.edge.EdgeDriver 21 | import org.scalatestplus.play.BrowserFactory.UnavailableDriver 22 | 23 | /** 24 | * Factory whose `createWebDriver` method will either return a new Selenium `EdgeDriver`, or 25 | * [[org.scalatestplus.play.BrowserFactory.UnavailableDriver UnavailableDriver]], if Edge browser is not available on the host platform. 26 | * 27 | * Traits [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]] and 28 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]] extend `BrowserFactory` and therefore require 29 | * you to fill in the `createWebDriver` method, usually by mixing in one of the `BrowserFactory` subtraits such as 30 | * `EdgeFactory`. 31 | */ 32 | trait EdgeFactory extends BrowserFactory { 33 | 34 | /** 35 | * Creates a new instance of a Selenium `EdgeDriver`, or returns a [[org.scalatestplus.play.BrowserFactory.UnavailableDriver BrowserFactory.UnavailableDriver]] that includes 36 | * the exception that indicated the driver was not supported on the host platform and an appropriate 37 | * error message. 38 | * 39 | * @return an new instance of a Selenium `EdgeDriver`, or a `BrowserFactory.UnavailableDriver` if an 40 | * Edge driver is not available on the host platform. 41 | */ 42 | def createWebDriver(): WebDriver = 43 | try { 44 | new EdgeDriver() 45 | } catch { 46 | case ex: Throwable => UnavailableDriver(Some(ex), Resources("cantCreateEdgeDriver", ex.getMessage)) 47 | } 48 | } 49 | 50 | /** 51 | * Companion object to trait `EdgeFactory` that mixes in the trait. 52 | */ 53 | object EdgeFactory extends EdgeFactory 54 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/FakeApplicationFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.Application 20 | 21 | /** 22 | * Trait that provides method that creates a new instance of `Application` to the functional test suite mixins. 23 | * 24 | * The `GuiceFakeApplicationFactory` provides a `FakeApplicationFactory` that uses `GuiceApplicationBuilder` 25 | * to build an Application for test suites. 26 | */ 27 | trait FakeApplicationFactory { 28 | 29 | def fakeApplication(): Application 30 | 31 | } 32 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/HtmlUnitFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.openqa.selenium.WebDriver 20 | import org.openqa.selenium.htmlunit.HtmlUnitDriver 21 | import org.scalatestplus.play.BrowserFactory.UnavailableDriver 22 | 23 | /** 24 | * Factory whose `createWebDriver` method will either return a new Selenium `HtmlUnitDriver`, or 25 | * [[org.scalatestplus.play.BrowserFactory.UnavailableDriver UnavailableDriver]], if HtmlUnit is not available on the host platform. 26 | * 27 | * Traits [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]] and 28 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]] extend `BrowserFactory` and therefore require 29 | * you to fill in the `createWebDriver` method, usually by mixing in one of the `BrowserFactory` subtraits such as 30 | * `HtmlUnitFactory`. 31 | */ 32 | trait HtmlUnitFactory extends BrowserFactory { 33 | 34 | /** 35 | * Creates a new instance of a Selenium `HtmlUnitDriver`, with Javascript enabled, or returns a 36 | * [[org.scalatestplus.play.BrowserFactory.UnavailableDriver BrowserFactory.UnavailableDriver]] that includes the exception that indicated the driver was not 37 | * supported on the host platform and an appropriate error message. 38 | * 39 | * @return an new instance of a Selenium `HtmlUnitDriver`, or a `BrowserFactory.UnavailableDriver` if an HtmlUnit driver is not 40 | * available on the host platform. 41 | */ 42 | def createWebDriver(): WebDriver = HtmlUnitFactory.createWebDriver(true) 43 | } 44 | 45 | /** 46 | * Companion object to trait `HtmlUnitFactory` that mixes in the trait. 47 | */ 48 | object HtmlUnitFactory extends HtmlUnitFactory { 49 | 50 | /** 51 | * Creates a new instance of a Selenium `HtmlUnitDriver`, with Javascript enabled or disabled depending upon 52 | * the passed flag, or returns a `BrowserFactory.UnavailableDriver` that includes the exception that indicated the 53 | * driver was not supported on the host platform and an appropriate error message. 54 | * 55 | * @return an new instance of a Selenium `HtmlUnitDriver`, or a `BrowserFactory.UnavailableDriver` if an HtmlUnit driver is not 56 | * available on the host platform. 57 | */ 58 | def createWebDriver(enableJavascript: Boolean): WebDriver = 59 | try { 60 | val htmlUnitDriver = new HtmlUnitDriver() 61 | htmlUnitDriver.setJavascriptEnabled(enableJavascript) 62 | htmlUnitDriver 63 | } catch { 64 | case ex: Throwable => UnavailableDriver(Some(ex), Resources("cantCreateHtmlUnitDriver", ex.getMessage)) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/InternetExplorerFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.openqa.selenium.WebDriver 20 | import org.openqa.selenium.ie.InternetExplorerDriver 21 | import org.scalatestplus.play.BrowserFactory.UnavailableDriver 22 | 23 | /** 24 | * Factory whose `createWebDriver` method will either return a new Selenium `InternetExplorerDriver`, or 25 | * [[org.scalatestplus.play.BrowserFactory.UnavailableDriver UnavailableDriver]], if Internet Explorer is not available on the host platform. 26 | * 27 | * Traits [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]] and 28 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]] extend `BrowserFactory` and therefore require 29 | * you to fill in the `createWebDriver` method, usually by mixing in one of the `BrowserFactory` subtraits such as 30 | * `InternetExplorerFactory`. 31 | */ 32 | trait InternetExplorerFactory extends BrowserFactory { 33 | 34 | /** 35 | * Creates a new instance of a Selenium `InternetExplorerDriver`, or returns a [[org.scalatestplus.play.BrowserFactory.UnavailableDriver BrowserFactory.UnavailableDriver]] that includes 36 | * the exception that indicated the driver was not supported on the host platform and an appropriate 37 | * error message. 38 | * 39 | * @return an new instance of a Selenium `InternetExplorerDriver`, or a `BrowserFactory.UnavailableDriver` if an Internet 40 | * Explorer driver is not available on the host platform. 41 | */ 42 | def createWebDriver(): WebDriver = 43 | try { 44 | new InternetExplorerDriver() 45 | } catch { 46 | case ex: Throwable => UnavailableDriver(Some(ex), Resources("cantCreateInternetExplorerDriver", ex.getMessage)) 47 | } 48 | } 49 | 50 | /** 51 | * Companion object to trait `InternetExplorerFactory` that mixes in the trait. 52 | */ 53 | object InternetExplorerFactory extends InternetExplorerFactory 54 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/MixedPlaySpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.test.* 20 | import org.scalatest.* 21 | import concurrent.Eventually 22 | import concurrent.IntegrationPatience 23 | import org.openqa.selenium.WebDriver 24 | import org.openqa.selenium.firefox.FirefoxDriver 25 | import org.openqa.selenium.firefox.FirefoxProfile 26 | import play.api.http.HttpProtocol 27 | import play.api.http.Status 28 | import play.api.http.HeaderNames 29 | import org.scalatest.wordspec 30 | import org.scalatest.matchers.must.Matchers 31 | 32 | /** 33 | * Convenience "super Suite" class for "mixed fixture" Play tests. 34 | * 35 | * This class mixes in trait [[org.scalatestplus.play.MixedFixtures MixedFixtures]], and is therefore convenient 36 | * when different tests in the same test class need different kinds of fixtures. When different tests in the same class 37 | * need the same fixture, you're probably better of extending [[org.scalatestplus.play.PlaySpec PlaySpec]] instead. 38 | */ 39 | abstract class MixedPlaySpec 40 | extends wordspec.FixtureAnyWordSpec 41 | with Matchers 42 | with OptionValues 43 | with MixedFixtures 44 | with Eventually 45 | with IntegrationPatience 46 | with WsScalaTestClient 47 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/OneAppPerSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 21 | 22 | /** 23 | * Synonym for GuiceOneAppPerSuite. 24 | */ 25 | @deprecated("Use GuiceOneAppPerSuite instead", "2.0.0") 26 | trait OneAppPerSuite extends GuiceOneAppPerSuite { this: TestSuite => 27 | 28 | } 29 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/OneAppPerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerTest 21 | 22 | /** 23 | * Synonym for GuiceOneAppPerTest 24 | */ 25 | @deprecated("Use GuiceOneAppPerTest instead", "2.0.0") 26 | trait OneAppPerTest extends GuiceOneAppPerTest { this: TestSuite => 27 | 28 | } 29 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/OneServerPerSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.TestSuite 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | 22 | /** 23 | * Synonym for GuiceOneServerPerSuite. 24 | */ 25 | @deprecated("Use GuiceOneServerPerSuite instead", "2.0.0") 26 | trait OneServerPerSuite extends GuiceOneServerPerSuite { this: TestSuite => 27 | 28 | } 29 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/OneServerPerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | 22 | /** 23 | * Synonym for GuiceOneServerPerTest 24 | */ 25 | @deprecated("Use GuiceOneServerPerTest instead", "2.0.0") 26 | trait OneServerPerTest extends GuiceOneServerPerTest { this: TestSuite => 27 | 28 | } 29 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/PlaySpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatest.matchers.must.Matchers 21 | import org.scalatest.wordspec.AnyWordSpec 22 | 23 | /** 24 | * Convenience "super Suite" base class for Play tests. 25 | * 26 | * 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 27 | * [[org.scalatestplus.play.guice.GuiceOneAppPerSuite GuiceOneAppPerSuite]], [[org.scalatestplus.play.guice.GuiceOneAppPerTest GuiceOneAppPerTest]], [[org.scalatestplus.play.guice.GuiceOneServerPerSuite GuiceOneServerPerSuite]], [[org.scalatestplus.play.guice.GuiceOneServerPerTest GuiceOneServerPerTest]], [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]], [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]], [[org.scalatestplus.play.AllBrowsersPerSuite AllBrowsersPerSuite]], or [[org.scalatestplus.play.AllBrowsersPerTest AllBrowsersPerTest]] mix If you want to use trait [[org.scalatestplus.play.MixedFixtures MixedFixtures]], extend [[org.scalatestplus.play.MixedPlaySpec MixedPlaySpec]] instead. 28 | */ 29 | abstract class PlaySpec extends AnyWordSpec with Matchers with OptionValues with WsScalaTestClient 30 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/PortNumber.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | /** 20 | * Wraps a port number of a provided `TestServer` so that it can be made available as an implicit without making an `Int` implicit. 21 | * 22 | * An implicit `PortNumber` is made available by traits that provide a `play.api.test.TestServer`: [[org.scalatestplus.play.MixedFixtures MixedFixtures]], 23 | * [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]], 24 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]], [[org.scalatestplus.play.guice.GuiceOneServerPerSuite GuiceOneServerPerSuite]], 25 | * and [[org.scalatestplus.play.OneServerPerTest OneServerPerTest]]. 26 | * 27 | * The implicit `PortNumber` is taken by the methods of [[org.scalatestplus.play.WsScalaTestClient WsScalaTestClient]]. 28 | * 29 | * @param the port number of a provided `play.api.test.TestServer` 30 | */ 31 | case class PortNumber(value: Int) 32 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/Resources.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import java.text.MessageFormat 20 | import java.util.ResourceBundle 21 | 22 | /** 23 | * Resources for internationalization. 24 | * 25 | * @author Bill Venners 26 | */ 27 | private[play] object Resources { 28 | 29 | lazy val resourceBundle = ResourceBundle.getBundle("org.scalatestplus.play.ScalaTestPlusPlayBundle") 30 | 31 | def apply(resourceName: String): String = resourceBundle.getString(resourceName) 32 | 33 | private def makeString(resourceName: String, argArray: Array[Any]): String = { 34 | val raw = apply(resourceName) 35 | val msgFmt = new MessageFormat(raw) 36 | msgFmt.format(argArray) 37 | } 38 | 39 | def apply(resourceName: String, o1: Any*): String = { 40 | makeString(resourceName, o1.toArray) 41 | } 42 | 43 | def bigProblems(ex: Throwable) = { 44 | val message = if (ex.getMessage == null) "" else ex.getMessage.trim 45 | if (message.length > 0) Resources("bigProblemsWithMessage", message) else Resources("bigProblems") 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/SafariFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.openqa.selenium.WebDriver 20 | import org.openqa.selenium.safari.SafariDriver 21 | import org.scalatestplus.play.BrowserFactory.UnavailableDriver 22 | 23 | /** 24 | * Factory whose `createWebDriver` method will either return a new Selenium `SafariDriver`, or 25 | * [[org.scalatestplus.play.BrowserFactory.UnavailableDriver UnavailableDriver]], if Safari is not available on the host platform. 26 | * 27 | * Traits [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]] and 28 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]] extend `BrowserFactory` and therefore require 29 | * you to fill in the `createWebDriver` method, usually by mixing in one of the `BrowserFactory` subtraits such as 30 | * `SafariFactory`. 31 | */ 32 | trait SafariFactory extends BrowserFactory { 33 | 34 | /** 35 | * Creates a new instance of a Selenium `SafariDriver`, or returns a [[org.scalatestplus.play.BrowserFactory.UnavailableDriver BrowserFactory.UnavailableDriver]] that includes 36 | * the exception that indicated the driver was not supported on the host platform and an appropriate 37 | * error message. 38 | * 39 | * @return an new instance of a Selenium `SafariDriver`, or a `BrowserFactory.UnavailableDriver` if a Safari driver is not 40 | * available on the host platform. 41 | */ 42 | def createWebDriver(): WebDriver = 43 | try { 44 | new SafariDriver() 45 | } catch { 46 | case ex: Throwable => UnavailableDriver(Some(ex), Resources("cantCreateSafariDriver", ex.getMessage)) 47 | } 48 | } 49 | 50 | /** 51 | * Companion object to trait `SafariFactory` that mixes in the trait. 52 | */ 53 | object SafariFactory extends SafariFactory 54 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/ServerProvider.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.Application 20 | import play.api.test.RunningServer 21 | 22 | /** 23 | * Trait that defines abstract methods that providing a port number and implicit `Application` and a concrete 24 | * method that provides an implicit [[org.scalatestplus.play.PortNumber PortNumber]] that wraps the port number. 25 | * 26 | * This trait is implemented by [[org.scalatestplus.play.BaseOneServerPerSuite OneServerPerSuite]], 27 | * [[org.scalatestplus.play.OneServerPerTest OneServerPerTest]], and 28 | * [[org.scalatestplus.play.ConfiguredServer ConfiguredServer]], each of which use a different strategy to 29 | * provide `TestServer`s to tests. This trait is included in the self-type of 30 | * [[org.scalatestplus.play.OneBrowserPerSuite OneBrowserPerSuite]], and 31 | * [[org.scalatestplus.play.OneBrowserPerTest OneBrowserPerTest]], and 32 | * [[org.scalatestplus.play.AllBrowsersPerTest AllBrowsersPerTest]], allowing you to select 33 | * the `WebDriver` strategy (i.e., the extent to which `WebDriver`s are shared between tests) independently from the 34 | * `TestServer` strategy (the extent to which `TestServer`s are shared between tests). 35 | */ 36 | trait ServerProvider { 37 | 38 | /** 39 | * Implicit method that returns a `Application` instance. 40 | */ 41 | implicit def app: Application 42 | 43 | protected implicit def runningServer: RunningServer 44 | 45 | /** 46 | * The port used by the `TestServer`. 47 | */ 48 | // TODO: Document that this has been converted to a final method 49 | final def port: Int = portNumber.value 50 | 51 | /** 52 | * Implicit `PortNumber` instance that wraps `port`. The value returned from `portNumber.value` 53 | * will be same as the value of `port`. 54 | * 55 | * @return the configured port number, wrapped in a `PortNumber` 56 | */ 57 | implicit def portNumber: PortNumber = { 58 | val port = runningServer.endpoints.httpEndpoint 59 | .orElse(runningServer.endpoints.httpsEndpoint) 60 | .fold(throw new IllegalStateException("Nor HTTP or HTTPS port available for test server"))(_.port) 61 | PortNumber(port) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/WsScalaTestClient.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.libs.ws.WSClient 20 | import play.api.libs.ws.WSRequest 21 | import play.api.mvc.Call 22 | 23 | /** 24 | * Trait providing convenience methods to create WS requests in tests. 25 | */ 26 | trait WsScalaTestClient { 27 | 28 | /** 29 | * Construct a WS request for the given reverse route. 30 | * 31 | * For example: 32 | * {{{ 33 | * wsCall(controllers.routes.Application.index()).get() 34 | * }}} 35 | * 36 | * @param call the `Call` describing the request 37 | * @param portNumber the port number of the `TestServer` 38 | * @param wsClient the implicit WSClient 39 | */ 40 | def wsCall(call: Call)(implicit portNumber: PortNumber, wsClient: WSClient): WSRequest = 41 | doCall(call.url, wsClient, portNumber) 42 | 43 | /** 44 | * Construct a WS request for the given relative URL. 45 | * 46 | * @param url the URL of the request 47 | * @param portNumber the port number of the `TestServer` 48 | * @param wsClient the implicit WSClient 49 | */ 50 | def wsUrl(url: String)(implicit portNumber: PortNumber, wsClient: WSClient): WSRequest = 51 | doCall(url, wsClient, portNumber) 52 | 53 | /** 54 | * Construct a WS request for the given relative URL. 55 | * 56 | * @param url the URL of the request 57 | * @param secure if the request should be send via https 58 | * @param portNumber the port number of the `TestServer` 59 | * @param wsClient the implicit WSClient 60 | */ 61 | def wsUrl(url: String, secure: Boolean)(implicit portNumber: PortNumber, wsClient: WSClient): WSRequest = 62 | doCall(url, wsClient, portNumber, secure) 63 | 64 | private def doCall(url: String, wsClient: WSClient, portNumber: PortNumber, secure: Boolean = false) = { 65 | wsClient.url((if (secure) "https" else "http") + "://localhost:" + portNumber.value + url) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/components/WithApplicationComponents.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.components 18 | 19 | import play.api.inject.guice.GuiceApplicationBuilder 20 | import play.api.BuiltInComponents 21 | import play.api.* 22 | 23 | /** 24 | * A trait that provides a components in scope and returns them when newApplication is called. 25 | * 26 | * Mixin one of the public traits in this package to provide the desired functionality. 27 | * 28 | * This class has several methods that can be used to customize the behavior in specific ways. 29 | * 30 | * This is targeted at functional tests requiring a running application that is bootstrapped using Macwire/Compile time DI. 31 | * This is provided as an alternative to the [[GuiceApplicationBuilder]] which requires guice bootstrapping. 32 | * 33 | * @see https://www.playframework.com/documentation/2.5.x/ScalaFunctionalTestingWithScalaTest#Creating-Application-instances-for-testing 34 | */ 35 | trait WithApplicationComponents { 36 | 37 | /** 38 | * Override this function to instantiate the components - a factory of sorts. 39 | * 40 | * @return the components to be used by the application 41 | */ 42 | def components: BuiltInComponents 43 | 44 | /** 45 | * @return new application instance and set the components. This must be called for components to be properly set up. 46 | */ 47 | final def newApplication: Application = components.application 48 | 49 | /** 50 | * @return a context to use to create the application. 51 | */ 52 | def context: ApplicationLoader.Context = { 53 | val env = Environment.simple() 54 | ApplicationLoader.Context.create(env) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/guice/GuiceFakeApplicationFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.guice 18 | 19 | import org.scalatestplus.play.FakeApplicationFactory 20 | import play.api.Application 21 | import play.api.inject.guice.GuiceApplicationBuilder 22 | 23 | /** 24 | * This trait is used to return an instance of Application that is used in tests. 25 | * 26 | * If you need to use a fake application for Play, but don't want to override the 27 | * application method for each suite individually, you can create a trait that 28 | * subclasses GuiceFakeApplicationFactory 29 | */ 30 | trait GuiceFakeApplicationFactory extends FakeApplicationFactory { 31 | 32 | def fakeApplication(): Application = new GuiceApplicationBuilder().build() 33 | 34 | } 35 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/guice/GuiceOneAppPerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.guice 18 | 19 | import org.scalatest.TestSuite 20 | import org.scalatestplus.play.BaseOneAppPerTest 21 | 22 | /** 23 | * Trait that provides a new `Application` instance for each test. 24 | * 25 | * This `TestSuiteMixin` trait's overridden `withFixture` method creates a new `Application` 26 | * before each test and ensures it is cleaned up after the test has completed. You can 27 | * access the `Application` from your tests as method `app` (which is marked implicit). 28 | * 29 | * By default, this trait creates a new `Application` for each test using default parameter values, which 30 | * is returned by the `newAppForTest` method defined in this trait. If your tests need a `Application` with non-default 31 | * parameters, override `newAppForTest` to return it. 32 | * 33 | * Here's an example that demonstrates some of the services provided by this trait: 34 | * 35 | *
36 |  * package org.scalatestplus.play.examples.oneapppertest
37 |  *
38 |  * import org.scalatest._
39 |  * import org.scalatestplus.play._
40 |  * import org.scalatestplus.play.guice.GuiceOneAppPerTest
41 |  * import play.api.{Play, Application}
42 |  * import play.api.inject.guice._
43 |  *
44 |  * class ExampleSpec extends PlaySpec with GuiceOneAppPerTest {
45 |  *
46 |  *   // Override newAppForTest if you need an Application with other than non-default parameters.
47 |  *   implicit override def newAppForTest(testData: TestData): Application =
48 |  *     new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build()
49 |  *
50 |  *   "The OneAppPerTest trait" must {
51 |  *     "provide an Application" in {
52 |  *       app.configuration.getOptional[String]("ehcacheplugin") mustBe Some("disabled")
53 |  *     }
54 |  *     "make the Application available implicitly" in {
55 |  *       def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key)
56 |  *       getConfig("ehcacheplugin") mustBe Some("disabled")
57 |  *     }
58 |  *   }
59 |  * }
60 |  * 
61 | */ 62 | trait GuiceOneAppPerTest extends BaseOneAppPerTest with GuiceFakeApplicationFactory { this: TestSuite => 63 | 64 | } 65 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/guice/GuiceOneServerPerSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.guice 18 | 19 | import org.scalatest.TestSuite 20 | import org.scalatestplus.play.BaseOneServerPerSuite 21 | 22 | trait GuiceOneServerPerSuite extends BaseOneServerPerSuite with GuiceFakeApplicationFactory { this: TestSuite => 23 | 24 | } 25 | -------------------------------------------------------------------------------- /module/src/main/scala/org/scalatestplus/play/guice/GuiceOneServerPerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.guice 18 | 19 | import org.scalatest.TestSuite 20 | import org.scalatestplus.play.BaseOneServerPerTest 21 | 22 | trait GuiceOneServerPerTest extends BaseOneServerPerTest with GuiceFakeApplicationFactory { this: TestSuite => 23 | 24 | } 25 | -------------------------------------------------------------------------------- /module/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/AppSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.test.* 20 | import org.scalatest.* 21 | import org.scalatestplus.play.guice.GuiceOneAppPerTest 22 | import org.scalatest.matchers.must.Matchers 23 | import org.scalatest.wordspec.AnyWordSpec 24 | 25 | /* 26 | * Play-Test super-suite that provides a new fake App to each test. 27 | */ 28 | abstract class AppSpec extends AnyWordSpec with Matchers with OptionValues with Inside with GuiceOneAppPerTest 29 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/AppSpecSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | import play.api.inject.guice.* 22 | 23 | class AppSpecSpec extends AppSpec { 24 | 25 | override def newAppForTest(testData: TestData): Application = { 26 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 27 | } 28 | 29 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 30 | 31 | "The AppFixture" must { 32 | "provide an Application" in { 33 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 34 | } 35 | "make the Application available implicitly" in { 36 | getConfig("foo") mustBe Some("bar") 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredAppSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 21 | import play.api.Application 22 | 23 | import play.api.inject.guice.* 24 | 25 | class ConfiguredAppSpec extends UnitSpec with SequentialNestedSuiteExecution with GuiceOneAppPerSuite { 26 | 27 | class NestedSuite extends UnitSpec with ConfiguredApp { 28 | 29 | // Doesn't need synchronization because set by withFixture and checked by the test 30 | // invoked inside same withFixture with super.withFixture(test) 31 | var configMap: ConfigMap = _ 32 | 33 | override def withFixture(test: NoArgTest): Outcome = { 34 | configMap = test.configMap 35 | super.withFixture(test) 36 | } 37 | 38 | "The ConfiguredApp trait" must { 39 | "provide an Application" in { 40 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 41 | } 42 | "make the Application available implicitly" in { 43 | getConfig("foo") mustBe Some("bar") 44 | } 45 | "put the app in the configMap" in { 46 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 47 | (configuredApp.value must be).theSameInstanceAs(app) 48 | } 49 | } 50 | } 51 | 52 | override def nestedSuites: Vector[NestedSuite] = Vector(new NestedSuite) 53 | 54 | override def fakeApplication(): Application = { 55 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 56 | } 57 | 58 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 59 | } 60 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredBrowserNestedSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | import org.openqa.selenium.WebDriver 22 | 23 | @DoNotDiscover 24 | class ConfiguredBrowserNestedSuite extends UnitSpec with ConfiguredServer with ConfiguredBrowser { 25 | 26 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 27 | 28 | // Doesn't need synchronization because set by withFixture and checked by the test 29 | // invoked inside same withFixture with super.withFixture(test) 30 | var configMap: ConfigMap = _ 31 | 32 | override def withFixture(test: NoArgTest): Outcome = { 33 | configMap = test.configMap 34 | super.withFixture(test) 35 | } 36 | 37 | "The ConfiguredBrowser trait" must { 38 | "provide an Application" in { 39 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 40 | } 41 | "make the Application available implicitly" in { 42 | getConfig("foo") mustBe Some("bar") 43 | } 44 | "put the app in the configMap" in { 45 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 46 | (configuredApp.value must be).theSameInstanceAs(app) 47 | } 48 | "put the port in the configMap" in { 49 | val configuredPort = configMap.getOptional[Int]("org.scalatestplus.play.port") 50 | configuredPort.value mustEqual port 51 | } 52 | "provide an http endpoint" in { 53 | runningServer.endpoints.httpEndpoint must not be empty 54 | } 55 | 56 | "send 404 on a bad request" in { 57 | import java.net.* 58 | val url = new URI("http://localhost:" + port + "/boum").toURL 59 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 60 | try con.getResponseCode mustBe 404 61 | finally con.disconnect() 62 | } 63 | "put the webDriver in the configMap" in { 64 | val configuredApp = configMap.getOptional[WebDriver]("org.scalatestplus.play.webDriver") 65 | configuredApp mustBe defined 66 | } 67 | "provide a web driver" in { 68 | go to ("http://localhost:" + port + "/testing") 69 | pageTitle mustBe "Test Page" 70 | click.on(find(name("b")).value) 71 | eventually { pageTitle mustBe "scalatest" } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredServerNestedSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | 22 | @DoNotDiscover 23 | class ConfiguredServerNestedSuite extends UnitSpec with ConfiguredServer { 24 | 25 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 26 | 27 | // Doesn't need synchronization because set by withFixture and checked by the test 28 | // invoked inside same withFixture with super.withFixture(test) 29 | var configMap: ConfigMap = _ 30 | 31 | override def withFixture(test: NoArgTest): Outcome = { 32 | configMap = test.configMap 33 | super.withFixture(test) 34 | } 35 | 36 | "The ConfiguredServer trait" must { 37 | "provide an Application" in { 38 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 39 | } 40 | "make the Application available implicitly" in { 41 | getConfig("foo") mustBe Some("bar") 42 | } 43 | "put the app in the configMap" in { 44 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 45 | (configuredApp.value must be).theSameInstanceAs(app) 46 | } 47 | "put the port in the configMap" in { 48 | val configuredPort = configMap.getOptional[Int]("org.scalatestplus.play.port") 49 | configuredPort.value mustEqual port 50 | } 51 | "provide an http endpoint" in { 52 | runningServer.endpoints.httpEndpoint must not be empty 53 | } 54 | "send 404 on a bad request" in { 55 | import java.net.* 56 | val url = new URI("http://localhost:" + port + "/boum").toURL 57 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 58 | try con.getResponseCode mustBe 404 59 | finally con.disconnect() 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredServerSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ConfiguredServerSpec extends UnitSpec with SequentialNestedSuiteExecution with GuiceOneServerPerSuite { 25 | 26 | override def nestedSuites: Vector[ConfiguredServerNestedSuite] = Vector(new ConfiguredServerNestedSuite) 27 | 28 | override def fakeApplication(): Application = { 29 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 30 | } 31 | 32 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 33 | } 34 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredServerWithAllBrowsersPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ConfiguredServerWithAllBrowsersPerTestSpec 25 | extends Suites(new ConfiguredServerWithAllBrowsersPerTestNestedSpec) 26 | with GuiceOneServerPerSuite 27 | with TestSuite { 28 | override def fakeApplication(): Application = { 29 | GuiceApplicationBuilder() 30 | .configure("foo" -> "bar") 31 | .appRoutes(app => TestRoutes.router(app)) 32 | .build() 33 | } 34 | } 35 | 36 | @DoNotDiscover 37 | class ConfiguredServerWithAllBrowsersPerTestNestedSpec extends UnitSpec with ConfiguredServer with AllBrowsersPerTest { 38 | 39 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 40 | 41 | def sharedTests(browser: BrowserInfo) = { 42 | 43 | "The AllBrowsersPerTest trait" must { 44 | "provide a web driver " + browser.name in { 45 | go to ("http://localhost:" + port + "/testing") 46 | pageTitle mustBe "Test Page" 47 | click.on(find(name("b")).value) 48 | eventually { pageTitle mustBe "scalatest" } 49 | } 50 | } 51 | } 52 | 53 | "The AllBrowsersPerTest trait" must { 54 | "provide an Application" in { 55 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 56 | } 57 | "make the Application available implicitly" in { 58 | getConfig("foo") mustBe Some("bar") 59 | } 60 | "provide an http endpoint" in { 61 | runningServer.endpoints.httpEndpoint must not be empty 62 | } 63 | "send 404 on a bad request" in { 64 | import java.net.* 65 | val url = new URI("http://localhost:" + port + "/boum").toURL 66 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 67 | try con.getResponseCode mustBe 404 68 | finally con.disconnect() 69 | } 70 | "provide an UnneededDriver to non-shared test whose methods throw UnsupportedOperationException with an error message that gives a hint to put the test into the sharedTests method" in { 71 | (the[UnsupportedOperationException] thrownBy webDriver.get("funky") must have) 72 | .message(org.scalatestplus.play.Resources("webDriverUsedFromUnsharedTest")) 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ConfiguredServerWithOneBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ConfiguredServerWithOneBrowserPerTestSpec 25 | extends Suites(new ConfiguredServerWithOneBrowserPerTestNestedSpec) 26 | with GuiceOneServerPerSuite 27 | with TestSuite { 28 | override def fakeApplication(): Application = { 29 | GuiceApplicationBuilder() 30 | .configure("foo" -> "bar") 31 | .appRoutes(app => TestRoutes.router(app)) 32 | .build() 33 | } 34 | } 35 | 36 | @DoNotDiscover 37 | class ConfiguredServerWithOneBrowserPerTestNestedSpec 38 | extends UnitSpec 39 | with ConfiguredServer 40 | with OneBrowserPerTest 41 | with FirefoxFactory { 42 | 43 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 44 | 45 | "The OneBrowserPerTest trait" must { 46 | "provide a FakeApplication" in { 47 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 48 | } 49 | "make the FakeApplication available implicitly" in { 50 | getConfig("foo") mustBe Some("bar") 51 | } 52 | "provide an http endpoint" in { 53 | runningServer.endpoints.httpEndpoint must not be empty 54 | } 55 | "send 404 on a bad request" in { 56 | import java.net.* 57 | val url = new URI("http://localhost:" + port + "/boum").toURL 58 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 59 | try con.getResponseCode mustBe 404 60 | finally con.disconnect() 61 | } 62 | "provide a web driver" in { 63 | go to ("http://localhost:" + port + "/testing") 64 | pageTitle mustBe "Test Page" 65 | click.on(find(name("b")).value) 66 | eventually { pageTitle mustBe "scalatest" } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/MixedSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import concurrent.Eventually 21 | import concurrent.IntegrationPatience 22 | import org.scalatest.wordspec 23 | import org.scalatest.matchers.must.Matchers 24 | 25 | /** 26 | * Play-Test super-suite for test classes that need different kinds of fixtures (App, Server, 27 | * Browser) in different tests. 28 | */ 29 | abstract class MixedSpec 30 | extends wordspec.FixtureAnyWordSpec 31 | with Matchers 32 | with OptionValues 33 | with Inside 34 | with MixedFixtures 35 | with Eventually 36 | with IntegrationPatience 37 | with WsScalaTestClient 38 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneAppPerSuiteComponentSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.ConfigMap 20 | import org.scalatest.Outcome 21 | import org.scalatestplus.play.components.OneAppPerSuiteWithComponents 22 | import play.api.* 23 | import play.api.mvc.Result 24 | import play.api.test.Helpers.* 25 | import play.api.test.FakeRequest 26 | import play.api.test.Helpers 27 | 28 | import scala.concurrent.Future 29 | 30 | class OneAppPerSuiteComponentSpec extends UnitSpec with OneAppPerSuiteWithComponents { 31 | 32 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 33 | 34 | import play.api.mvc.Results 35 | import play.api.routing.Router 36 | import play.api.routing.sird.* 37 | 38 | lazy val router: Router = Router.from { case GET(p"/") => 39 | defaultActionBuilder { 40 | Results.Ok("success!") 41 | } 42 | } 43 | override lazy val configuration: Configuration = 44 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 45 | } 46 | 47 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 48 | 49 | // Doesn't need synchronization because set by withFixture and checked by the test 50 | // invoked inside same withFixture with super.withFixture(test) 51 | var configMap: ConfigMap = _ 52 | 53 | override def withFixture(test: NoArgTest): Outcome = { 54 | configMap = test.configMap 55 | super.withFixture(test) 56 | } 57 | 58 | "The OneAppPerSuiteWithComponents trait" must { 59 | "provide an Application" in { 60 | import play.api.test.Helpers.GET 61 | import play.api.test.Helpers.route 62 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 63 | Helpers.contentAsString(result) must be("success!") 64 | } 65 | "override the configuration" in { 66 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 67 | } 68 | "put the app in the configMap" in { 69 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 70 | (configuredApp.value must be).theSameInstanceAs(app) 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneAppPerSuiteSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneAppPerSuiteSpec extends UnitSpec with GuiceOneAppPerSuite { 25 | 26 | override def fakeApplication(): Application = { 27 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 28 | } 29 | 30 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 31 | 32 | // Doesn't need synchronization because set by withFixture and checked by the test 33 | // invoked inside same withFixture with super.withFixture(test) 34 | var configMap: ConfigMap = _ 35 | 36 | override def withFixture(test: NoArgTest): Outcome = { 37 | configMap = test.configMap 38 | super.withFixture(test) 39 | } 40 | 41 | "The GuiceOneAppPerSuite trait" must { 42 | "provide an Application" in { 43 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 44 | } 45 | "make the Application available implicitly" in { 46 | getConfig("foo") mustBe Some("bar") 47 | } 48 | "put the app in the configMap" in { 49 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 50 | (configuredApp.value must be).theSameInstanceAs(app) 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneAppPerTestComponentSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 20 | import play.api.* 21 | import play.api.mvc.Result 22 | import play.api.test.Helpers.* 23 | import play.api.test.FakeRequest 24 | import play.api.test.Helpers 25 | 26 | import scala.concurrent.Future 27 | 28 | class OneAppPerTestComponentSpec extends UnitSpec with OneAppPerTestWithComponents { 29 | 30 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 31 | 32 | import play.api.mvc.Results 33 | import play.api.routing.Router 34 | import play.api.routing.sird.* 35 | 36 | lazy val router: Router = Router.from { case GET(p"/") => 37 | defaultActionBuilder { 38 | Results.Ok("success!") 39 | } 40 | } 41 | 42 | override lazy val configuration: Configuration = 43 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 44 | 45 | } 46 | 47 | "The OneAppPerTestWithComponents trait" must { 48 | "provide an Application" in { 49 | import play.api.test.Helpers.GET 50 | import play.api.test.Helpers.route 51 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 52 | Helpers.contentAsString(result) must be("success!") 53 | } 54 | "override the configuration" in { 55 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneAppPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneAppPerTestSpec extends UnitSpec with GuiceOneAppPerTest { 25 | 26 | override def newAppForTest(testData: TestData): Application = { 27 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 28 | } 29 | 30 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 31 | 32 | "The OneAppPerTest trait" must { 33 | "provide an Application" in { 34 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 35 | } 36 | "make the Application available implicitly" in { 37 | getConfig("foo") mustBe Some("bar") 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneChromeBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.test.* 20 | import org.scalatest.* 21 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | class OneChromeBrowserPerTestSpec 26 | extends UnitSpec 27 | with GuiceOneServerPerTest 28 | with OneBrowserPerTest 29 | with ChromeFactory { 30 | 31 | override def newAppForTest(testData: TestData): Application = { 32 | GuiceApplicationBuilder() 33 | .configure("foo" -> "bar") 34 | .appRoutes(app => TestRoutes.router(app)) 35 | .build() 36 | } 37 | 38 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 39 | 40 | "The OneBrowserPerTest trait" must { 41 | "provide an Application" in { 42 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 43 | } 44 | "make the Application available implicitly" in { 45 | getConfig("foo") mustBe Some("bar") 46 | } 47 | "provide an http endpoint" in { 48 | runningServer.endpoints.httpEndpoint must not be empty 49 | } 50 | "send 404 on a bad request" in { 51 | import java.net.* 52 | val url = new URI("http://localhost:" + port + "/boum").toURL 53 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 54 | try con.getResponseCode mustBe 404 55 | finally con.disconnect() 56 | } 57 | "provide a web driver" in { 58 | go to ("http://localhost:" + port + "/testing") 59 | pageTitle mustBe "Test Page" 60 | click.on(find(name("b")).value) 61 | eventually { pageTitle mustBe "scalatest" } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneHtmlUnitBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneHtmlUnitBrowserPerTestSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerTest 27 | with OneBrowserPerTest 28 | with HtmlUnitFactory { 29 | 30 | override def newAppForTest(testData: TestData): Application = { 31 | GuiceApplicationBuilder() 32 | .configure("foo" -> "bar") 33 | .appRoutes(app => TestRoutes.router(app)) 34 | .build() 35 | } 36 | 37 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 38 | 39 | "The OneBrowserPerTest trait" must { 40 | "provide an Application" in { 41 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 42 | } 43 | "make the Application available implicitly" in { 44 | getConfig("foo") mustBe Some("bar") 45 | } 46 | "provide an http endpoint" in { 47 | runningServer.endpoints.httpEndpoint must not be empty 48 | } 49 | "send 404 on a bad request" in { 50 | import java.net.* 51 | val url = new URI("http://localhost:" + port + "/boum").toURL 52 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 53 | try con.getResponseCode mustBe 404 54 | finally con.disconnect() 55 | } 56 | "provide a web driver" in { 57 | go to ("http://localhost:" + port + "/testing") 58 | pageTitle mustBe "Test Page" 59 | click.on(find(name("b")).value) 60 | eventually { pageTitle mustBe "scalatest" } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneInternetExplorerBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneInternetExplorerBrowserPerTestSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerTest 27 | with OneBrowserPerTest 28 | with InternetExplorerFactory { 29 | 30 | override def newAppForTest(testData: TestData): Application = { 31 | GuiceApplicationBuilder() 32 | .configure("foo" -> "bar") 33 | .appRoutes(app => TestRoutes.router(app)) 34 | .build() 35 | } 36 | 37 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 38 | 39 | "The OneBrowserPerTest trait" must { 40 | "provide an Application" in { 41 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 42 | } 43 | "make the Application available implicitly" in { 44 | getConfig("foo") mustBe Some("bar") 45 | } 46 | "provide an http endpoint" in { 47 | runningServer.endpoints.httpEndpoint must not be empty 48 | } 49 | "send 404 on a bad request" in { 50 | import java.net.* 51 | val url = new URI("http://localhost:" + port + "/boum").toURL 52 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 53 | try con.getResponseCode mustBe 404 54 | finally con.disconnect() 55 | } 56 | "provide a web driver" in { 57 | go to ("http://localhost:" + port + "/testing") 58 | pageTitle mustBe "Test Page" 59 | click.on(find(name("b")).value) 60 | eventually { pageTitle mustBe "scalatest" } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneSafariBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneSafariBrowserPerTestSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerTest 27 | with OneBrowserPerTest 28 | with SafariFactory { 29 | 30 | override def newAppForTest(testData: TestData): Application = { 31 | GuiceApplicationBuilder() 32 | .configure("foo" -> "bar") 33 | .appRoutes(app => TestRoutes.router(app)) 34 | .build() 35 | } 36 | 37 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 38 | 39 | "The OneBrowserPerTest trait" must { 40 | "provide an Application" in { 41 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 42 | } 43 | "make the Application available implicitly" in { 44 | getConfig("foo") mustBe Some("bar") 45 | } 46 | "provide an http endpoint" in { 47 | runningServer.endpoints.httpEndpoint must not be empty 48 | } 49 | "send 404 on a bad request" in { 50 | import java.net.* 51 | val url = new URI("http://localhost:" + port + "/boum").toURL 52 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 53 | try con.getResponseCode mustBe 404 54 | finally con.disconnect() 55 | } 56 | "provide a web driver" in { 57 | go to ("http://localhost:" + port + "/testing") 58 | pageTitle mustBe "Test Page" 59 | click.on(find(name("b")).value) 60 | eventually { pageTitle mustBe "scalatest" } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerSuiteSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerSuiteSpec extends UnitSpec with GuiceOneServerPerSuite { 25 | 26 | override def fakeApplication(): Application = { 27 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 28 | } 29 | 30 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 31 | 32 | // Doesn't need synchronization because set by withFixture and checked by the test 33 | // invoked inside same withFixture with super.withFixture(test) 34 | var configMap: ConfigMap = _ 35 | 36 | override def withFixture(test: NoArgTest): Outcome = { 37 | configMap = test.configMap 38 | super.withFixture(test) 39 | } 40 | 41 | "The OneServerPerSuite trait" must { 42 | "provide an Application" in { 43 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 44 | } 45 | "make the Application available implicitly" in { 46 | getConfig("foo") mustBe Some("bar") 47 | } 48 | "provide an http endpoint" in { 49 | runningServer.endpoints.httpEndpoint must not be empty 50 | } 51 | "send 404 on a bad request" in { 52 | import java.net.* 53 | val url = new URI("http://localhost:" + port + "/boum").toURL 54 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 55 | try con.getResponseCode mustBe 404 56 | finally con.disconnect() 57 | } 58 | "put the app in the configMap" in { 59 | val configuredApp = configMap.getOptional[Application]("org.scalatestplus.play.app") 60 | (configuredApp.value must be).theSameInstanceAs(app) 61 | } 62 | "put the port in the configMap" in { 63 | val configuredPort = configMap.getOptional[Int]("org.scalatestplus.play.port") 64 | configuredPort.value mustEqual port 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerSuiteWithAllBrowsersPerSuiteSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.Application 20 | import org.openqa.selenium.WebDriver 21 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerSuiteWithAllBrowsersPerSuiteSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerSuite 27 | with AllBrowsersPerSuite { 28 | 29 | override def fakeApplication(): Application = { 30 | GuiceApplicationBuilder() 31 | .configure("foo" -> "bar") 32 | .appRoutes(app => TestRoutes.router(app)) 33 | .build() 34 | } 35 | 36 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 37 | 38 | var theWebDriver: WebDriver = _ 39 | 40 | def sharedTests(browser: BrowserInfo) = { 41 | 42 | "The AllBrowsersPerSuite trait" must { 43 | "provide a web driver " + browser.name in { 44 | go to ("http://localhost:" + port + "/testing") 45 | pageTitle mustBe "Test Page" 46 | click.on(find(name("b")).value) 47 | eventually { pageTitle mustBe "scalatest" } 48 | } 49 | "provide, for each browser type,... " + browser.name in { 50 | theWebDriver = webDriver 51 | } 52 | "...the same WebDriver instance " + browser.name in { 53 | (theWebDriver must be).theSameInstanceAs(webDriver) 54 | } 55 | } 56 | } 57 | 58 | "The AllBrowsersPerSuite trait" must { 59 | "provide an Application" in { 60 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 61 | } 62 | "make the Application available implicitly" in { 63 | getConfig("foo") mustBe Some("bar") 64 | } 65 | "provide an http endpoint" in { 66 | runningServer.endpoints.httpEndpoint must not be empty 67 | } 68 | "send 404 on a bad request" in { 69 | import java.net.* 70 | val url = new URI("http://localhost:" + port + "/boum").toURL 71 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 72 | try con.getResponseCode mustBe 404 73 | finally con.disconnect() 74 | } 75 | "provide an UnneededDriver to non-shared test whose methods throw UnsupportedOperationException with an error message that gives a hint to put the test into the sharedTests method" in { 76 | (the[UnsupportedOperationException] thrownBy webDriver.get("funky") must have) 77 | .message(Resources("webDriverUsedFromUnsharedTest")) 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerSuiteWithAllBrowsersPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 20 | import play.api.Application 21 | import play.api.inject.guice.* 22 | 23 | class OneServerPerSuiteWithAllBrowsersPerTestSpec extends UnitSpec with GuiceOneServerPerSuite with AllBrowsersPerTest { 24 | 25 | override def fakeApplication(): Application = { 26 | GuiceApplicationBuilder() 27 | .configure("foo" -> "bar") 28 | .appRoutes(app => TestRoutes.router(app)) 29 | .build() 30 | } 31 | 32 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 33 | 34 | def sharedTests(browser: BrowserInfo) = { 35 | 36 | "The AllBrowsersPerTest trait" must { 37 | "provide a web driver " + browser.name in { 38 | go to ("http://localhost:" + port + "/testing") 39 | pageTitle mustBe "Test Page" 40 | click.on(find(name("b")).value) 41 | eventually { pageTitle mustBe "scalatest" } 42 | } 43 | } 44 | } 45 | 46 | "The AllBrowsersPerTest trait" must { 47 | "provide an Application" in { 48 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 49 | } 50 | "make the Application available implicitly" in { 51 | getConfig("foo") mustBe Some("bar") 52 | } 53 | "provide an http endpoint" in { 54 | runningServer.endpoints.httpEndpoint must not be empty 55 | } 56 | "send 404 on a bad request" in { 57 | import java.net.* 58 | val url = new URI("http://localhost:" + port + "/boum").toURL 59 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 60 | try con.getResponseCode mustBe 404 61 | finally con.disconnect() 62 | } 63 | "provide an UnneededDriver to non-shared test whose methods throw UnsupportedOperationException with an error message that gives a hint to put the test into the sharedTests method" in { 64 | (the[UnsupportedOperationException] thrownBy webDriver.get("funky") must have) 65 | .message(Resources("webDriverUsedFromUnsharedTest")) 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerSuiteWithOneBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.concurrent.IntegrationPatience 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerSuiteWithOneBrowserPerTestSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerSuite 27 | with OneBrowserPerTest 28 | with IntegrationPatience 29 | with FirefoxFactory { 30 | 31 | override def fakeApplication(): Application = { 32 | GuiceApplicationBuilder() 33 | .configure("foo" -> "bar") 34 | .appRoutes(app => TestRoutes.router(app)) 35 | .build() 36 | } 37 | 38 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 39 | 40 | "The OneBrowserPerTest trait" must { 41 | "provide an Application" in { 42 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 43 | } 44 | "make the Application available implicitly" in { 45 | getConfig("foo") mustBe Some("bar") 46 | } 47 | "provide an http endpoint" in { 48 | runningServer.endpoints.httpEndpoint must not be empty 49 | } 50 | "send 404 on a bad request" in { 51 | import java.net.* 52 | val url = new URI("http://localhost:" + port + "/boum").toURL 53 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 54 | try con.getResponseCode mustBe 404 55 | finally con.disconnect() 56 | } 57 | "provide a web driver" in { 58 | go to ("http://localhost:" + port + "/testing") 59 | pageTitle mustBe "Test Page" 60 | click.on(find(name("b")).value) 61 | eventually { pageTitle mustBe "scalatest" } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestComponentShutdownSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import java.util.concurrent.atomic.AtomicInteger 20 | 21 | import org.scalatest.BeforeAndAfterAll 22 | import org.scalatestplus.play.components.OneServerPerTestWithComponents 23 | import play.api.ApplicationLoader.Context 24 | import play.api.* 25 | import play.api.mvc.Results 26 | import play.api.routing.Router 27 | 28 | import scala.concurrent.Future 29 | 30 | class OneServerPerTestComponentShutdownSpec 31 | extends UnitSpec 32 | with OneServerPerTestWithComponents 33 | with BeforeAndAfterAll { 34 | 35 | override def components: BuiltInComponents = new TestComponents(context) 36 | 37 | class TestComponents(context: Context) extends BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 38 | 39 | lazy val router: Router = Router.from { case _ => 40 | defaultActionBuilder { 41 | Results.Ok("success!") 42 | } 43 | } 44 | 45 | applicationLifecycle.addStopHook(() => { 46 | Future.successful(shutDownCounter.incrementAndGet()) 47 | }) 48 | } 49 | 50 | protected override def afterAll(): Unit = { 51 | shutDownCounter.get() mustBe 2 52 | super.afterAll() 53 | } 54 | 55 | private val shutDownCounter = new AtomicInteger() 56 | 57 | "The OneServerPerTestWithComponents trait" must { 58 | "shutdown application correctly after each test" in { 59 | app 60 | } 61 | 62 | "shutdown application correctly after this test too" in { 63 | app 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestComponentSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatestplus.play.components.OneServerPerTestWithComponents 20 | import play.api.* 21 | import play.api.mvc.Result 22 | import play.api.test.Helpers.* 23 | import play.api.test.FakeRequest 24 | import play.api.test.Helpers 25 | 26 | import scala.concurrent.Future 27 | 28 | class OneServerPerTestComponentSpec extends UnitSpec with OneServerPerTestWithComponents { 29 | 30 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 31 | 32 | import play.api.mvc.Results 33 | import play.api.routing.Router 34 | import play.api.routing.sird.* 35 | 36 | lazy val router: Router = Router.from { case GET(p"/") => 37 | defaultActionBuilder { 38 | Results.Ok("success!") 39 | } 40 | } 41 | 42 | override lazy val configuration: Configuration = 43 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 44 | } 45 | 46 | "The OneServerPerTestWithComponents trait" must { 47 | "provide an Application" in { 48 | import play.api.test.Helpers.GET 49 | import play.api.test.Helpers.route 50 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 51 | Helpers.contentAsString(result) must be("success!") 52 | } 53 | "override the configuration" in { 54 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 55 | } 56 | "provide an http endpoint" in { 57 | runningServer.endpoints.httpEndpoint must not be empty 58 | } 59 | "send 404 on a bad request" in { 60 | import java.net.* 61 | val url = new URI("http://localhost:" + port + "/boum").toURL 62 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 63 | try con.getResponseCode mustBe 404 64 | finally con.disconnect() 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestComponentSynchronizationSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import java.util.concurrent.TimeoutException 20 | 21 | import org.scalatestplus.play.components.OneServerPerTestWithComponents 22 | import play.api.mvc.Results 23 | import play.api.routing.Router 24 | import play.api.BuiltInComponents 25 | import play.api.BuiltInComponentsFromContext 26 | import play.api.NoHttpFiltersComponents 27 | 28 | import scala.concurrent.ExecutionContext.Implicits.global 29 | import scala.concurrent.duration.* 30 | import scala.concurrent.Await 31 | import scala.concurrent.Future 32 | import org.scalatest.flatspec.AnyFlatSpec 33 | import org.scalatest.matchers.must.Matchers 34 | 35 | class OneServerPerTestComponentSynchronizationSpec 36 | extends AnyFlatSpec 37 | with Matchers 38 | with OneServerPerTestWithComponents { 39 | 40 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 41 | 42 | lazy val router: Router = Router.from { case _ => 43 | defaultActionBuilder { 44 | Results.Ok("success!") 45 | } 46 | } 47 | 48 | } 49 | 50 | lazy val sum: Int = 1 + 1 51 | 52 | "A asynchronous test based on OneServerPerTestWithComponents trait" must "not result in dead lock when the test initializes lazy val" in { 53 | val action = Future { 54 | sum mustBe 2 55 | } 56 | 57 | try { 58 | Await.result(action, 1.second) 59 | } catch { 60 | case _: TimeoutException => 61 | fail() 62 | } 63 | succeed 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerTestSpec extends UnitSpec with GuiceOneServerPerTest { 25 | 26 | override def newAppForTest(testData: TestData): Application = { 27 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 28 | } 29 | 30 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 31 | 32 | "The OneServerPerTest trait" must { 33 | "provide an Application" in { 34 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 35 | } 36 | "make the Application available implicitly" in { 37 | getConfig("foo") mustBe Some("bar") 38 | } 39 | "provide an http endpoint" in { 40 | runningServer.endpoints.httpEndpoint must not be empty 41 | } 42 | "send 404 on a bad request" in { 43 | import java.net.* 44 | val url = new URI("http://localhost:" + port + "/boum").toURL 45 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 46 | try con.getResponseCode mustBe 404 47 | finally con.disconnect() 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestWithAllBrowsersPerSuiteSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | import org.openqa.selenium.WebDriver 22 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 23 | import play.api.inject.guice.* 24 | 25 | class OneServerPerTestWithAllBrowsersPerSuiteSpec extends UnitSpec with GuiceOneServerPerTest with AllBrowsersPerSuite { 26 | 27 | override def newAppForTest(testData: TestData): Application = { 28 | GuiceApplicationBuilder() 29 | .configure("foo" -> "bar") 30 | .appRoutes(app => TestRoutes.router(app)) 31 | .build() 32 | } 33 | 34 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 35 | 36 | var theWebDriver: WebDriver = _ 37 | 38 | def sharedTests(browser: BrowserInfo) = { 39 | 40 | "The AllBrowsersPerSuite trait" must { 41 | "provide a web driver " + browser.name in { 42 | go to ("http://localhost:" + port + "/testing") 43 | pageTitle mustBe "Test Page" 44 | click.on(find(name("b")).value) 45 | eventually { pageTitle mustBe "scalatest" } 46 | } 47 | "provide, for each browser type,... " + browser.name in { 48 | theWebDriver = webDriver 49 | } 50 | "...the same WebDriver instance " + browser.name in { 51 | (theWebDriver must be).theSameInstanceAs(webDriver) 52 | } 53 | } 54 | } 55 | 56 | "The AllBrowsersPerSuite trait" must { 57 | "provide an Application" in { 58 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 59 | } 60 | "make the Application available implicitly" in { 61 | getConfig("foo") mustBe Some("bar") 62 | } 63 | "provide an http endpoint" in { 64 | runningServer.endpoints.httpEndpoint must not be empty 65 | } 66 | "send 404 on a bad request" in { 67 | import java.net.* 68 | val url = new URI("http://localhost:" + port + "/boum").toURL 69 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 70 | try con.getResponseCode mustBe 404 71 | finally con.disconnect() 72 | } 73 | "provide an UnneededDriver to non-shared test whose methods throw UnsupportedOperationException with an error message that gives a hint to put the test into the sharedTests method" in { 74 | (the[UnsupportedOperationException] thrownBy webDriver.get("funky") must have) 75 | .message(org.scalatestplus.play.Resources("webDriverUsedFromUnsharedTest")) 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestWithAllBrowsersPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerTestWithAllBrowsersPerTestSpec extends UnitSpec with GuiceOneServerPerTest with AllBrowsersPerTest { 25 | 26 | override def newAppForTest(testData: TestData): Application = { 27 | GuiceApplicationBuilder() 28 | .configure("foo" -> "bar") 29 | .appRoutes(app => TestRoutes.router(app)) 30 | .build() 31 | } 32 | 33 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 34 | 35 | def sharedTests(browser: BrowserInfo) = { 36 | 37 | "The AllBrowsersPerTest trait" must { 38 | "provide a web driver " + browser.name in { 39 | go to ("http://localhost:" + port + "/testing") 40 | pageTitle mustBe "Test Page" 41 | click.on(find(name("b")).value) 42 | eventually { pageTitle mustBe "scalatest" } 43 | } 44 | } 45 | } 46 | 47 | "The AllBrowsersPerTest trait" must { 48 | "provide an Application" in { 49 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 50 | } 51 | "make the Application available implicitly" in { 52 | getConfig("foo") mustBe Some("bar") 53 | } 54 | "provide an http endpoint" in { 55 | runningServer.endpoints.httpEndpoint must not be empty 56 | } 57 | "send 404 on a bad request" in { 58 | import java.net.* 59 | val url = new URI("http://localhost:" + port + "/boum").toURL 60 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 61 | try con.getResponseCode mustBe 404 62 | finally con.disconnect() 63 | } 64 | "provide an UnneededDriver to non-shared test whose methods throw UnsupportedOperationException with an error message that gives a hint to put the test into the sharedTests method" in { 65 | (the[UnsupportedOperationException] thrownBy webDriver.get("funky") must have) 66 | .message(org.scalatestplus.play.Resources("webDriverUsedFromUnsharedTest")) 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestWithOneBrowserPerSuiteSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | import org.openqa.selenium.WebDriver 22 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 23 | import play.api.inject.guice.* 24 | 25 | class OneServerPerTestWithOneBrowserPerSuiteSpec 26 | extends UnitSpec 27 | with GuiceOneServerPerTest 28 | with OneBrowserPerSuite 29 | with FirefoxFactory { 30 | 31 | override def newAppForTest(testData: TestData): Application = { 32 | GuiceApplicationBuilder() 33 | .configure("foo" -> "bar") 34 | .appRoutes(app => TestRoutes.router(app)) 35 | .build() 36 | } 37 | 38 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 39 | 40 | // Doesn't need synchronization because set by withFixture and checked by the test 41 | // invoked inside same withFixture with super.withFixture(test) 42 | var configMap: ConfigMap = _ 43 | 44 | override def withFixture(test: NoArgTest): Outcome = { 45 | configMap = test.configMap 46 | super.withFixture(test) 47 | } 48 | 49 | "The OneBrowserPerSuite trait" must { 50 | "provide an Application" in { 51 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 52 | } 53 | "make the Application available implicitly" in { 54 | getConfig("foo") mustBe Some("bar") 55 | } 56 | "provide an http endpoint" in { 57 | runningServer.endpoints.httpEndpoint must not be empty 58 | } 59 | "send 404 on a bad request" in { 60 | import java.net.* 61 | val url = new URI("http://localhost:" + port + "/boum").toURL 62 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 63 | try con.getResponseCode mustBe 404 64 | finally con.disconnect() 65 | } 66 | "put the webDriver in the configMap" in { 67 | val configuredApp = configMap.getOptional[WebDriver]("org.scalatestplus.play.webDriver") 68 | configuredApp mustBe defined 69 | } 70 | "provide a web driver" in { 71 | go to ("http://localhost:" + port + "/testing") 72 | pageTitle mustBe "Test Page" 73 | click.on(find(name("b")).value) 74 | eventually { pageTitle mustBe "scalatest" } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/OneServerPerTestWithOneBrowserPerTestSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class OneServerPerTestWithOneBrowserPerTestSpec 25 | extends UnitSpec 26 | with GuiceOneServerPerTest 27 | with OneBrowserPerTest 28 | with FirefoxFactory { 29 | 30 | override def newAppForTest(testData: TestData): Application = { 31 | GuiceApplicationBuilder() 32 | .configure("foo" -> "bar") 33 | .appRoutes(app => TestRoutes.router(app)) 34 | .build() 35 | } 36 | 37 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 38 | 39 | "The OneBrowserPerTest trait" must { 40 | "provide an Application" in { 41 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 42 | } 43 | "make the Application available implicitly" in { 44 | getConfig("foo") mustBe Some("bar") 45 | } 46 | "provide an http endpoint" in { 47 | runningServer.endpoints.httpEndpoint must not be empty 48 | } 49 | "send 404 on a bad request" in { 50 | import java.net.* 51 | val url = new URI("http://localhost:" + port + "/boum").toURL 52 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 53 | try con.getResponseCode mustBe 404 54 | finally con.disconnect() 55 | } 56 | "provide a web driver" in { 57 | go to ("http://localhost:" + port + "/testing") 58 | pageTitle mustBe "Test Page" 59 | click.on(find(name("b")).value) 60 | eventually { pageTitle mustBe "scalatest" } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/PlaySpecSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | 21 | class PlaySpecSpec extends UnitSpec { 22 | 23 | "PlaySpec" must { 24 | "mix in OptionValues" in { 25 | assert((new PlaySpec {}).isInstanceOf[OptionValues]) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ServerSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerTest 21 | import org.scalatest.matchers.must.Matchers 22 | import org.scalatest.wordspec.AnyWordSpec 23 | 24 | /* 25 | * Play-Test super-suite that provides a new fake Server for each test. 26 | */ 27 | abstract class ServerSpec extends AnyWordSpec with Matchers with OptionValues with Inside with GuiceOneServerPerTest { 28 | this: TestSuite => 29 | } 30 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/ServerSpecSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | import play.api.Application 21 | import play.api.inject.guice.* 22 | 23 | class ServerSpecSpec extends ServerSpec { 24 | 25 | override def newAppForTest(testData: TestData): Application = { 26 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 27 | } 28 | 29 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 30 | 31 | "The ServerFixture" must { 32 | "provide an Application" in { 33 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 34 | } 35 | "make the Application available implicitly" in { 36 | getConfig("foo") mustBe Some("bar") 37 | } 38 | "provide an http endpoint" in { 39 | runningServer.endpoints.httpEndpoint must not be empty 40 | } 41 | "send 404 on a bad request" in { 42 | import java.net.* 43 | val url = new URI("http://localhost:" + port + "/boum").toURL 44 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 45 | try con.getResponseCode mustBe 404 46 | finally con.disconnect() 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/SilentReporter.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.* 20 | 21 | object SilentReporter extends Reporter { 22 | def apply(event: events.Event): Unit = {} 23 | } 24 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/TestRoute.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.* 20 | import play.api.http.MimeTypes 21 | import play.api.mvc.* 22 | import play.api.routing.Router 23 | import play.api.routing.sird.* 24 | 25 | object TestRoutes { 26 | 27 | private val Success = Results 28 | .Ok( 29 | "" + 30 | "Test Page" + 31 | "" + 32 | "" + 33 | "" + 34 | "" 35 | ) 36 | .as(MimeTypes.HTML) 37 | 38 | def router(implicit app: Application): PartialFunction[(String, String), Handler] = { case ("GET", "/testing") => 39 | app.injector.instanceOf(classOf[DefaultActionBuilder]) { 40 | Success 41 | } 42 | } 43 | 44 | def router(Action: DefaultActionBuilder): Router = { 45 | Router.from { case GET(p"/testing") => 46 | Action { 47 | Success 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/UnitSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import play.api.test.* 20 | import org.scalatest.* 21 | 22 | /* 23 | * Play-Test super-suite for basic unit tests. 24 | */ 25 | abstract class UnitSpec extends PlaySpec with OptionValues with Inside 26 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/WsScalaTestClientSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play 18 | 19 | import org.scalatest.concurrent.IntegrationPatience 20 | import org.scalatest.concurrent.ScalaFutures 21 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 22 | import play.api.Application 23 | import play.api.mvc.Call 24 | import play.api.inject.guice.* 25 | import play.api.libs.ws.WSClient 26 | 27 | class WsScalaTestClientSpec extends UnitSpec with GuiceOneServerPerSuite with ScalaFutures with IntegrationPatience { 28 | 29 | override def fakeApplication(): Application = { 30 | GuiceApplicationBuilder() 31 | .configure("foo" -> "bar") 32 | .appRoutes(app => TestRoutes.router(app)) 33 | .build() 34 | } 35 | 36 | "WsScalaTestClient's" must { 37 | 38 | "wsUrl works correctly" in { 39 | implicit val ws: WSClient = app.injector.instanceOf(classOf[WSClient]) 40 | val futureResult = wsUrl("/testing").get() 41 | val body = futureResult.futureValue.body 42 | val expectedBody = 43 | "" + 44 | "Test Page" + 45 | "" + 46 | "" + 47 | "" + 48 | "" 49 | assert(body == expectedBody) 50 | } 51 | 52 | "wsCall works correctly" in { 53 | implicit val ws: WSClient = app.injector.instanceOf(classOf[WSClient]) 54 | 55 | val futureResult = wsCall(Call("get", "/testing")).get() 56 | val body = futureResult.futureValue.body 57 | val expectedBody = 58 | "" + 59 | "Test Page" + 60 | "" + 61 | "" + 62 | "" + 63 | "" 64 | assert(body == expectedBody) 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/SomeAppComponents.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components 18 | 19 | import play.api.ApplicationLoader.Context 20 | import play.api.mvc.Results 21 | import play.api.routing.Router 22 | import play.api.BuiltInComponentsFromContext 23 | import play.api.NoHttpFiltersComponents 24 | 25 | /** 26 | * Simple components class which instantiates an application with a simple router 27 | * Responding 'Ok' to root level GET requests. 28 | */ 29 | protected class SomeAppComponents(context: Context) 30 | extends BuiltInComponentsFromContext(context) 31 | with NoHttpFiltersComponents { 32 | 33 | import play.api.routing.sird.* 34 | 35 | lazy val router: Router = Router.from { case GET(p"/") => 36 | defaultActionBuilder { 37 | Results.Ok("success!") 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneapppersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneapppersuite 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneAppPerSuiteWithComponents 21 | import play.api.* 22 | import play.api.mvc.Result 23 | import play.api.test.Helpers.* 24 | import play.api.test.FakeRequest 25 | import play.api.test.Helpers 26 | 27 | import scala.concurrent.Future 28 | 29 | class ExampleSpec extends PlaySpec with OneAppPerSuiteWithComponents { 30 | 31 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 32 | 33 | import play.api.mvc.Results 34 | import play.api.routing.Router 35 | import play.api.routing.sird.* 36 | 37 | lazy val router: Router = Router.from { case GET(p"/") => 38 | defaultActionBuilder { 39 | Results.Ok("success!") 40 | } 41 | } 42 | override lazy val configuration: Configuration = 43 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 44 | } 45 | 46 | "The OneAppPerSuiteWithComponents trait" must { 47 | "provide an Application" in { 48 | import play.api.test.Helpers.GET 49 | import play.api.test.Helpers.route 50 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 51 | Helpers.contentAsString(result) must be("success!") 52 | } 53 | "override the configuration" in { 54 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneapppersuite/NestedExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneapppersuite 18 | 19 | import org.scalatest.DoNotDiscover 20 | import org.scalatest.Suites 21 | import org.scalatest.TestSuite 22 | import org.scalatestplus.play.components.OneAppPerSuiteWithComponents 23 | import org.scalatestplus.play.ConfiguredApp 24 | import org.scalatestplus.play.PlaySpec 25 | import play.api.* 26 | import play.api.mvc.Result 27 | import play.api.test.Helpers.* 28 | import play.api.test.FakeRequest 29 | import play.api.test.Helpers 30 | 31 | import scala.concurrent.Future 32 | 33 | class NestedExampleSpec 34 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 35 | with OneAppPerSuiteWithComponents 36 | with TestSuite { 37 | // Override fakeApplication if you need an Application with other than non-default parameters. 38 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 39 | 40 | import play.api.mvc.Results 41 | import play.api.routing.Router 42 | import play.api.routing.sird.* 43 | 44 | lazy val router: Router = Router.from { case GET(p"/") => 45 | defaultActionBuilder { 46 | Results.Ok("success!") 47 | } 48 | } 49 | 50 | override lazy val configuration: Configuration = 51 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 52 | } 53 | } 54 | 55 | // These are the nested suites 56 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp { 57 | "OneSpec" must { 58 | "make the Application available implicitly" in { 59 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 60 | 61 | getConfig("foo") mustBe Some("bar") 62 | } 63 | } 64 | 65 | } 66 | 67 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp 68 | 69 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp 70 | 71 | @DoNotDiscover class BlueSpec extends PlaySpec with ConfiguredApp { 72 | 73 | "The NestedExampleSpec" must { 74 | "provide an Application" in { 75 | import play.api.test.Helpers.GET 76 | import play.api.test.Helpers.route 77 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 78 | Helpers.contentAsString(result) must be("success!") 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneapppertest/ExamplePreDefinedOverrideSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneapppertest 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 21 | import org.scalatestplus.play.examples.components.SomeAppComponents 22 | import play.api.* 23 | import play.api.mvc.Result 24 | import play.api.test.Helpers.* 25 | import play.api.test.FakeRequest 26 | import play.api.test.Helpers 27 | 28 | import scala.concurrent.Future 29 | 30 | class ExamplePreDefinedOverrideSpec extends PlaySpec with OneAppPerTestWithComponents { 31 | 32 | override def components: BuiltInComponents = new SomeAppComponents(context) { 33 | override lazy val configuration: Configuration = 34 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 35 | } 36 | 37 | "The OneAppPerTestWithComponents trait" must { 38 | "provide an Application" in { 39 | import play.api.test.Helpers.GET 40 | import play.api.test.Helpers.route 41 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 42 | Helpers.contentAsString(result) must be("success!") 43 | } 44 | "override the configuration" in { 45 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneapppertest/ExamplePreDefinedSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneapppertest 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 21 | import org.scalatestplus.play.examples.components.SomeAppComponents 22 | import play.api.* 23 | import play.api.mvc.Result 24 | import play.api.test.Helpers.* 25 | import play.api.test.FakeRequest 26 | import play.api.test.Helpers 27 | 28 | import scala.concurrent.Future 29 | 30 | class ExamplePreDefinedSpec extends PlaySpec with OneAppPerTestWithComponents { 31 | 32 | override def components: BuiltInComponents = new SomeAppComponents(context) 33 | 34 | "The OneAppPerTestWithComponents trait" must { 35 | "provide an Application" in { 36 | import play.api.test.Helpers.GET 37 | import play.api.test.Helpers.route 38 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 39 | Helpers.contentAsString(result) must be("success!") 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneapppertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneapppertest 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneAppPerTestWithComponents 21 | import play.api.* 22 | import play.api.mvc.Result 23 | import play.api.test.Helpers.* 24 | import play.api.test.FakeRequest 25 | import play.api.test.Helpers 26 | 27 | import scala.concurrent.Future 28 | 29 | class ExampleSpec extends PlaySpec with OneAppPerTestWithComponents { 30 | 31 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 32 | 33 | import play.api.mvc.Results 34 | import play.api.routing.Router 35 | import play.api.routing.sird.* 36 | 37 | lazy val router: Router = Router.from { case GET(p"/") => 38 | defaultActionBuilder { 39 | Results.Ok("success!") 40 | } 41 | } 42 | 43 | override lazy val configuration: Configuration = 44 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 45 | } 46 | 47 | "The OneAppPerTestWithComponents trait" must { 48 | "provide an Application" in { 49 | import play.api.test.Helpers.GET 50 | import play.api.test.Helpers.route 51 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 52 | Helpers.contentAsString(result) must be("success!") 53 | } 54 | "override the configuration" in { 55 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneserverpersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneserverpersuite 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneServerPerSuiteWithComponents 21 | import play.api.* 22 | import play.api.mvc.Result 23 | import play.api.test.Helpers.* 24 | import play.api.test.FakeRequest 25 | import play.api.test.Helpers 26 | 27 | import scala.concurrent.Future 28 | 29 | class ExampleSpec extends PlaySpec with OneServerPerSuiteWithComponents { 30 | 31 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 32 | 33 | import play.api.mvc.Results 34 | import play.api.routing.Router 35 | import play.api.routing.sird.* 36 | 37 | lazy val router: Router = Router.from { case GET(p"/") => 38 | defaultActionBuilder { 39 | Results.Ok("success!") 40 | } 41 | } 42 | 43 | override lazy val configuration: Configuration = 44 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 45 | } 46 | 47 | "The OneServerPerSuiteWithComponents trait" must { 48 | "provide an Application" in { 49 | import play.api.test.Helpers.GET 50 | import play.api.test.Helpers.route 51 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 52 | Helpers.contentAsString(result) must be("success!") 53 | } 54 | "override the configuration" in { 55 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/components/oneserverpertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.components.oneserverpertest 18 | 19 | import org.scalatestplus.play.PlaySpec 20 | import org.scalatestplus.play.components.OneServerPerTestWithComponents 21 | import play.api.* 22 | import play.api.mvc.Result 23 | import play.api.test.Helpers.* 24 | import play.api.test.FakeRequest 25 | import play.api.test.Helpers 26 | 27 | import scala.concurrent.Future 28 | 29 | class ExampleSpec extends PlaySpec with OneServerPerTestWithComponents { 30 | 31 | override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { 32 | 33 | import play.api.mvc.Results 34 | import play.api.routing.Router 35 | import play.api.routing.sird.* 36 | 37 | lazy val router: Router = Router.from { case GET(p"/") => 38 | defaultActionBuilder { 39 | Results.Ok("success!") 40 | } 41 | } 42 | 43 | override lazy val configuration: Configuration = 44 | Configuration("foo" -> "bar").withFallback(context.initialConfiguration) 45 | } 46 | 47 | "The OneServerPerTestWithComponents trait" must { 48 | "provide an Application" in { 49 | import play.api.test.Helpers.GET 50 | import play.api.test.Helpers.route 51 | val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/")) 52 | Helpers.contentAsString(result) must be("success!") 53 | } 54 | "override the configuration" in { 55 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/allbrowserspersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.allbrowserspersuite 18 | 19 | import org.scalatestplus.play.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with AllBrowsersPerSuite { 25 | 26 | // Override app if you need an Application with other than 27 | // default parameters. 28 | override def fakeApplication(): Application = { 29 | GuiceApplicationBuilder() 30 | .configure("foo" -> "bar") 31 | .appRoutes(app => TestRoutes.router(app)) 32 | .build() 33 | } 34 | 35 | // Place tests you want run in different browsers in the `sharedTests` method: 36 | def sharedTests(browser: BrowserInfo) = { 37 | 38 | "The AllBrowsersPerSuite trait" must { 39 | "provide a web driver " + browser.name in { 40 | go to ("http://localhost:" + port + "/testing") 41 | pageTitle mustBe "Test Page" 42 | click.on(find(name("b")).value) 43 | eventually { pageTitle mustBe "scalatest" } 44 | } 45 | } 46 | } 47 | 48 | // Place tests that don't need a WebDriver outside the `sharedTests` method 49 | // in the constructor, the usual place for tests in a `PlaySpec` 50 | "The AllBrowsersPerSuite trait" must { 51 | "provide an Application" in { 52 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 53 | } 54 | "make the Application available implicitly" in { 55 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 56 | getConfig("foo") mustBe Some("bar") 57 | } 58 | "provide an http endpoint" in { 59 | runningServer.endpoints.httpEndpoint must not be empty 60 | } 61 | "provide an actual running server" in { 62 | import java.net.* 63 | val url = new URI("http://localhost:" + port + "/boum").toURL 64 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 65 | try con.getResponseCode mustBe 404 66 | finally con.disconnect() 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/allbrowserspertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.allbrowserspertest 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.* 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | class ExampleSpec extends PlaySpec with GuiceOneServerPerTest with AllBrowsersPerTest { 26 | 27 | // Override newAppForTest if you need a Application with other than non-default parameters. 28 | override def newAppForTest(testData: TestData): Application = { 29 | GuiceApplicationBuilder() 30 | .configure("foo" -> "bar") 31 | .appRoutes(app => TestRoutes.router(app)) 32 | .build() 33 | } 34 | 35 | // Place tests you want run in different browsers in the `sharedTests` method: 36 | def sharedTests(browser: BrowserInfo) = { 37 | 38 | "The AllBrowsersPerTest trait" must { 39 | "provide a web driver " + browser.name in { 40 | go to ("http://localhost:" + port + "/testing") 41 | pageTitle mustBe "Test Page" 42 | click.on(find(name("b")).value) 43 | eventually { pageTitle mustBe "scalatest" } 44 | } 45 | } 46 | } 47 | 48 | // Place tests you want run just once outside the `sharedTests` method 49 | // in the constructor, the usual place for tests in a `PlaySpec` 50 | "The AllBrowsersPerTest trait" must { 51 | "provide a FakeApplication" in { 52 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 53 | } 54 | "make the FakeApplication available implicitly" in { 55 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 56 | getConfig("foo") mustBe Some("bar") 57 | } 58 | "provide an http endpoint" in { 59 | runningServer.endpoints.httpEndpoint must not be empty 60 | } 61 | "provide an actual running server" in { 62 | import java.net.* 63 | val url = new URI("http://localhost:" + port + "/boum").toURL 64 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 65 | try con.getResponseCode mustBe 404 66 | finally con.disconnect() 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneapppersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneapppersuite 18 | 19 | import org.scalatestplus.play.* 20 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ExampleSpec extends PlaySpec with GuiceOneAppPerSuite { 25 | 26 | // Override app if you need an Application with other than non-default parameters. 27 | override def fakeApplication(): Application = { 28 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 29 | } 30 | 31 | "The GuiceOneAppPerSuite trait" must { 32 | "provide a FakeApplication" in { 33 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 34 | } 35 | "make the FakeApplication available implicitly" in { 36 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 37 | getConfig("foo") mustBe Some("bar") 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneapppersuite/NestedExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneapppersuite 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | // This is the "master" suite 26 | class NestedExampleSpec 27 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 28 | with GuiceOneAppPerSuite 29 | with TestSuite { 30 | // Override fakeApplication if you need an Application with other than non-default parameters. 31 | override def fakeApplication(): Application = { 32 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 33 | } 34 | } 35 | 36 | // These are the nested suites 37 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp 38 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp 39 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp 40 | 41 | @DoNotDiscover 42 | class BlueSpec extends PlaySpec with ConfiguredApp { 43 | 44 | "The GuiceOneAppPerSuite trait" must { 45 | "provide an Application" in { 46 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 47 | } 48 | "make the Application available implicitly" in { 49 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 50 | getConfig("foo") mustBe Some("bar") 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneapppertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneapppertest 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.GuiceOneAppPerTest 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | class ExampleSpec extends PlaySpec with GuiceOneAppPerTest { 26 | 27 | // Override newAppForTest if you need a FakeApplication with other than non-default parameters. 28 | override def newAppForTest(testData: TestData): Application = { 29 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 30 | } 31 | 32 | "The OneAppPerTest trait" must { 33 | "provide an Application" in { 34 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 35 | } 36 | "make the Application available implicitly" in { 37 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 38 | getConfig("foo") mustBe Some("bar") 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/onebrowserpersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.onebrowserpersuite 18 | 19 | import org.scalatest.tags.FirefoxBrowser 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | @FirefoxBrowser 26 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite with OneBrowserPerSuite with FirefoxFactory { 27 | 28 | // Override fakeApplication or use GuiceOneServerPerSuite if you need a Application with other than non-default parameters. 29 | override def fakeApplication(): Application = { 30 | GuiceApplicationBuilder() 31 | .configure("foo" -> "bar") 32 | .appRoutes(app => TestRoutes.router(app)) 33 | .build() 34 | } 35 | 36 | "The OneBrowserPerSuite trait" must { 37 | "provide an Application" in { 38 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 39 | } 40 | "make the Application available implicitly" in { 41 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 42 | getConfig("foo") mustBe Some("bar") 43 | } 44 | "provide an http endpoint" in { 45 | runningServer.endpoints.httpEndpoint must not be empty 46 | } 47 | "provide an actual running server" in { 48 | import java.net.* 49 | val url = new URI("http://localhost:" + port + "/boum").toURL 50 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 51 | try con.getResponseCode mustBe 404 52 | finally con.disconnect() 53 | } 54 | "provide a web driver" in { 55 | go to ("http://localhost:" + port + "/testing") 56 | pageTitle mustBe "Test Page" 57 | click.on(find(name("b")).value) 58 | eventually { pageTitle mustBe "scalatest" } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/onebrowserpersuite/NestedExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.onebrowserpersuite 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | // This is the "master" suite 26 | class NestedExampleSpec 27 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 28 | with TestSuite 29 | with GuiceOneServerPerSuite 30 | with OneBrowserPerSuite 31 | with FirefoxFactory { 32 | // Override app if you need an Application with other than non-default parameters. 33 | override def fakeApplication(): Application = { 34 | GuiceApplicationBuilder() 35 | .configure("foo" -> "bar") 36 | .appRoutes(app => TestRoutes.router(app)) 37 | .build() 38 | } 39 | } 40 | 41 | // These are the nested suites 42 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser 43 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser 44 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser 45 | 46 | @DoNotDiscover 47 | class BlueSpec extends PlaySpec with ConfiguredServer with ConfiguredBrowser { 48 | 49 | "The OneBrowserPerSuite trait" must { 50 | "provide an Application" in { 51 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 52 | } 53 | "make the Application available implicitly" in { 54 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 55 | getConfig("foo") mustBe Some("bar") 56 | } 57 | "provide an http endpoint" in { 58 | runningServer.endpoints.httpEndpoint must not be empty 59 | } 60 | "provide an actual running server" in { 61 | import java.net.* 62 | val url = new URI("http://localhost:" + port + "/boum").toURL 63 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 64 | try con.getResponseCode mustBe 404 65 | finally con.disconnect() 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/onebrowserpertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.onebrowserpertest 18 | 19 | import org.scalatest.* 20 | import org.scalatest.tags.FirefoxBrowser 21 | import org.scalatestplus.play.* 22 | import org.scalatestplus.play.guice.* 23 | import play.api.Application 24 | import play.api.inject.guice.* 25 | 26 | @FirefoxBrowser 27 | class ExampleSpec extends PlaySpec with GuiceOneServerPerTest with OneBrowserPerTest with FirefoxFactory { 28 | 29 | // Override newAppForTest if you need an Application with other than non-default parameters. 30 | override def newAppForTest(testData: TestData): Application = { 31 | GuiceApplicationBuilder() 32 | .configure("foo" -> "bar") 33 | .appRoutes(app => TestRoutes.router(app)) 34 | .build() 35 | } 36 | 37 | "The OneBrowserPerTest trait" must { 38 | "provide an Application" in { 39 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 40 | } 41 | "make the Application available implicitly" in { 42 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 43 | getConfig("foo") mustBe Some("bar") 44 | } 45 | "provide an http endpoint" in { 46 | runningServer.endpoints.httpEndpoint must not be empty 47 | } 48 | "provide an actual running server" in { 49 | import java.net.* 50 | val url = new URI("http://localhost:" + port + "/boum").toURL 51 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 52 | try con.getResponseCode mustBe 404 53 | finally con.disconnect() 54 | } 55 | "provide a web driver" in { 56 | go to ("http://localhost:" + port + "/testing") 57 | pageTitle mustBe "Test Page" 58 | click.on(find(name("b")).value) 59 | eventually { pageTitle mustBe "scalatest" } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneserverpersuite/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneserverpersuite 18 | 19 | import org.scalatestplus.play.* 20 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 21 | import play.api.Application 22 | import play.api.inject.guice.* 23 | 24 | class ExampleSpec extends PlaySpec with GuiceOneServerPerSuite { 25 | 26 | // Override fakeApplication() if you need a Application with other than non-default parameters. 27 | implicit override def fakeApplication(): Application = { 28 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 29 | } 30 | 31 | "The OneServerPerSuite trait" must { 32 | "provide an Application" in { 33 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 34 | } 35 | "make the Application available implicitly" in { 36 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 37 | getConfig("foo") mustBe Some("bar") 38 | } 39 | "provide an http endpoint" in { 40 | runningServer.endpoints.httpEndpoint must not be empty 41 | } 42 | "provide an actual running server" in { 43 | import java.net.* 44 | val url = new URI("http://localhost:" + port + "/boum").toURL 45 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 46 | try con.getResponseCode mustBe 404 47 | finally con.disconnect() 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneserverpersuite/NestedExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneserverpersuite 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.GuiceOneServerPerSuite 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | // This is the "master" suite 26 | class NestedExampleSpec 27 | extends Suites(new OneSpec, new TwoSpec, new RedSpec, new BlueSpec) 28 | with GuiceOneServerPerSuite 29 | with TestSuite { 30 | // Override app if you need an Application with other than non-default parameters. 31 | override def fakeApplication(): Application = { 32 | GuiceApplicationBuilder().configure("foo" -> "bar").build() 33 | } 34 | } 35 | 36 | // These are the nested suites 37 | @DoNotDiscover class OneSpec extends PlaySpec with ConfiguredServer 38 | @DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredServer 39 | @DoNotDiscover class RedSpec extends PlaySpec with ConfiguredServer 40 | 41 | @DoNotDiscover 42 | class BlueSpec extends PlaySpec with ConfiguredServer { 43 | 44 | "The OneServerPerSuite trait" must { 45 | "provide an Application" in { 46 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 47 | } 48 | "make the Application available implicitly" in { 49 | def getConfig(key: String)(implicit app: Application) = app.configuration.getOptional[String](key) 50 | getConfig("foo") mustBe Some("bar") 51 | } 52 | "provide an http endpoint" in { 53 | runningServer.endpoints.httpEndpoint must not be empty 54 | } 55 | "provide an actual running server" in { 56 | import java.net.* 57 | val url = new URI("http://localhost:" + port + "/boum").toURL 58 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 59 | try con.getResponseCode mustBe 404 60 | finally con.disconnect() 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /module/src/test/scala/org/scalatestplus/play/examples/guice/oneserverpertest/ExampleSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2022 Artima, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.scalatestplus.play.examples.guice.oneserverpertest 18 | 19 | import org.scalatest.* 20 | import org.scalatestplus.play.* 21 | import org.scalatestplus.play.guice.* 22 | import play.api.Application 23 | import play.api.inject.guice.* 24 | 25 | class ExampleSpec extends PlaySpec with GuiceOneServerPerTest { 26 | 27 | // Override newAppForTest if you need a test with other than non-default parameters, or use GuiceOneServerPerTest. 28 | override def newAppForTest(testData: TestData): Application = { 29 | new GuiceApplicationBuilder() 30 | .configure("foo" -> "bar") 31 | .appRoutes(app => TestRoutes.router(app)) 32 | .build() 33 | } 34 | 35 | "The OneServerPerTest trait" must { 36 | "provide a FakeApplication" in { 37 | app.configuration.getOptional[String]("foo") mustBe Some("bar") 38 | } 39 | "make the FakeApplication available implicitly" in { 40 | def getConfig(key: String)(implicit app: Application): Option[String] = app.configuration.getOptional[String](key) 41 | getConfig("foo") mustBe Some("bar") 42 | } 43 | "provide an http endpoint" in { 44 | runningServer.endpoints.httpEndpoint must not be empty 45 | } 46 | "provide an actual running server" in { 47 | import java.net.* 48 | val url = new URI("http://localhost:" + port + "/boum").toURL 49 | val con = url.openConnection().asInstanceOf[HttpURLConnection] 50 | try con.getResponseCode mustBe 404 51 | finally con.disconnect() 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /project/Omnidoc.scala: -------------------------------------------------------------------------------- 1 | import sbt.* 2 | import sbt.Keys.* 3 | import sbt.Package.ManifestAttributes 4 | 5 | /** 6 | * This AutoPlugin adds the `Omnidoc-Source-URL` key on the MANIFEST.MF of artifact-sources.jar so later Omnidoc can use 7 | * that value to link scaladocs to GitHub sources. 8 | */ 9 | object Omnidoc extends AutoPlugin { 10 | 11 | object autoImport { 12 | lazy val omnidocSnapshotBranch = settingKey[String]("Git branch for development versions") 13 | lazy val omnidocPathPrefix = settingKey[String]("Prefix before source directory paths") 14 | lazy val omnidocSourceUrl = settingKey[Option[String]]("Source URL for scaladoc linking") 15 | } 16 | 17 | val repoName = "scalatestplus-play" 18 | 19 | val omnidocGithubRepo: Option[String] = Some(s"playframework/${repoName}") 20 | 21 | val omnidocTagPrefix: Option[String] = Some("") 22 | 23 | val SourceUrlKey = "Omnidoc-Source-URL" 24 | 25 | override def requires = sbt.plugins.JvmPlugin 26 | 27 | override def trigger = noTrigger 28 | 29 | import autoImport.* 30 | 31 | override def projectSettings = Seq( 32 | omnidocSourceUrl := omnidocGithubRepo.map { repo => 33 | val development: String = (omnidocSnapshotBranch ?? "main").value 34 | val tagged: String = omnidocTagPrefix.getOrElse("v") + version.value 35 | val tree: String = if (isSnapshot.value) development else tagged 36 | val prefix: String = "/" + (omnidocPathPrefix ?? "").value 37 | val path: String = { 38 | val buildDir: File = (ThisBuild / baseDirectory).value 39 | val projDir: File = baseDirectory.value 40 | val rel: Option[String] = IO.relativize(buildDir, projDir) 41 | rel match { 42 | case None if buildDir == projDir => "" // Same dir (sbt 0.13) 43 | case Some("") => "" // Same dir (sbt 1.0) 44 | case Some(childDir) => prefix + childDir // Child dir 45 | case None => "" // Disjoint dirs (Rich: I'm not sure if this can happen) 46 | } 47 | } 48 | s"https://github.com/${repo}/tree/${tree}${path}" 49 | }, 50 | Compile / packageSrc / packageOptions ++= omnidocSourceUrl.value.toSeq.map { url => 51 | ManifestAttributes(SourceUrlKey -> url) 52 | } 53 | ) 54 | 55 | } 56 | -------------------------------------------------------------------------------- /project/Playdoc.scala: -------------------------------------------------------------------------------- 1 | import sbt.* 2 | import sbt.Keys.* 3 | import sbt.io.IO 4 | 5 | object Playdoc extends AutoPlugin { 6 | 7 | object autoImport { 8 | final val Docs = config("docs") 9 | val playdocDirectory = settingKey[File]("Base directory of play documentation") 10 | val playdocPackage = taskKey[File]("Package play documentation") 11 | } 12 | 13 | import autoImport.* 14 | 15 | override def requires = sbt.plugins.JvmPlugin 16 | 17 | override def trigger = noTrigger 18 | 19 | override def projectSettings = 20 | Defaults.packageTaskSettings(playdocPackage, playdocPackage / mappings) ++ 21 | Seq( 22 | playdocDirectory := (ThisBuild / baseDirectory).value / "docs" / "manual", 23 | playdocPackage / mappings := { 24 | val base: File = playdocDirectory.value 25 | base.allPaths.pair(IO.relativize(base.getParentFile(), _)) 26 | }, 27 | playdocPackage / artifactClassifier := Some("playdoc"), 28 | playdocPackage / artifact ~= { _.withConfigurations(Vector(Docs)) } 29 | ) ++ 30 | addArtifact(playdocPackage / artifact, playdocPackage) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.10.7 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | resolvers ++= DefaultOptions.resolvers(snapshot = true) 2 | resolvers ++= Resolver 3 | .sonatypeOssRepos("snapshots") // used by deploy nightlies, which publish here & use -Dplay.version 4 | 5 | addSbtPlugin("org.playframework" % "play-docs-sbt-plugin" % sys.props.getOrElse("play.version", "3.1.0-M1")) 6 | 7 | addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") 8 | addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.4") 9 | addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") 10 | 11 | addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.9.2") 12 | --------------------------------------------------------------------------------