Sharif Mamun
Sharif Mamun

Reputation: 3554

How to Click on checkboxes one by one in Groovy/Geb?

I was working on an automation script using Groovy/Geb and the HTML code looks like the tags below:

<table class="backgrid">
  <thead>
    <tr>
        <th class="select-all-header-cell">
            <input tabindex="-1" type="checkbox">
        </th>
        <th>
            <a>
                List Item<b class="sort-caret"></b>
            </a>
        </th>
    </tr>
  </thead>
    <tbody>
        <tr>
            <td class="select-row-cell">
                <input tabindex="-1" type="checkbox">
            </td><td class="string-cell">CAD</td>
        </tr>
        <tr>
            <td class="select-row-cell">
                <input tabindex="-1" type="checkbox">
            </td>
            <td class="string-cell">
                USD
            </td>
        </tr>
        <tr>
            <td class="select-row-cell">
                <input tabindex="-1" type="checkbox">
            </td>
            <td class="string-cell">.
                GBP
            </td>
        </tr>
        <tr>
            <td class="select-row-cell">
                <input tabindex="-1" type="checkbox">
            </td>
            <td class="string-cell">
                KPW
            </td>
        </tr>
    </tbody>
</table>

I need to click on the checkboxes inside the one by one and then move to the next stage of the automation. But I am stuck here. I know for clicking checkboxes I should work with interact {} but I can't find any sample example for this kind of stuff. Any kind of hints or suggestions would be a great help for me!

Upvotes: 1

Views: 3872

Answers (2)

erdi
erdi

Reputation: 6954

You don't need to use interact {} to achieve it - there is a much easier way using click().

To click the checkbox in header you can use:

$("table.backgrid thead input").click()

To click on the currency checkboxes use:

def checkboxes = $("table.backgrid tbody input")
checkboxes[0].click() //CAD
checkboxes[2].click() //GBP

Upvotes: 2

Hern&#225;n Erasmo
Hern&#225;n Erasmo

Reputation: 371

Quoting the docs

[...] Calling value(value) will set the current value of all elements in the Navigator. The argument can be of any type and will be coerced to a String if necessary. The exceptions are that when setting a checkbox value the method expects a boolean...

So, I didn't try this myself, but I think that if you can change your html to something like this (note that I added a name attribute to the element)

[...]
    <td class="select-row-cell">
        <input tabindex="-1" type="checkbox" name="GBP">
    </td>
    <td class="string-cell">.
        GBP
    </td>
[...]

Then the next code should do the trick:

$("checkbox", name: "GBP").value(true)    //Should set the checkbox to 'checked'

Upvotes: 0

Related Questions