Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara field.has_css? matcher

I'm using following spec with MiniTest::Spec and Capybara:

find_field('Email').must_have_css('[autofocus]')

to check if the field called 'Email' has the autofocus attribute. The doc says following:

has_css?(path, options = {})

Checks if a given CSS selector is on the page or current node.

As far as I understand, field 'Email' is a node, so calling must_have_css should definitely work! What I'm doing wrong?

like image 703
Mario Uher Avatar asked Nov 26 '11 23:11

Mario Uher


1 Answers

Got an answer by Jonas Nicklas:

No, it shouldn't work. has_css? will check if any of the descendants of the element match the given CSS. It will not check the element itself. Since the autofocus property is likely on the email field itself, has_css? will always return false in this case.

You might try:

find_field('Email')[:autofocus].should be_present

this can also be done with XPath, but I can't recall the syntax off the top of my head.


My solution:

find_field('Email')[:autofocus].must_equal('autofocus')
like image 152
Mario Uher Avatar answered Sep 29 '22 08:09

Mario Uher