【问题标题】:Geb / Groovy - Page content not recognized when used within other Page Object methodGeb / Groovy - 在其他页面对象方法中使用时无法识别页面内容
【发布时间】:2014-04-21 20:22:57
【问题描述】:

当我点击一个按钮时,我必须等待一些动态内容被渲染。当我将 waitFor 闭包放在测试中时,它可以正常工作。但是,我想将 waitFor 放在 Page 对象内的一个方法中,这样我就不必在每次单击后总是调用 waitFor ,但是当我这样做时它会失败,说明它找不到该属性。

这不起作用:

class LandingPage extends Page {
    static content = {
        resultsBtn(to: ResultsPage) { $("button", id: "showresults") }
    }

    void getResults() {
        resultsBtn.click()
        waitFor { ResultsPage.results.displayed }
    }
}

class ResultsPage extends Page {
    static content = { 
        results { $("div", id: "listresults") }
    }
}

class ShowResults extends GebReportingTest {
    @Test
    public void displayResults() {
        to LandingPage
        getResults()
    }
}

错误状态类似于“没有这样的属性:ResultsPage 类的结果”。

是否可以将来自其他页面对象的内容引用放在其他页面对象方法中?

编辑:我觉得这更像是 Groovy 特定的东西,而不是 Geb。我不确定是否可以访问内容闭包中的绑定。但似乎在页面对象内创建一个 getVariable() 函数也没有多大帮助。

【问题讨论】:

    标签: groovy automated-tests geb pageobjects


    【解决方案1】:

    首先,您不应在内容块中分配闭包(ResultPage 中有不必要的 =),而是将它们传递给隐式方法,您应该:

    static content = { 
        results { $("div", id: "listresults") }
    }
    

    另一个问题是,为什么要将其建模为两个页面?据我了解,单击该按钮不会导致页面重新加载,但有一个 ajax 调用来检索结果。我会简单地将resultsresultsBtn 作为一页的内容,您的问题就会消失。

    编辑:

    事实证明,您的案例涉及页面更改。假设您总是想等待这些结果出现,您可以:

    • 将您的waitFor 条件放在static at = {} 块内,用于ResultsPage - at checks are executed implicitly whenever you use to(),这意味着无论您转到该页面,它都会等待
    • waitFor 放入page change listener
    • 通过页面上的浏览器属性访问当前页面,LandingPage:waitFor { browser.page.results.displayed } 但这对我来说似乎是一个肮脏的解决方案 - 从一个页面到达另一个页面......

    【讨论】:

    • 修复了结果关闭。为了澄清,有一个页面更改,这就是我使用(到:ResultsPage)的原因,但是,当我到达 ResultsPage 时,我必须检查结果是否已完全呈现。我认为这是因为在页面技术上完成加载后加载的结果页面上有一些异步内容。
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2015-11-17
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多