How can the PhantomJSDriver be configured with a specific Accept-Language language header in a Play Framework 2.2 specification?
Given this code:
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.i18n._
import play.api.test._
import play.api.test.Helpers._
import org.openqa.selenium.phantomjs.PhantomJSDriver
@RunWith(classOf[JUnitRunner])
class IntegrationSpec extends Specification {
"Application" should {
"work from within a browser" in new WithBrowser(webDriver = classOf[PhantomJSDriver]) {
browser.goTo("http://localhost:" + port)
implicit val lang = Lang("pt-BR")
val expected = Messages("home.index.featured_lead")
browser.pageSource must contain(expected)
}
}
}
How can I make sure that the request generated by goTO
will be sent with a specific Accept-Language
header, e.g. pt-BR
?
Update: The objective of the question is to be able to run tests in a simulated browser (such as PhantomJS) with the browser configured for a specific language. The code sample above just asks the browser to detect if there is some localized text in the page, but the kind of tests that can be run in a simulated browser vary a lot. For instance, the text might be set at runtime via JavaScript. Or I may want to take a screenshot and compare it with a previous reference screenshot, to test layouts. By default, apparently the browser is using the machine's locale, which breaks continuous integration tests. So the question is how can PhantomJS be configured from a Play Framework test.
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.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
:In file
IntegrationSpec.scala
:Also, this dependency is required in
build.sbt
:In Play Framework 2.3, the
WithBrowser
class will accept aWebDriver
instance directly.