TypeError When dealing with link clicks via Capybara::Session
BIGred053 opened this issue · comments
Meta
Poltergeist Version: 1.6.0/1.18.1
(Tried both, saw issues with both)
Expected Behavior
I am attempting to confirm the correct function of a link with target="_blank"
by switching to the window opened up when clicking the link, then checking the current_url
. This should return the url opened by clicking the link.
Actual Behavior
In reality, I am getting the following error when attempting to switch_to_window
:
Failure/Error: switch_to_window(new_window)
Capybara::Poltergeist::JavascriptError:
One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
TypeError: Attempting to configurable attribute of unconfigurable property.
TypeError: Attempting to configurable attribute of unconfigurable property.
at https://www.gstatic.com/recaptcha/api2/v1562567553145/recaptcha__en.js:5 in defineProperty
at https://www.gstatic.com/recaptcha/api2/v1562567553145/recaptcha__en.js:5 in fZ
at https://www.gstatic.com/recaptcha/api2/v1562567553145/recaptcha__en.js:16
If I attempt the switch_to_window
in binding.pry
, it gives this error code, but actually seems to process switching windows, since afterwards I can check current_url
and get the correct result and a passing test.
Since I ran into this with versions 1.6.0 and 1.18.1, I decided to try to work around this by executing some javascript to reset the links' target
to "self". After this workaround, when testing the specs involved individually, they typically pass. However, when I run the entire file of specs, they fail locally. They do pass in our CI process, though.
If this is an error in something in my implementation, please let me know.
Steps to reproduce
Here is the code I attempt:
new_window = window_opened_by { row_to_click.click_link }
sleep 3
switch_to_window(new_window)
expect(current_url).to include anticipated_url # use include in case routing adds path to root url
Here is my 'workaround' code:
before do
execute_script("$('.link-to-click').each(function() {$(this).attr('target', '_self');})")
end
scenario 'the user is redirected to the correct path' do
row_to_click.click_link
sleep 3 # wait for event logging
expect(current_url).to include anticipated_url # In case link redirects to more specific path
end
You're getting errors from the JS in the page you're loading. PhantomJS (which Poltergeist depends on) went EOL a while ago and does a pretty terrible job of replicating a modern browser nowadays, so there's a lot of JS libraries that are no longer compatible with it. Thats a large part of the reason Poltergeist is no longer maintained. You will have a better time if you upgrade to one of the newer, more modern browser using drivers - https://github.com/teamcapybara/capybara#drivers
Additionally - by doing - expect(current_url).to include anticipated_url
you're opening yourself to a race condition. You'd be much better off using Capybaras matcher with a regex expect(page).to have_current_path(%r{^http://x\.y\.z/blah})
Thanks for this feedback!