gpt4 book ai didi

scala - 在 Play 框架规范中设置 PhantomJSDriver 上的 Accept-Language

转载 作者:行者123 更新时间:2023-12-01 02:20:09 25 4
gpt4 key购买 nike

如何使用 Play Framework 2.2 规范中的特定 Accept-Language 语言 header 配置 PhantomJSDriver?

鉴于此代码:

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)
}
}
}

如何确保 goTO 生成的请求将与特定的 Accept-Language 一起发送标题,例如 pt-BR ?

更新:该问题的目标是能够在模拟浏览器(例如 PhantomJS)中运行测试,并为特定语言配置浏览器。上面的代码示例只是要求浏览器检测页面中是否有一些本地化的文本,但是可以在模拟浏览器中运行的测试种类有很大差异。例如,文本可能在运行时通过 JavaScript 设置。或者我可能想截取屏幕截图并将其与之前的引用屏幕截图进行比较,以测试布局。默认情况下,显然浏览器正在使用机器的语言环境,这会破坏持续集成测试。所以问题是如何从 Play Framework 测试中配置 PhantomJS。

最佳答案

基于 a forum message by Yasuki Okumura ,这可以通过创建 TestBrowser 来完成。来自预配置驱动程序的对象。

例如:

在文件中 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()
}
}
}

在文件中 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) will accept WebDriver直接实例。

关于scala - 在 Play 框架规范中设置 PhantomJSDriver 上的 Accept-Language,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com