Reputation: 105
Im new to capybara and poltergeist, but here's what I have so far.
It finds the button but fails to click on it. I've tried 'execute_script' but i cant get that to work either due to a NotSupportedByDriverError. I've included gem 'capybara-webkit' but that didn't change anything.
Line in question:
assert page.execute_script("$('.js-login-action').click()"), "Failed to click on Login"
Here's the full test.rb file
require 'test_helper'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
options = {js_errors: false, js: true}
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
class LoginTest < ActionDispatch::IntegrationTest
test "Load login page" do
visit("/users/sign_in")
assert page.has_content?('derp'), "Page does not have Derp heading"
end
test "Successful load of sign in page" do
get("/users/sign_in")
assert_response :success
end
test "Login" do
visit("/users/sign_in")
fill_in "login-username", with: 'derp'
fill_in "login-password", with: '0123456789'
assert find_button("Login"), "Failed to Find Login Button"
assert page.execute_script("$('.js-login-action').click()"), "Failed to click on Login"
assert page.has_css?('en-main-header'), "Failed to Login"
end
end
Upvotes: 0
Views: 3449
Reputation: 49890
You have a couple of things going wrong here:
You're setting up your javascript driver but then not telling your tests to use the javascript driver, so instead they are using the default driver which is rack-test and doesn't support javascript at all (hence the unsupported by driver error) You can either set Capybara.default_driver to :poltergeist if you want all tests to run with JS support, or look into setting the driver via metadata with something like https://github.com/wojtekmach/minitest-capybara
As @fabersky points out, you should be using click_button or Node#click to click on items in the page So - click_button('Login')
- which removes the need for you assertion on find_button since click_button will error if it can't find the button.
Asserting on find_button doesn't read very well, you'd be better off using something like page.assert_selector :button, 'Login'
which will provide a more descriptive error message on failure. Same goes for your assert has_css? which could be page.assert_selector(:css, 'en-main-header')
- the :css is optional if your default selector is set to :css - which would give a more descriptive error about what was looked for/missing
Asserting on the result of execute_script is not going to work since by definition execute_script doesn't return anything. If you really want to check the result of some JS being executed on the page you want to use evaluate_script
Upvotes: 0
Reputation: 2444
The page.execute_script
is not really meant to click on a button.
As you can see in this very helpful capybara cheatsheet, you can click on a button in different ways. For example:
click_button('Save')
or
find_button('Send').click
or
find('//table/tr').click
Upvotes: 2