Frage

I'm automating a task using Ruby and Watir.

I want to set a checkbox (which is in the first column of a table) based on whether the value in the second column matches my input value. For example, in the following code snippet, the value "brett58" matches my input value so I want to set the checkbox associated with it.

<tr class="cuesTableRowOdd">
    <td align="center">
        <input class="content-nogroove" type="checkbox" name="result[1].chked" value="true">
        <input type="hidden" value="7bd67e4d-a59f-0143-3886-22c1d205a5c1" name="result[1].col[0].stringVal">
        <input type="hidden" value="brett58" name="result[1].col[1].stringVal">
    </td>
    <td align="left">
        <a class="cuesTextLink" href="userEdit.do?key=7bd67e4d-a59f-0143-3886-22c1d205a5c1">brett58</a>
    </td>
    <td align="left">brett</td>
    <td align="left">lee</td>
    <td align="left"></td>
</tr>

But I'm unable to do it. In the above case, the following line serves the purpose (as my input value matches with that it first row):

browser.table(:class, "cuesTableBg").checkbox(:name, "result[1].chked").click

But it can't be used as the required value might not always be in the first row.

Looping through all the rows is one option but it's inefficient.

War es hilfreich?

Lösung

I would do:

browser.table(:class, "cuesTableBg").rows.find{ |row| 
  row.cells[1].text == 'brett58'
}.checkbox.set

Basically, this is saying find the first row of the table that has "brett58" in the second column. Then set the first checkbox in that row.

Andere Tipps

Try something like this:

def set_checkbox(row_text_to_match)
  @browser.table(:class, "cuesTableBg").row(:text, /#{Regexp.escape(row_text_to_match)}/).checkbox(:name, "result[1].chked").set
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top