Capybara + Webkit:如何测试客户端验证-“必需”输入元素?

dgi*_*rez 7 html5 webkit rspec ruby-on-rails capybara

我正在尝试使用Rspec和Capybara对表单进行失败的验证,该表单未填写“必需”输入,因此失败。理解HTML5的新导航员提供了内置的验证,我了解Capybara也在使用该验证。以前我在用

page.should have_error
Run Code Online (Sandbox Code Playgroud)

这对我不起作用了。

有人知道现在如何测试吗?

非常感谢!

大卫

fro*_*001 11

HTML5 客户端验证很难找到。我发现这篇文章有一个很好的答案。代码是:

describe "when I leave required field empty" do
  it "I get an the correct html5 validation error" do
    #Leave the field empty
    click_on "Save" # or whichever button triggers the submit

    message = page.find("#field_id_attr").native.attribute("validationMessage")
    expect(message).to eq "Please fill out this field."
  end
end
Run Code Online (Sandbox Code Playgroud)

基本上它的工作方式是 field 元素有一个名为“validationMessage”的属性,过程是:

  1. 单击提交 - 这会触发错误消息
  2. 获取对称为“validationMessage”的 native(html) 属性(与 Ca​​pybara 页面对象属性相对)的引用。这将为您提供价值或消息​​本身。
  3. 断言该值符合预期。