黄瓜,如何检查父级是否有某个班级?

发布于 2024-11-24 04:48:52 字数 343 浏览 0 评论 0原文

我正在使用带有 Rails 的黄瓜,并且我正在检查元素的父元素是否具有特定的类。我找到了这段代码,但它不起作用。顺便说一句,我正在使用水豚。提前致谢!

Then the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)" do |element_id,parent,css_class|
  response.should have_selector "#{parent} .#{css_class}" do |matches|
    matches.should have_selector element_id
  end
end

I am using cucumber with rails, and I'm checking if the parent of an element has a certain class. I found this code, but it doesn't work. I'm using Capybara by the way. Thanks in advance!

Then the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)" do |element_id,parent,css_class|
  response.should have_selector "#{parent} .#{css_class}" do |matches|
    matches.should have_selector element_id
  end
end

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

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

发布评论

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

评论(1

淤浪 2024-12-01 04:48:52

从技术上讲,这并不是真正应该用黄瓜进行测试的方式。 Cucumber 用于测试行为,而不是检查 DOM - 当然它可以以这种方式使用。

如果你真的想写这样的东西,你可以让它变得更简单:

Then /^the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)"$/ do |element_id,parent,css_class|
  page.should have_css("#{parent}.#{css_class} #{element_id}")
end

不过,这在很多层面上都很糟糕。您不仅直接从该步骤检查 DOM(这会产生非常不可读的功能),而且还混合传递元素名称、类名称和元素 ID - 每个元素的样式都略有不同...在此步骤中,元素可以可以是任何类型的选择器,而parent和css_class则更加固定 - 它们必须始终“适合”选择器字符串,否则它将找不到任何内容。

我还没有很好地解释这一点,但简而言之,您应该考虑您实际要测试的内容,并考虑是否可以将其重命名为更有用和可重用的内容。您是否能够重复使用该步骤,而无需查看其实现来弄清楚什么去了哪里?

此外,通过更具表现力的命名,测试稍后会立即变得更加有用。例如,步骤 Then I should see the current list is activeThen 具有父级“ul”的元素“li.active”应该具有类“active-”更具可读性和表现力。列表”。在步骤定义实现中要具体,而不是您的功能!

阅读这篇博文 - http://elabs.se/blog /15-you-re-cuking-it-wrong - 它应该让您很好地了解如何编写更好的步骤。

Technically, this isn't really the way you should be testing with cucumber. Cucumber is for testing behaviour, not checking the DOM - though of course it can be used in that way.

If you're really wanting to write something like this, you could make that much simpler:

Then /^the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)"$/ do |element_id,parent,css_class|
  page.should have_css("#{parent}.#{css_class} #{element_id}")
end

This sucks on many levels, though. Not only are you checking the DOM directly from the step, which makes for very unreadable features, but you're mixing passing in element names, class names and element IDs - each with a slightly different style... In this step, element can be any type of selector whereas parent and css_class are much more fixed - they must always 'fit' into the selector string or it won't find anything.

I haven't really explained that very well, but in a nutshell, you should consider what you're actually trying to test and think whether it can be renamed to something more useful and re-usable. Would you ever be able to re-use that step without looking at it's implementation to figure out what goes where?

Also, with more expressive naming, the test instantly becomes more useful later down the line. For example, the step Then I should see the current list is active is much more readable and expressive than Then the element "li.active" with parent "ul" should have class "active-list". Be specific in the step definition implementation, not your features!

Have a read of this blog post - http://elabs.se/blog/15-you-re-cuking-it-wrong - it should give you a good idea of how to write better steps.

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