Kingsley Ijomah
Kingsley Ijomah

Reputation: 3413

Rspec / Capybara clicking on button that was generated using javascript

I have a generated a button with javascript:

<script>
  var button = document.createElement("button");
  button.innerHTML = "Do Something";

  var body = document.getElementsByTagName("body")[0];
  body.appendChild(button);    
</script>

but can't seem to be able to find that button using Capybara e.g:

find_button 'Do Something' or click_button 'Do Something'

produces Unable to find button "Do Something"

has anyone come across anything like this before?

Upvotes: 1

Views: 664

Answers (1)

Kingsley Ijomah
Kingsley Ijomah

Reputation: 3413

Added to Gemfile and bundle install:

gem 'capybara-webkit'

Add to rails_helper:

Capybara.javascript_driver = :webkit

Enabled js on specific rspec scenario e.g:

scenario "dynamic button generated", js: true do
 ...
end

Upvotes: 1

Related Questions