Andrew Kozin
Andrew Kozin

Reputation: 1022

Rspec have_field raises an error while have_selector return success

In the example below 3 have_field specs fail, while 2 have_selector pass.

describe "without js support", js: false do
  subject { page }      

  it { should have_selector "form label[for='user_password']", text: "Password" }
  it { should have_selector "form input#user_password[name='user[password]'][type='password'][required]" }
  it { should have_field "user_password" }  # check by field id
  it { should have_field "user[password]" } # check by field name
  it { should have_field "Password" }       # check by field label
end

In the template being tested I actually have (js support disabled in browser):

<label for="user_password" id="label_password">Password</label>
<input id="user_password" name="user[password]" required="required" type="password" />

have_selector specs pass as expected, but have_field aren't. Why?

Even more interesting is after I change an example to:

describe "with js support", js: true" do
...

than all 5 specs become green. It's wonderful, but I have no damned idea, what is wrong with my nojs specs.

Upvotes: 1

Views: 919

Answers (1)

Andrew Kozin
Andrew Kozin

Reputation: 1022

Well, my experiments shows that instead of simply testing have_field

it { should have_field "user_password" }

I have to explicitly set field type

it { should have_field "user_password", :type => :password }

Then the test passes. (Be careful! Use :password, not 'password' in type setting).

And if you have to check password hasn't value, it can be done as usual

it { should have_field "user_password", :type => :password, :with => nil }

Upvotes: 1

Related Questions