简体   繁体   中英

Capybara selectors matched but not to all filter, what does this mean?

I have the following html saved with the save_and_open_page command right before the capybara selectors are run (I've also checked page.body, it's the same): https://gist.github.com/davidkovaccs/8991af5bed1805277f52

I get the following error message: 'expected to find xpath "//div[@class='box']" 9 times, found 6 matches: "box_2001 attrkulcs1: attrvalue 1 attrkulcs2: attrvalue 2 attrkulcs3: attrvalue 3", "box_2002 attrkulcs1: attrvalue 1 attrkulcs2: attrvalue 2 attrkulcs3: attrvalue 3 attrkulcs4: attrvalue 4", "box_2003", "box_1001", "box_1002 attrkulcs1: attrvalue 1 attrkulcs2: attrvalue 2 attrkulcs3: attrvalue 3", "box_1003 attrkulcs1: attrvalue 1". Also found "", "", "", which matched the selector but not all filters. (Capybara::ExpectationNotMet)'

Note that the found 6 matches is not 6 for every test run. it a number between 4 and 9. I don't really understand what can be the 'matched the selector but not all filters' statement. As I know I'm not using any filters.

I've tried the following selectors, but all of them gives the same error message:

have_xpath("//div[@class='box']", :count => 9)
have_selector(:css, '.box', :count => 9)
have_css('.box', :count => 9)

I've tried it with capybara 2.2.1 and 2.3.0 also. I'm using Selenium webdriver 2.42.0

Running $x("//div[@class='box']").length from browser console works perfectly.

I'm copying twalpole's answer from github ( https://github.com/jnicklas/capybara/issues/1326 ):

"You are actually using filters count and visible (defaults to true) without the relevant css it's impossible to say for sure, however the three empty sets of quotes would tend to indicate that 3 of the .box elements weren't actually visible on the page"

Indeed this was the problem, I've had to add a 'visible: false' parameter to my selectors to search for the elements that cannot be seen in the screen.

'matched the selector but not all filters' means:

  • matched the selector : a "div"
  • did not match the filter : @class='box'

So it seems you always have 9 div's, but a few of them are not recognized with the expected .box class.

What happens if you manually look at the raw html (do all 9 div's always have the correct .box class attached)?

UPDATE:

I probable was incorrect in my assumption of selector and filters. The whole block "//div[@class='box']" is the selector and the optional filters are things like text: 'foo' etc.

Also, I pasted your html in a project and bundle updated that to capybara(2.2.1) and all your tests pass:

```

expect(response.body).to have_xpath("//div[@class='box']", :count => 9)
expect(response.body).to have_selector(:css, '.box', :count => 9)
expect(response.body).to have_css('div.box', :count => 9)

```

So, I am unable to reproduce the problem ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM