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

StackOverflow https://stackoverflow.com/questions/23376617

  •  12-07-2023
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top