0

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?

Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51

2 Answers2

3

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?

inanutshellus
  • 9,683
  • 9
  • 53
  • 71
  • Thanks @Gabriel. I will check and will get back to you, seems like that should work! – Sharif Mamun Apr 30 '14 at 02:41
  • It worked fine Gabriel. Thanks a lot for the recursive way to solve the issue. 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? – Sharif Mamun Apr 30 '14 at 17:48
  • 1
    The way your original question was worded it sounded like you needed the index itself. Since my solution didn't use the index, I asked why you needed it, just in case it didn't work out for you. – inanutshellus Apr 30 '14 at 18:05
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...

Brine
  • 3,733
  • 1
  • 21
  • 38
  • Sorry, Brian. It was not anything related to repeating content. Actually, it was unique, I mean the opposite. Thanks for the suggestions though :) – Sharif Mamun Apr 30 '14 at 17:50
  • 1
    It actually is related to repeating content. You're using a table, no? Using a module, you can get an entire row as an object. Thus, you can do away with .find, or even indexes, which can be flaky, and handle any amount of data. I know it's hard to comprehend and first but it is a better way to handle table/row data. Good luck! – Brine May 01 '14 at 00:20