【问题标题】:can't use withFrame in page class content不能在页面类内容中使用 withFrame
【发布时间】:2026-01-06 11:35:02
【问题描述】:

我正在使用 geb-spock。我正在尝试在页面本身中验证页面类的内容,以便我只调用变量或函数。使用函数。我做了这样的事情

class BasePage extends Page {
    static content = { 

       verifyheader { withFrame ("myFrame") { assert $("h1").text() == "Header1" } 

    }
  }
}



  ...

then: 
  to BasePage  
and: 
  verifyheader 

我收到测试失败且 withFrame 为空的错误。 当我将 withFrame 放入测试用例时,不会发生这种情况

  then: 
    to BasePage
  and: 
   withFrame('myFrame') {...}

这很好用,但我希望在页面类中使用它。可能吗?我该怎么办?或者换句话说,我的代码有什么问题

【问题讨论】:

  • 你能发布堆栈跟踪

标签: groovy automated-tests spock geb


【解决方案1】:

是的,您的内容定义中的 withFrame 调用返回 null,因为传递给它的块内的最后一条语句是一个始终返回 null 的断言。您不应该在内容定义中断言,而是在测试中断言:

class BasePage extends Page {
    static content = { 
       headerText { withFrame("myFrame") { $("h1").text() } }
  }
}

和:

when:
    to BasePage

then:
    headerText == "Header1"

【讨论】:

  • 感谢您的回答