Geb WaitFor Element 的 class 刷新问题

Geb WaitFor Element's class to refresh issue

我有一个 JSF/ADF 页面,其中有一个 "button" 以

开头
<a id="pt1:r1:0:proBut" class="xfe p_AFDisabled" style="text-decoration:none;">
  <span id="pt1:r1:0:pgl13" class="x26j x1a">Proceed</span>
</a>

然后按下另一个按钮,将此按钮更改为启用。

<a id="pt1:r1:0:proBut" class="xfe" href="#" onclick="return false;" style="text-decoration:none;">
  <span id="pt1:r1:0:pgl13" class="x26j x1a">Proceed</span>
</a>

您可以看到 a 元素有一个不同的 class。当元素被禁用时,它的 class 是 "xfe p_AFDisabled",当它被激活时,它变为 "xfe".

我有一个 Geb 页面,其中包含一个方法,该方法将等到按钮启用并且看起来像这样

   class CustomerSelection extends Page {
    static at = { waitFor(100) {$("div",id:"pt1:pt_pgl10").$("div").$("span").text() == "Customer Selection"} }
    static content = {
        customers { $("div",id:"pt1:r1:0:pc1:tt1::db").$("table").$("tbody")}
        proceedButton(to: Dashboard) { $("a",id: "pt1:r1:0:proBut",class: "xfe")}   
    }

    void selectCustomer(int position) {
        customers.$("tr",position).click()
        println "selected customer!"
        waitFor {proceedButton.present}
        println "proceedButton present " + proceedButton.@class
    }

    void proceed() {
        proceedButton.click()
    }
}

但是当我 运行 通过 SpockTest

这个测试
package xx;

import geb.spock.GebReportingSpec

class LoginSpec extends GebReportingSpec {

    def "login"() {
        when:
            to Login
            report "login screen"
        and:
            login(username,password)
        and:
            at CustomerSelection
        and:
            selectCustomer(0)
        and:
            proceed()
        then:
            at Dashboard
        where:
            username   | password
            "x"        | "x"
    }

}

println 的输出是

selected customer!
proceedButton present xfe p_AFDisabled

这说明class还是xfep_AFDisabled,还没有处理完。

好像是

waitFor {proceedButton.present}

工作不正常?

编辑 ---

我已将 waitFor 更改为

waitFor {proceedButton.@class == "xfe"}

proceedButton 定义为

proceedButton(to: Dashboard) { $("a",id: "pt1:r1:0:proBut")}    

因此删除 class 属性

这行得通,但是我对解决方案不满意,因为现在有 2 个地方有 DOM 个特定任务。我希望将 class 属性移回按钮定义中?

编辑 2----

我在 Geb 页面中添加了一个新元素,如下所示:

proceedButtonActive(wait: true, required: false) {proceedButton.@class == "xfe"}

然后我可以在 selectCustomer 方法中调用这个元素并且它起作用了。但是,此逻辑仍然需要 2 个元素。 1是理想的。

您需要更改选择器以过滤掉具有 p_AFDisabled class 的元素:

proceedButton(to: Dashboard, required: false) { 
    $("a", id: "pt1:r1:0:proBut").not(class: "p_AFDisabled") 
}

从技术上讲,您甚至不需要在 processButton 上调用 isPresent(),因为如果它不存在,那么该定义将 return 一个 "falsey" 值和 waitFor {} 不会 return。所以 selectCustomer() 变成:

void selectCustomer(int position) {
    customers.$("tr",position).click()
    waitFor { proceedButton }
}