twalpole / apparition

Capybara driver for Chrome using CDP
MIT License
363 stars 46 forks source link

page.find(xyz).click intermittently does not work... #37

Closed patrick99e99 closed 4 years ago

patrick99e99 commented 4 years ago

I (frustratingly) ran into this problem years and years ago when using poltergeist, and so I had switched to capybara-webkit which eliminated it... Now wanting to use chrome for integration testing, I have been working on switching over from webkit to apparition, and unfortunately am seeing this behavior once again.

It appears related to this recently closed issue: https://github.com/twalpole/apparition/issues/34

And you can see others had reported this same bug way back when in the poltergeist repo but mistakingly thought that it has something to do with animations: https://github.com/teampoltergeist/poltergeist/issues/530

I have an angular + rails app, and am rendering a simple angular directive which is basically a div container that has a checkbox input within it. For testing purposes, I made the on-click event for the checkbox input execute a function which adds a class of "clicked" to its div container.

My test is simply doing:

page.find('checkbox input').click
expect(page).to have_selector('.clicked')

The majority of test runs, this fails saying there was no .clicked selector found, but sometimes it passes.

If I change this test to do page.find('checkbox input').trigger(:click) then it works 100% of the time...

So in my case, there are no animations, nothing asynchronous happening what-so-ever---

As per Jordoh's answer in the checkbox issue-- he suggested this 'wait_for_document_load_complete' method, but prior to clicking I have output page.driver.evaluate_script('document.readyState') and the value is complete so that is not the issue...

twalpole commented 4 years ago

The driver works by clicking in the browser at the location where the element you click is reported to be. capybara-WebKit worked slightly differently because it could sit inside the browsers event loop. If click is not reporting errors but not producing the behavior you expect there are only 3 possible reasons

  1. Your page is broken 2 Animation of some type is causing the click to happen where the element was rather than where it is
  2. The click is happening before click handlers are added to the page

I guess technically there’s a 4th - the calculation of element position is wrong, although the fact that apparitions tests pass pretty much rules that out

If you provide a simple reproducible example that shows the issue I will narrow down what the issue is, but’s it’s going to be one of the 3