是否存在用Gebish方法在两个元素之间选择元素的方法?

时间:2019-04-12 05:18:38

标签: geb

我正在尝试编写一个Geb模块,该模块将所有div元素都放在两个元素之间,并且看不到这样做的好方法。

我可能可以执行某种常规列表操作或迭代方法,但以为我首先要检查一下是否缺少Gebish方式。

html的结构大致如下

<div id="theParent">
    <div class="v-gridlayout-slot">
        <div id="sectionHeader">Header 1</div>
    </div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">
        <div id="anotherSectionHeader">Header 2</div>
    </div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">
        <div id="yetAnotherSectionHeader">Header 3</div>
    </div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">...</div>
    <div class="v-gridlayout-slot">
        <div id="actionButton"></div>
</div>

我模块的内容部分看起来像

static content = {
    headerOne { $("#sectionHeader")}
    headerTwo { $("#anotherSectionHeader")}
    headerThree { $("#yetAnotherSectionHeader")}
    ...
}

我希望有一种Gebish方式来收集headerOne和headerTwo之间的所有$("div.v-gridlayout-slot"),但看不到任何合适的东西。

erdi解决方案:

    Navigator getElementsBetween(def fromElement, def toElement) {
        String nextId = toElement.attr('id')
        def betweenElements = fromElement.parent().nextAll("div.v-gridlayout-slot").takeWhile {
            !it.has(id: nextId)
        }
        $(*betweenElements)
    }

1 个答案:

答案 0 :(得分:2)

准确地说,根据您发布的html结构,您不是要查找$("div.v-gridlayout-slot")headerOne之间但headerTwoheaderOne。我会这样写:

headerTwo

def nextHeaderId = headerTwo.attr('id') def betweenHeaders = headerOne.parent.nextAll("div.v-gridlayout-slot").takeWhile { it.hasNot(id: nextHeaderId) } 最终将成为betweenHeaders(因为使用Collection<Navigator>until上的默认Groovy方法,其中Iterable为{{1 }},而不是Navigator上的方法,因此,如果您需要将其转换为导航器,则必须在其上调用Iterable<Navigator>

Navigator

或将其传播并传递给sum()

betweenHeaders.sum()