capybara-webkit icon indicating copy to clipboard operation
capybara-webkit copied to clipboard

Failed to click element errors

Open jnicklas opened this issue 13 years ago • 10 comments

I just tried to run edge capy-webkit on an app, to see how we're doing towards Capybara 2.1 compatibility, and I ran into a lot of "Failed to click element" errors. These appear to be due to the changes in #463. From what I can tell they don't seem to be justified.

This is a fairly minimal repro:

require "capybara"
require "capybara-webkit"

html = %(<a href="/something"><i></i>Some link</a>)
app = proc { |env| [200, { "Content-Type" => "text/html" }, [html] ] }

sess = Capybara::Session.new(:webkit, app)
sess.visit("/")
sess.click_link("Some link")

Removing the inner empty <i> tag, or even adding content to it will make the test pass.

This passes:

<a href="/something">Some link</a>
<a href="/something"><i>a</i>Some link</a>

This does not:

<a href="/something"><i></i>Some link</a>

I haven't really worked on the capy-webkit source before, and so far I've been unable to debug this sensibly, I'll keep trying, maybe I can figure out why this is, and how to work around it.

My blind guess is that the clickTest method, implemented here, somehow checks the visibility of the clicked node. Empty nodes are counted as invisible, even if they have non-zero width and height, IIRC.

jnicklas avatar Mar 01 '13 12:03 jnicklas

(The selenium driver runs the above code without problem, btw)

jnicklas avatar Mar 01 '13 12:03 jnicklas

Awesome, thanks for bringing this up. We just implemented native click events, which is something Poltergeist has been doing for a while. It seems that the empty node has a clientRect width of 0, which can't be clicked. We should really click the first node with a width greater than 0. Ping @jonleighton as Poltergeist likely has the same bug.

mhoran avatar Mar 02 '13 05:03 mhoran

@mhoran thanks for the heads up, but doesn't look like poltergeist is affected:

$ cat test.rb 
require "capybara"
require "capybara/poltergeist"

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, debug: true)
end

html = %(<a href="/something"><i></i>Some link</a>)
app = proc { |env| [200, { "Content-Type" => "text/html" }, [html] ] }

sess = Capybara::Session.new(:poltergeist, app)
sess.visit("/")
sess.click_link("Some link")
$ ruby test.rb 
{"name"=>"set_debug", "args"=>[true]}
{"response"=>true}
{"name"=>"visit", "args"=>["http://127.0.0.1:46284/"]}
poltergeist [1362220257560] state default -> loading
poltergeist [1362220257588] state loading -> default
{"response"=>{"status"=>"success"}}
{"name"=>"find", "args"=>[".//a[./@href][(((./@id = 'Some link' or contains(normalize-space(string(.)), 'Some link')) or contains(./@title, 'Some link')) or .//img[contains(./@alt, 'Some link')])]"]}
{"response"=>{"page_id"=>2, "ids"=>[0]}}
{"name"=>"click", "args"=>[2, 0]}
poltergeist [1362220257605] state default -> clicked
poltergeist [1362220257613] state clicked -> loading
poltergeist [1362220257622] state loading -> default
{"response"=>{"status"=>"success", "click"=>{"x"=>8, "y"=>8}}}
poltergeist [1362220257623] state default -> default

I'm not too sure why it's not affected, but it seems like it's ok.

jonleighton avatar Mar 02 '13 10:03 jonleighton

@jonleighton, it looks like the difference is in how Poltergeist and capybara-webkit calculate the center of an element. Poltergeist returns the origin of an empty node, while capybara-webkit returns 0, 0. This is due to the fact that Qt considers zero-width rectangles invalid, and the center of an invalid rectangle is 0, 0 (!). This behavior is probably fine, but I went ahead and changed clickPosition to return the center of the first non-empty node. This way we'll always click in the center of something, not the origin.

mhoran avatar Mar 04 '13 01:03 mhoran

@jnicklas, would you let me know if 1de4647fad5459133a2fc38a9d69231dc0a8b289 resolves this issue for you?

mhoran avatar Mar 04 '13 01:03 mhoran

Doesn't seem like this fixed it. I'm now getting:

     Capybara::Webkit::ClickFailed:
       Failed to click element /html/body/div/div[@id='main']/div/section/ul[2]/li[@id='post_1']/article/div/div/ul[1]/li/a at unknown position

I'll investigate further and see if I can nail down the cause.

jnicklas avatar Mar 08 '13 09:03 jnicklas

@jnicklas I also experienced "Failed to click element" errors. And current commit fixes the test you provided. Try checking dialogs or drop-down boxes which might remain opened, overlap elements you want to click and lead to the click error. E.g. in my scenario it was necessary to close the 'select2' drop-down explicitly.

DimaSamodurov avatar Mar 08 '13 14:03 DimaSamodurov

@jnicklas, this is likely due to dialogs or drop-down boxes as @DimaSamodurov pointed out. Native events require the mouse pointer to hit the target, and that won't happen if the event propagation is blocked by an element. However, because there's not a real user clicking an element, we have to guess at what point on the element may be clickable. We click the first non-zero width and height element, which could be blocked by another element.

mhoran avatar Mar 10 '13 22:03 mhoran

Note that Selenium hovers, and likely clicks, the center of the boundingClientRect, which may be incorrect as detailed in the following test case: http://git.io/LFP2qA.

mhoran avatar Mar 10 '13 22:03 mhoran

Closing in favor of #494.

mhoran avatar May 22 '13 02:05 mhoran