I'd like to be able to do this:
find('input["name=desired_amount"]:invalid')
Right now, using Selenium, I get the following error:
Selenium::WebDriver::Error::InvalidSelectorError:
The given selector .//input[@name = 'desired_amount' and invalid(.)] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression .//input[@name = 'desired_amount' and invalid(.)] because of the following error:
[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "file:///var/folders/gc/3hx7nhq5257_6dzxblc3s1k00000gn/T/webdriver-profile20130104-35671-uj1dp/extensions/[email protected]/components/driver_component.js Line: 5697"]
Using Webkit:
Failure/Error: find("input[name='desired_amount']:invalid")
RuntimeError:
xmlXPathCompOpEval: function invalid not found
What is :invalid supposed to do?
I hope you'll forgive me for defaulting to the documentation: I think it will do a better job of explaining it than me. However, the short version is that it is the class thanks to which the form elements that fail validation glow red.
The :invalid CSS pseudo-class represents any <input> or <form> element whose content fails to
validate according to the input's type setting. This allows you to easily have invalid fields adopt
an appearance that helps the user identify and correct errors.
And, because a pseudo-class is kinda weird, here is its definition, again from MDN:
A CSS pseudo-class is a keyword added to selectors that specifies a special state of the
element to be selected. For example :hover will apply a style when the user hovers over
the element specified by the selector.
Pseudo-classes, together with pseudo-elements, let you apply a style to an element not only
in relation to the content of the document tree, but also in relation to external factors like the
history of the navigator (:visited, for example), the status of its content (like :checked on
some form elements), or the position of the mouse (like :hover which lets you know if the
mouse is over an element or not).
Ahh, I see. This would be difficult for us to support, since Capybara actually doesn't do anything with CSS selectors. We just use Nokogiri to convert them to XPath, before handing off the XPath to the driver. I don't think that XPath can express the "invalid" notion, so this is probably pretty impossible. We might be able to support it with a syntax more like this:
find('input["name=desired_amount"]', :invalid => true)
I would be happy with that. I think it makes sense. So... This means that the 'invalid' pseudo-class would be applied to every selector in the string, right? example:
find('input[name="desired_amount"] select[name="desired_amount"]', :invalid => true)
Would translate to looking for the following two things, correct?
input[name="desired_amount"]:invalid
select[name="desired_amount"]:invalid
I hope Capybara is able to support this.
Oh, also, I was able to get this working successfully through Browser#find_elements, like so:
browser = Capybara.current_session.driver.browser
browser.find_elements(:css, "input[name='desired_amount']:invalid")
But it's not the open API, and doing this is a hack, so it doesn't work with drivers like poltergeist.
I'm closing this, so as to not leave feature requests lying around. If someone wants this, please send a pull request.
That doesn't make any sense. No one is going to go sift through closed issues to find feature requests. I understand that you want to keep your list of issues clean, but why not just create a tag for features and leave that alone?
The only way someone will find this is if they happen to do a Google search for exactly this; if you leave it open, any number of people can see it and be inspired to help.
I highly doubt that someone is going to go to the Capybara issue tracker to find some fun stuff to work on. That very rarely happens. Most people who contribute do so because they have a need for a certain feature. In that case they can search for it. I've seen feature requests which were really good ideas linger around for years, with no one ever stepping up to fix it. In the end they just become distractions from what really needs to be done.
@trevoke PR #961 has been merged into master, so the :invalid pseudo selector should work once capybara 2.1 is released and whatever driver you are using is updated (assuming the browser you are testing against supports :invalid)
I'm happy to be wrong :)
@twalpole Wonderful, thank you!
Most helpful comment
I'm happy to be wrong :)