Set Accept-Language on PhantomJSDriver in a Play Framework Specification Set Accept-Language on PhantomJSDriver in a Play Framework Specification selenium selenium

Set Accept-Language on PhantomJSDriver in a Play Framework Specification


Based on a forum message by Yasuki Okumura, this can be done by creating a TestBrowser object from a preconfigured driver.

For example:

In file WithPhantomJS.scala:

package com.myproject.website.testsimport org.openqa.selenium.remote.DesiredCapabilitiesimport org.openqa.selenium.phantomjs.PhantomJSDriverimport org.openqa.selenium.phantomjs.PhantomJSDriverServiceimport org.specs2.execute.AsResultimport org.specs2.execute.Resultimport org.specs2.mutable.Aroundimport org.specs2.specification.Scopeimport play.api.i18n.Langimport play.api.test.Helpers._import play.api.test.FakeApplicationimport play.api.test.TestBrowserimport play.api.test.TestServerimport 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.testsimport 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.Specificationimport 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)}

Also, this dependency is required in build.sbt:

libraryDependencies += "com.github.detro.ghostdriver" % "phantomjsdriver" % "1.0.4" % "test"

In Play Framework 2.3, the WithBrowser class will accept a WebDriver instance directly.


According this comment in an issue about reading response headers, it looks like WebDriver does not have an API for setting request headers or checking response headers. The comment suggests using an HTTP client (Play has the WS Library) to test request and response headers.

Given that information, you could use the WS Library to check that the server is responding with the language corresponding to the Accept-Language header, and any front-end testing you do can assume that the headers are being respected correctly.