Check Alert Message with Poltergeist or Capybara Webkit

June 28, 2016 poltergeist, Testing, capybara

Use accept_alert to get the alert message.

  • Poltergeist ignores alerts/confirms by default(source)
  • Poltergeist has had support for Capybaras modal api since September 2015( source)

That means you can same syntax for Poltergeist or capybara-webkit.

expect(accept_alert).to eq("Javascript alert message")

# or with block syntax

# will fail if alert message doesn't match
accept_alert("Javascript alert message") do 
  # next expectation

The first one expect(accept_alert).to reads a bit weird, but that's the return value of accept_alert.

Qihuan Piao

Qihuan Piao

Qihuan Piao(aka kinopyo) is Chinese based in Tokyo, Japan. Fulltime fullstack software writer using Rails. He shares coding tips, personal failures and random success in this blog. His infamous line - "I feel calm when I kill those monsters, or people (in game)" shocks his friends deeply.