Temporarily set js_errors to false in Poltergeist
Asked Answered
I

2

11

I have a set of tests which lead to a Facebook page where the user logs in. Unfortunately, this page has some JavaScript errors which I can't influence, so my tests would never finish.

Is there any way to temporarily disable the check for JS errors? I was thinking about something like: Capybara.javascript_driver.js_errors = false and then setting it to true later, but unfortunately this doesn't work. I have tried variations of this to no avail.

Any ideas on how my problem could be solved?

Ineffective answered 25/2, 2014 at 16:44 Comment(1)
Can you wrap test code to try{}catch(e){} block? I mean the part where Facebook page opens. Or tests itself not written in javascript?Handful
I
11

Thanks to Roman Pominov's comment I was able to find a solution. It was actually quite simple:

I just added rescue Capybara::Poltergeist::JavascriptError after the statement in question, and then it worked like a charm. My initial idea was too complicated ;)

Ineffective answered 26/2, 2014 at 9:31 Comment(5)
what was your initial idea? I have a large number of tests so disabling the js_errors in a before/after block is much preferable to adding rescue statements to each testLankester
Well, if possible, I would suggest to deal with all JS errors (they are errors, after all). If not possible, I would stick to rescue. My initial idea was described in the question, as far as I remember. Is it possible to solve those JS errors?Ineffective
Ah my bad, I thought maybe you had another idea that you had removed on an edit. And its a JS error that's throwing in an external service? I guess my bigger problem is that I have a feature spec visiting an external service in the first place.Lankester
Keep in mind this could roll back any operation that was running when the error happened i.e. I had log in form not submit when I caught the error js error from it.Acromegaly
YOu made my day! ;)Donnelldonnelly
O
7

This makes the trick:

page.driver.browser.js_errors = false

You may also add around callback:

# spec_helpers.rb
config.around(:each) do |example|
  original_value = page.driver.browser.instance_variable_get(:@js_errors)
  if example.metadata.has_key?(:js_errors)
    page.driver.browser.js_errors = example.metadata[:js_errors]
  end

  example.run

  page.driver.browser.js_errors = original_value
end

In your tests:

# my_feature_spec.rb
it "should ignore errors", js_errors: false do
  ...
end
Opposable answered 18/5, 2016 at 4:55 Comment(2)
This would not work if you have non-request specs, right? Is page going to be defined for other specs?Piccaninny
Note that if you're taking this approach in eg Cucumber, then JS errors will remain disabled for all subsequent tests as well!Helpmeet

© 2022 - 2024 — McMap. All rights reserved.