Simulate network failure / offline mode in Capybara?

My application has JavaScript that detects when a network connection is leaving and temporarily caches data in local storage to synchronize with the server when reconnecting.

I am trying to find a way to test this end-to-end usage with Capybara, but I cannot find a way to temporarily disable the application server or switch the dumb browser offline. FWIW I am using Poltergeist as a driver.

Does anyone have an idea how to check this? (I can test the JavaScript application using synon to fake the server by leaving, but I would like it to be able to test it with a pass-through browser without a browser, if possible).

+4
source share
2 answers

My team cut out a Rack application to simulate errors from the server. It works quite well (in Firefox). Here are some excerpts from the code:

class NoResponseRack
  attr_reader :requests

  def initialize disconnected_mode
    @disconnected_mode = disconnected_mode

    @requests = []
    @sleeping_threads = []
  end

  def call(env)
    @requests.push(env)

    case @disconnected_mode
    when :elb_pool_empty
      @sleeping_threads << Thread.current
      sleep 65
      @sleeping_threads.delete Thread.current
      [504, {}, ['']]
    when :server_maintenance
      [200, {}, ['status_message=Atlas is down for maintenance.']]
    else
      [999, {}, [""]]
    end
  end

  def wakeup_sleeping_threads
    @sleeping_threads.each &:wakeup
    @sleeping_threads.clear
  end
end

def go_disconnected_with_proxy disconnected_mode=:server_error
  if $proxy_server_disconnected
    puts 'going DISconnected'
    $current_proxy = NoResponseRack.new disconnected_mode
    rack_mappings.unshift([nil, "", /^(.*)/n, $current_proxy])

    $proxy_server_disconnected = false
  end
end

def rack_app
  Capybara::current_session.driver.app
end

def rack_mappings
  rack_app.instance_variable_get(:@mapping)
end
+1
source

About how I can think of allowing the host to be redefined in your tests and giving it a dummy host (something like localhost: 31337).

Perhaps take a look at http://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http and see if anything comes out.

0
source

Source: https://habr.com/ru/post/1543149/


All Articles