سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top