spock - 为什么在Where block 中无法访问任何Geb页面方法或模块方法

标签 spock geb

我有一个 Page 类,并且有一些提取网页的方法。现在我想将这些方法调用到 Spock 的Where block 中以作为数据提供者传递。但是当我调用该方法时,它会抛出错误,因为它没有找到它。但同样可以从Where block 之外访问。为什么会这样?

示例

def "Validate all article has its id"(){
    when:"I am at Overview page"
    at OverviewPage

    then:"I should the article id of all article"
    getAllCountOf(articleInfo).size() ==  actualCount

    where:
    articleInfo                          |actualCount
    TopArticleListInfo.ARTICLE_THUMBNAIL |filter.getCount()

}

在上面的代码中,'filter.getCount()'无法从Where block 中访问,但在when或then block 中可以访问相同的方法。

我想了解场景背后的逻辑看起来,Where block 无法静态地找到这个方法,需要创建一个对象来调用它。

当我尝试erdi提供的解决方案时,但这也没有解决问题

when:"I am at Overview page"
    at OverviewPage
    then:"I should the article id of all article"
    getAllCountOf(articleInfo).size() ==  page."$actualCount"

    where:
    articleInfo                          |actualCount
    TopArticleListInfo.ARTICLE_THUMBNAIL |'getRowCount().size()'

这里 getRowCount().size() 替换为“$actualCount”。但它仍然抛出错误

错误消息

getAllCountOf(articleInfo).size() == page."$actualCount" | | | | | | | 10 | 10 groovy.lang.MissingPropertyException:无法将 getRowCount().size() 解析为 inca.sundashboard.pageobject.OverviewPage 的内容,或其导航器上下文上的属性。 getRowCount().size() 是您忘记导入的类吗? |

最佳答案

我正在使用Dynamic Method Names在我的测试中,这是一个小例子:

def "Validate all article has its id"(){
    when: "I am at Overview page"
    at OverviewPage

    then: "I should the article id of all article"
    getAllCountOf(articleInfo).size() ==  "$actualCountMethod"().getCount()

    where:
    articleInfo                          | actualCountMethod
    TopArticleListInfo.ARTICLE_THUMBNAIL | filter

}

关于spock - 为什么在Where block 中无法访问任何Geb页面方法或模块方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356484/

相关文章:

intellij-idea - 如何将 Spock 添加到 IntelliJ?

exception - Spock - 使用数据表测试异常

groovy - Spock 中的数组断言

grails - 在 Grails 1.3.7 中使用 Geb、Selenium 和 Spock 进行功能测试时出错

events - Spock和Grails-等待服务或 Controller 发生事件

selenium - 如何排除 Geb 中的测试?

testing - 我们如何使用 browserstack 和 Geb Framework 在多个浏览器上执行单个测试

events - 如何在 Grails 3.2 中测试服务中事件的触发?

unit-testing - 什么时候 Spock 的 @Shared 注释应该比静态字段更受欢迎?

testing - Geb 设置值以选择问题