Warm tip: This article is reproduced from stackoverflow.com, please click
cucumber google-chrome-headless poltergeist

Poltergeist JS/Headless Chrome

发布于 2020-03-27 10:21:04

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, infact 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. Anyone done or do this in their test setup?

Questioner
Richlewis
Viewed
36
Thomas Walpole 2019-07-04 00:50

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.