1

使用 page-object 和 watir-webdriver 如何根据以下行文本单击表中的链接:

该表包含 3 行,第一列中有名称,右侧列中有相应的详细信息链接:

仪表板 .... 详细信息

示例 .... 详细信息

等等。

<div class="basicGridHeader">
    <table class="basicGridTable">ALL THE DETAILS: 
    ....soon
    </table>

</div>

<div class="basicGridWrapper">
    <table class="basicGridTable">
        <tbody id="bicFac9" class="ide043">
            <tr id="id056">
                <td class="bicRowFac10">
                    <span>
                        <label class="bicDeco5">
                            <b>DASHBOARD:</b> ---> Based on this text
                        </label>
                    </span>
                </td>

                <td class="bicRowFac11">
                    ....some element
                </td>

                <td class="bicRowFac12">
                    <span>
                        <a class="bicFacDet5">Details</a> ---> I should able click this link
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</div>
4

3 回答 3

4

您可以找到包含指定文本的单元格,转到父行,然后在该行中找到详细信息链接。

假设您可能要单击其他详细信息链接,我将定义一个 view_details 方法,该方法接受您要定位的行的文本:

class MyPage
  include PageObject

  table(:grid){ div_element(:class => 'basicGridWrapper')
    .table_element(:class => 'basicGridTable') }

  def view_details(label)
    grid_element.cell_element(:text => /#{label}/)
      .parent
      .link_element(:text => 'Details')
      .click
  end

end

然后,您可以单击链接:

page.view_details('DASHBOARD')
于 2013-09-13T13:15:10.197 回答
3

表格元素包括 Enumerable 模块,我发现它在这种情况下非常有用。http://ruby-doc.org/core-2.0.0/Enumerable.html。您可以使用 find 方法来定位并返回与您正在查找的条件匹配的行。例如:

class MyPage
  include PageObject

  table(:grid_table, :class => 'basicGridTable')

  def click_link_by_row_text(text_value)
    matched_row = locate_row_by_text(text_value)
    matched_row.link_element.click
    #if you want to make sure you click on the link under the 3rd column you can also do this...
    #matched_row[2].link_element.click
  end

  def locate_row_by_text(text_value)
    #find the row that matches the text you are looking for
    matched_row = grid_table_element.find { |row| row.text.include? text_value }
    fail "Could not locate the row with value #{text_value}" if matched_row.nil?
    matched_row
  end
end

在这里,locate_row_by_text 将查找包含您要查找的文本的行,如果没有找到它会抛出异常。然后,一旦找到该行,您就可以深入到该链接,并单击它,如 click_link_by_row_text 方法中所示。

于 2013-09-14T17:09:22.937 回答
0

只是为了后代,我想给出一个更新的答案。现在可以使用 遍历表table_element[row_index][column_index]。稍微详细一点:

  1. row_index也可以是要匹配的一行中的文本-在您的情况下-table_element['DASHBOARD']

  2. 然后使用索引(从零开始)或该列的标题找到相应的单元格/td 元素

table_element['DASHBOARD'][2]- 选择所选行中的第三个元素。

由于您没有标题行(<th>元素),您可以使用链接的类属性过滤单元格元素。像这样的东西

table_element['DASHBOARD'].link_element(:class => 'bicRowFac10').click

所以代码看起来像这样:

class MyPage
  include PageObject

  def click_link_by_row_text(text_value)
    table_element[text_value].link_element(:class => 'bicRowFac10').click      
  end
end

如果您需要更多解释,请告诉我。乐意效劳 :)

于 2017-05-17T16:48:21.013 回答