如何使用 Watir 循环浏览链接并单击每个链接

发布于 2024-08-10 02:09:59 字数 417 浏览 1 评论 0原文

我有一个带有网格的网页,某些列的列标题中有一个链接,该链接将按该列对值进行排序[通过往返服务器]。

我想编写一个 Watir 测试来识别这些排序链接并连续单击每个链接。这是一个冒烟测试,可确保排序表达式不会导致运行时异常。

我的问题是,(1)识别这些链接,(2)连续单击每个链接的最佳方法是什么?这是我到目前为止所拥有的:

$ie.table(:id, "myGrid").body(:index, 1).each do | row |
  row.each do | cell |
    ## todo: figure out if this contains a sort link
    ## todo: click the link now, or save a reference for later?
  end
end

I have a web page with a grid, and some columns have a link in the column header that will sort the values by that column [by round-tripping to the server].

I want to write a single Watir test that will identify those sorting links and click each one in succession. This is a smoke test that ensures there are no runtime exceptions resulting from the sort expression.

My question is, what is the best way to (1) identify these links, and (2) click each one in succession? This is what I have so far:

$ie.table(:id, "myGrid").body(:index, 1).each do | row |
  row.each do | cell |
    ## todo: figure out if this contains a sort link
    ## todo: click the link now, or save a reference for later?
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-08-17 02:09:59

我认为不可能单击循环内的每个链接,因为单击链接将触发页面加载,并且我相信这会使其他 Watir 链接元素无效。

因此,我建议您获取链接的 ID,然后相互循环进行点击。


例如,我会向这些排序链接添加一个特定的类以及一个有意义的 id。
大致如下:

<a id='sortby-name' class='sorting' href='...'>Name</a>

然后你可以这样选择它们:

$ie.table(:id, "myGrid").body(:index, 1).each do | row |
  # pick sorting links' ids
  sorting_link_ids = row.links.select{|x| x.class_name == "sorting"}.map{|x| x.id}
end

然后,你像这样点击:

sorting_link_ids.each do |link_id|
  $ie.table(:id, "myGrid").body(:index, 1).link(:id, link_id).click
end

希望有帮助!

I think it won't be possible to click each link inside the loop since clicking a link will trigger a page load and I believe this invalidates the other Watir Link Elements.

So, what I suggest is that you take the IDs of the links and then loop over each other doing the clicks.


For instance, I would add an specific class to those sorting links and a meaningful id too.
Something along the lines of:

<a id='sortby-name' class='sorting' href='...'>Name</a>

Then you can pick them with this:

$ie.table(:id, "myGrid").body(:index, 1).each do | row |
  # pick sorting links' ids
  sorting_link_ids = row.links.select{|x| x.class_name == "sorting"}.map{|x| x.id}
end

And then, you do your clicking like this:

sorting_link_ids.each do |link_id|
  $ie.table(:id, "myGrid").body(:index, 1).link(:id, link_id).click
end

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文