Webrat说它找不到一些文本,但是文本确实存在

[英]Webrat says it can't find some text, but the text is actually there


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

我有一个网页,上面有一个表单按钮,叫做“删除”,还有一个cuke方案,上面有一行:

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:

…它将webrat页面转储到stdout,而“delete”实际上不在那里。目前为止一切都很顺利。然而,当我告诉webrat在错误发生之前向我显示页面时:

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.

…Safari启动并显示页面,在Safari中有一个“删除”按钮,完全在那里。

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.

为什么webrat没有找到表单按钮?我在表单字段上也遇到过同样的问题,比如当页面加载时文本输入有一个值,但是webrat说那里什么都没有。再一次,在Safari中查看它,可以发现字段中确实有正确的文本。

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

这是一个bug,还是webrat仅仅不适合检查表单元素?有别的方法吗?谢谢!

4 个解决方案

#1


2  

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:

您可以通过编写自己的步骤定义来检查表单元素。例如,要找到一个值为“delete”的按钮,请将您的特性写为:

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

我应该会看到一个值为“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..."

作为补充说明,您将注意到,如果自定义步骤失败,它将转储整个响应,然后您将看到form元素。我不完全确定为什么会这样,但显然Cucumber在写“我应该看到…”的时候并没有与表单元素的值相匹配。

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

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

#2


1  

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

#3


0  

Check if the HTML you are testing against is valid

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

#4


0  

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
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/04/15/d35670192a543618c60322aeadff5ec6.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告