我想编写集成测试,检查图像是否实际加载到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
}
}
}
答案 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)
}