如何使用 capybara + 断言 ruby​​ 中的 html 表行数黄瓜

发布于 2024-09-04 21:15:54 字数 328 浏览 2 评论 0原文

我正在尝试使用 cucumber + capybara 来掌握 Ruby 中的 BDD Web 开发,但我却陷入了本应是一项简单任务的任务 - 只是检查表中的行数。我想要实现的目的是:

page.should have_xpath("//table[@id='myTable']")
find("//table[@id='myTable']/tr").length.should == 3

但这不起作用(缺少方法长度),而且我找不到一种方法来断言表长度。

任何人有任何想法(请对我宽容一点,我是一个红宝石菜鸟)

提前

谢谢尼尔

I am trying to get to grips with BDD web development in Ruby using cucumber + capybara and I am stuck at what should be an easy task - just to check the number of rows in a table. The intention of what I'm trying to achieve is something along the lines of:

page.should have_xpath("//table[@id='myTable']")
find("//table[@id='myTable']/tr").length.should == 3

But this doesn't work (missing method length) and I can't find a way to assert against the table length.

Any ideas anyone (please be easy on me tho' I'm a ruby nooby)

Thanks in advance

Neil

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

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

发布评论

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

评论(6

百变从容 2024-09-11 21:15:54

尽管 have_css? 可以解决问题,但您的测试应该告诉您它们是如何失败的,而不是仅仅说某些条件应该为真而它是假的。考虑到这一点,下面的第一个示例比第二个示例读起来要好得多:

# IF FAILED => "expected 10, got 7"
page.all('table#myTable tr').count.should == 10

# IF FAILED => "expected true, got false"
page.should have_css("table#myTable tr", :count=>10)

Even though have_css? will do the trick, your tests should tell you how they failed, rather than just saying some condition was supposed to be true and it was false. With this in mind, the first example below reads much better than the second:

# IF FAILED => "expected 10, got 7"
page.all('table#myTable tr').count.should == 10

# IF FAILED => "expected true, got false"
page.should have_css("table#myTable tr", :count=>10)
小忆控 2024-09-11 21:15:54

我认为你可以这样做:

page.should have_css("table#mytable tr", :count=>3)

I think you can do this:

page.should have_css("table#mytable tr", :count=>3)
山色无中 2024-09-11 21:15:54

由于某种原因“has_css”对我不起作用,但是“all(选择器)”效果很好

all("table#movies tr").count

For some reason "has_css" does not work for me, however "all(selector)" works really wel

all("table#movies tr").count
小…红帽 2024-09-11 21:15:54

我最终选择了这个:

Then /^I should see "(.*)" once$/ do |text|
    within_table('myTable') do
      should have_xpath("//tr", :text => text, :count => 1)
    end
end

这看起来相当优雅。

我意识到其他答案有效,但这似乎读起来很好。

有什么意见吗?

I went with this in the end:

Then /^I should see "(.*)" once$/ do |text|
    within_table('myTable') do
      should have_xpath("//tr", :text => text, :count => 1)
    end
end

which seemed suitably elegant.

I realise the other answers work but this seems to read well.

Any comments?

黑凤梨 2024-09-11 21:15:54

#find 方法仅返回一个元素(我认为如果有多个匹配项,它只返回第一个元素),因此您不会获得 #length 方法,因为 #find 的结果是一个节点而不是数组。

为了向自己证明这一点,请尝试

puts find("//table[@id='myTable']/tr").class

您想要的是#all,它将返回所有匹配节点的数组。

The method #find only returns one element (I think it just returns the first one if there are several matches) so you don't get a #length method because the result of #find is a Node not an Array.

To prove this to yourself, try

puts find("//table[@id='myTable']/tr").class

What you want is #all, which will return you an Array of all the matching nodes.

看春风乍起 2024-09-11 21:15:54

通过这种方式你可以得知html表格的行数。

area = find_by_id('#areaID').all('tr').size

假设表的开头有列,这样就可以得到实际的列数。

area = area-1

In this way you can learn the number of lines in the html table.

area = find_by_id('#areaID').all('tr').size

Assume that there are columns at the beginning of the table.You can reach the actual number in this way.

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