LoginSignup
4
3

More than 5 years have passed since last update.

PlayFrameworkのテスト用PhantomJSを自動インストールする

Last updated at Posted at 2015-01-16

PhantomJSをいちいちインストールするのがめんどくさいため、自動的にやるプラグインsbt-phantomjsを作りました。そいつのPlayFrameworkへの適用方法を解説します。

まずは、sbt-phantomjsプラグインをプロジェクトに追加します。

project/plugins.sbt
addSbtPlugin("jp.leafytree.sbt" % "sbt-phantomjs" % "0.1.4")

次に、追加したプラグインを有効化します。

build.sbt
-lazy val myApp = (project in file(".")).enablePlugins(PlayScala)
+lazy val myApp = (project in file(".")).enablePlugins(PlayScala, PhantomJs)

次に、SeleniumのPhantomJS Driverを追加します。

build.sbt
libraryDependencies += "com.github.detro.ghostdriver" % "phantomjsdriver" % "1.1.0" % "test"

次に、下記設定を追加します。これはPhantomJSを自動でインストールしつつPhantomJSDriver用のインストール先情報をテスト実行時のJava VMのプロパティへ設定します1

build.sbt
javaOptions in Test ++= PhantomJs.setup(baseDirectory.value)

最後に、PhantomJS Driverを使ったテストコードを書きます

test/IntegrationSpec.scala
import org.junit.runner._
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.specs2.mutable._
import org.specs2.runner._
import play.api.test._

@RunWith(classOf[JUnitRunner])
class IntegrationSpec extends Specification {
  "Application" should {
    "work from within a browser" in new WithBrowser(webDriver = WebDriverFactory(classOf[PhantomJSDriver])) {
      browser.goTo("http://localhost:" + port)
      browser.pageSource must contain("Hello, PhantomJS World")
    }
  }
}

下記のように動くはず!

$ sbt test
[info] IntegrationSpec
[info]
[info] Application should
[info] + work from within a browser
[info]
[info] Total for specification IntegrationSpec
[info] Finished in 3 seconds, 779 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 18 s, completed 2015/01/17 2:39:21

完全に動作するサンプルはGitHubに置いてあります ⇒ https://github.com/saturday06/sbt-phantomjs/tree/master/sample/playframework


1. この処理は、将来僕が「PhantomJS Driverが動作する環境のjavaOptionsを自動判定して自動的にプロパティを追加する」やり方を発見しプラグインに反映した場合不要になると思います。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3