【发布时间】:2014-11-18 05:19:44
【问题描述】:
我正在 grails 项目中编写黄瓜测试用例。我想访问在 message.properties 文件中配置的消息
例如我在 message.properties 文件中配置了一个属性:
view.user.page.title = View user
我有一个黄瓜步骤 ViewUserSteps.groovy
Given(~'^I looged in into the app$') { ->
//code related to login
}
When(~'^I click view users tab"$') { ->
// click user tab logic
}
Then(~'^I see I am on View User page$') { ->
at ViewUserPage
}
而 ViewUserPage.groovy 是
import geb.Page
import grails.util.Holders
class ViewUserPage extends Page{
static url = "${Holders.config.app.url}/users/view"
static at = {
waitFor(30,2) {
title == "Edit user" // this title should be fetched from message.properties file
}
}
}
在 ViewUserPage 中,我应该能够获取在 message.properties 中配置的标题。像 g.message(code:'view.user.page.title') 或其他方式,这样如果我在 message.properties 中更改,则无需更改测试用例。有什么想法吗?
【问题讨论】:
-
也许它不能回答你的问题,但你不应该在测试中依赖 messages.properties 文件。最好在你的html中添加一些测试相关信息,例如
<body class="test-edit-user">...</body>并检查body.test-edit-user是否存在。 -
感谢您的评论。你能告诉我为什么我们不应该依赖消息外部化吗?我不确定将一个类添加到特定元素并断言它是一个好主意。这里我的要求是验证页面标题。
-
每次有人更改/更新标题,您的测试都会失败。我相信检查您的应用程序逻辑是否正确(显示正确的页面)更有意义。翻译的验证应该由翻译人员完成,而不是程序员。最后,如果你真的需要检查这个标题,我相信最好检查用于翻译目的的
key,而不是翻译本身。也许您可以在测试阶段替换 g.message 的行为以始终返回code?
标签: grails groovy cucumber geb