rspec-html

0.3.5
Light Mode Dark Mode

Attributes

If you need to verify the exact value of an attribute, use Hash key lookup syntax by calling #[].

subject(:document) do
  parse_html('<div><input type="text" value="my-value"></div>')
end

it 'has an input whose value is "my-value"' do
  expect(document.div.input[:value]).to eql 'my-value'
end
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
    <div><input type="text" value="my-value"></div>
  </body></html>

One particular use case that makes this especially useful is retrieving an anti-CSRF token from a form before submitting a POST or PATCH request:

it 'submits a user form' do
  get '/users/new'
  token = document.form.input(name: 'authenticity_token')[:value]
  post '/users', params: { user: { email: 'user@example.com' }, authenticity_token: token }
  expect(document.div('.flash.notice')).to match_text 'User successfully created'
end
<div class="flash notice">User successfully created</div>
User successfully created

Documentation generated by rspec-documentation