如何编写Play Framework应用程序的集成测试以查看是否加载了图像?

时间:2013-12-12 20:00:02

标签: playframework playframework-2.2

我想编写集成测试,检查图像是否实际加载到Play Framework应用程序页面中。应用程序本身生成图像,如果将无效路径指定为 src 值,则可能会失败。 使用浏览器测试有一种简单的方法吗?

  @RunWith(classOf[JUnitRunner])
  class IntegrationSpec extends Specification {

  "Application" should {

      "display image in frontpage" in new WithBrowser {

        browser.goTo("http://localhost:" + port)

        // what should be coded here to test if image in <img id="mug_image" > 
        // was loaded    

      }
    }
 }

1 个答案:

答案 0 :(得分:0)

这个测试似乎对我有用:

 
"should show image in frontpage" in new WithBrowser {
  browser.goTo("http://localhost:" + port)
  val img = browser.$("#mug_image")
  val imageUrl = img.getAttribute("src")
  val response: Response = Helpers.await(WS.url(imageUrl).get())

  response.status must equalTo(OK)
  response.getAHCResponse.getContentType must equalTo("image/png")
  response.getAHCResponse.getResponseBodyAsBytes.size must beGreaterThan(0)
}