How to find the row that contains a specific string in GEB and in that particular row I need to select a link/checkbox

妖精的绣舞 提交于 2020-01-07 06:44:10

问题


Is there any way to find the row number/index that contains a specific string in GEB? I can check whether a row contains a specific string or not with:

text: contains()

But how can I find the index of that row and then click on a link in the same row using that index ID? Any suggestion or help would be really appreciated. Please don't say what did I try so far. I spent few hours already and still no way!

EDIT: Okay, so the problem was: I can select any row with known index, for instance:

$('p', 1).click() 

but when I don't know the index of the row and in that particular row I need to select a link/checkbox, I believe this is the way we want to handle the issue, isn't it? Or is there any other better way to do it?


回答1:


Must you find the link by row index? If it's on the same row, why not find the row using contains as you suggested, then access its parent, then dive back in to find the link?

e.g. $('p', contains('some text')).parent().find(the link).click();

If that won't work, could you explain why you need to access it in such a manor?




回答2:


The best way to handle repeating content (tables/search results/etc...) in Geb is with a Module. Example from the Book of Geb:

class CartRow extends Module {
    static content = {
        cell { $("td", it) }
        productName { cell(0).text() }
        quantity { cell(1).text().toInteger() }
        price { cell(2).text().toDouble() }
    }
}

And define a list of CartRows in our Page:

class CheckoutPage extends Page {
    static content = {
        cartItems { moduleList CartRow, $("table tr").tail() } // tailing to skip the header row
    }
}

Because the return value of cartItems is a list of CartRow instances, we can use any of the usual collection methods:

assert cartItems.every { it.price > 0.0 }

I also have a working example of this in my Geb Examples project on GitHub...



来源:https://stackoverflow.com/questions/23376617/how-to-find-the-row-that-contains-a-specific-string-in-geb-and-in-that-particula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!