Webrat 说它找不到某些文本,但文本实际上在那里

发布于 2024-08-28 13:12:17 字数 601 浏览 4 评论 0原文

我有一个网页,上面有一个名为“删除”的表单按钮,还有一个 cuke 场景,其中包含以下行:

And I should see "delete"

当我运行该场景时,我收到此错误:

expected the following element's content to include "delete"

...并将 webrat 页面转储到 stdout 和“删除”实际上不存在。到目前为止,一切都很好。但是,当我告诉 webrat 在错误发生之前向我显示页面时:

Then show me the page
And I should see "delete"

...Safari 启动并向我显示该页面,并且在 Safari 中有“删除”按钮,完全在那里。

为什么 webrat 找不到表单按钮?我在表单字段上也遇到了同样的问题,例如页面加载时其中有值的文本输入,但 webrat 说那里什么也没有。在 Safari 中查看,再次显示该字段中确实包含正确的文本。

这是一个错误,还是 webrat 不适合检查表单元素?有其他方法可以做到这一点吗?谢谢!

I have a webpage that has a form button on it called "delete", and a cuke scenario that has the line:

And I should see "delete"

When I run the scenario, I get this error:

expected the following element's content to include "delete"

...and it dumps the webrat page to stdout and the "delete" is, in fact, not there. So far so good. However, when I tell webrat to show me the page before the error happens:

Then show me the page
And I should see "delete"

...Safari fires up and shows me the page, and in Safari there's the "delete" button, totally there.

Why is webrat not finding the form button? I've also had this same problem with form fields, such as text inputs that have a value in them when the page loads, but webrat says there's nothing there. Looking at it in Safari shows, again, that the field does have the right text in it.

Is this a bug, or is webrat just not suitable for checking form elements? Is there a different way to do this? Thanks!

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

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

发布评论

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

评论(4

药祭#氼 2024-09-04 13:12:17

您可以通过编写自己的步骤定义来检查表单元素。例如,要查找值为“删除”的按钮,请将您的功能编写为:

我应该看到值为“删除”的按钮

以及步骤定义:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value=#{value}]")
end

作为旁注,您会注意到,如果您的自定义步骤失败,它将转储整个响应,然后您将看到表单元素。我不完全确定这是为什么,但显然当你写“我应该看到......”时,Cucumber 与表单元素的值不匹配

Webrat 声称支持 CSS3 选择器,用于在使用 have_selector 语法时匹配 HTML 元素。

You can check for form elements by writing your own step definitions. For example, to find a button with a value of "delete", write your feature as:

And I should see a button with a value of "delete"

And a step definition of:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value=#{value}]")
end

As a side note, you'll notice that if your custom step fails, it will dump the entire response, and then you'll see the form element. I'm not entirely sure why this is, but apparently Cucumber is not matching the values of form elements when you write "I should see..."

Webrat claims to support CSS3 selectors for matching HTML elements when using the have_selector syntax.

尝蛊 2024-09-04 13:12:17

致 Leonardo:

对于其中包含空格的按钮标签,您需要将 value 包含在引号中,如下所示:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value='#{value}']")
end

To Leonardo:

For button labels with spaces in them, you need to include the value in quotation marks, like this:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value='#{value}']")
end
和影子一齐双人舞 2024-09-04 13:12:17

检查您正在测试的 HTML 是否有效

Check if the HTML you are testing against is valid

任谁 2024-09-04 13:12:17

万一其他人遇到这个。上面的答案对我不起作用(不知道为什么),但以下答案对我有用。

Then /^I should see a button with "([^"]*)"$/ do |button_text|
  if page.respond_to? :should
    page.should have_xpath("//input[contains(@value,'#{button_text}')]")
  else
    assert page.has_xpath?("//input[contains(@value,'#{button_text}')]")
  end
end

In case anyone else comes across this. The above answer didn't work for me (not sure why), but the following did.

Then /^I should see a button with "([^"]*)"$/ do |button_text|
  if page.respond_to? :should
    page.should have_xpath("//input[contains(@value,'#{button_text}')]")
  else
    assert page.has_xpath?("//input[contains(@value,'#{button_text}')]")
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文