基于Yasuki Okumura 的论坛留言 https://groups.google.com/d/msg/play-framework/vjAhrsD753E/1g4V4saC48AJ,这可以通过创建一个来完成TestBrowser
来自预配置驱动程序的对象。
例如:
In file WithPhantomJS.scala
:
package com.myproject.website.tests
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.phantomjs.PhantomJSDriverService
import org.specs2.execute.AsResult
import org.specs2.execute.Result
import org.specs2.mutable.Around
import org.specs2.specification.Scope
import play.api.i18n.Lang
import play.api.test.Helpers._
import play.api.test.FakeApplication
import play.api.test.TestBrowser
import play.api.test.TestServer
import scala.collection.JavaConverters._
abstract class WithPhantomJS(val additionalOptions: Map[String, String] = Map()) extends Around with Scope {
implicit def app = FakeApplication()
implicit def port = play.api.test.Helpers.testServerPort
lazy val browser: TestBrowser = {
val defaultCapabilities = DesiredCapabilities.phantomjs
val additionalCapabilities = new DesiredCapabilities(additionalOptions.asJava)
val capabilities = new DesiredCapabilities(defaultCapabilities, additionalCapabilities)
val driver = new PhantomJSDriver(capabilities)
TestBrowser(driver, Some("http://localhost:" + port))
}
override def around[T: AsResult](body: => T): Result = {
try {
running(TestServer(port, app))(AsResult.effectively(body))
} finally {
browser.quit()
}
}
}
In file IntegrationSpec.scala
:
package com.myproject.website.tests
import com.myproject.common.helpers._
import org.junit.runner._
import org.specs2.runner._
import play.api.i18n._
import play.api.test._
import play.api.test.Helpers._
import org.specs2.mutable.Specification
import org.openqa.selenium.phantomjs.PhantomJSDriverService
/**
* An integration test will fire up a whole play application in a real (or headless) browser.
*/
@RunWith(classOf[JUnitRunner])
class IntegrationSpec extends Specification {
val enUSLangCode = "en-US"
val ptBRLangCode = "pt-BR"
val enUSOptions = getPhantomJSLanguageOption(enUSLangCode)
val ptBROptions = getPhantomJSLanguageOption(ptBRLangCode)
"Application" should {
"work from within a browser with en-US language" in new WithPhantomJS(enUSOptions) {
browser.goTo("http://localhost:" + port)
implicit val lang = Lang(enUSLangCode)
val expected = Messages("home.index.featured_lead")
browser.pageSource must contain(expected)
}
"work from within a browser with pt-BR language" in new WithPhantomJS(ptBROptions) {
browser.goTo("http://localhost:" + port)
implicit val lang = Lang(ptBRLangCode)
val expected = Messages("home.index.featured_lead")
browser.pageSource must contain(expected)
}
}
private def getPhantomJSLanguageOption(langCode: String) =
Map(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language" -> langCode)
}
此外,这种依赖关系是必需的build.sbt
:
libraryDependencies += "com.github.detro.ghostdriver" % "phantomjsdriver" % "1.0.4" % "test"
在 Play Framework 2.3 中,WithBrowser
class 会接受 https://github.com/playframework/playframework/blob/31afc82125ed75e25d1d1eda9ea9ae132ce98b7e/framework/src/play-test/src/main/scala/play/api/test/Specs.scala#L49 a WebDriver
直接实例化。