Search code examples
cucumbergoogle-chrome-headlesspoltergeist

Poltergeist JS/Headless Chrome - Switch to offline mode


I am looking to write a test where I can switch between Offline mode and back to Online mode mid way through a Cucumber test. I can manually achieve this via Dev Tools in Chrome but is there a way to automate this using Poltergeist JS or Headless Chrome?

I know that page.driver is accessible, in fact I use this for setting cookie values in another test

Given(/^I set the "([^"]*)" cookie value to "([^"]*)" for the domain "([^"]*)"$/) do |cookieName,cookieValue,cookieDomain|
  if "#{DRIVER}" == "headless_chrome"
    page.driver.browser.manage.add_cookie name: cookieName, value: cookieValue, domain: cookieDomain
  else
    page.driver.set_cookie(cookieName, cookieValue, {:domain => cookieDomain})
  end
  sleep 1
end

Unless I'm missing something I can't see how to switch between Offline and Online modes. How can I do this in my test setup?


Solution

  • When using Selenium with Chrome as the driver you can use network_conditions=

    page.driver.browser.network_conditions = { offline: true }
    

    I don't believe Poltergeist had similar functionality.