1) The easiest way I think to use find method with id of element or with xpath and click() function:
Here is an example:
feature 'Shall check devise authorization' do
scenario "shall authorize User with correct email and password" do
user = User.find(1)
visit new_user_session_path
fill_in "user[email]", with: user.email
fill_in "user[password]", with: user.password
# click_button "Sign in"
find('input#sign_in_button').click()
# find("//input[@id='sign_in_button']").click()
has_selector? 'input#created_at_first'
end
end
Here click_button "Sign in" the same as
find('input#sign_in_button').click()
[id of element]
and also the same as
find("//input[@id='sign_in_button']").click()
[xpath]
Choose whatever you want.
If you would like to choose element by its class and id, here is the solution:
find('input#sign_in_button.btn.btn-primary.btn-success').click()
find("//input[@class='btn btn-primary btn-success'][@id='sign_in_button']").click()
Also it can be helpful to inspect page capybara#finding. There are a lot of useful information.
2) Another option is to use capybara#scripting.
But it is a little harder, because you need to configure your Capybara driver for testing JS.
To configure your Capybara driver you should add gem 'capybara-webkit' in your Gemfile (you need to have libqtwebkit-dev).
sudo apt-get install libqtwebkit-dev
bundle install
Then place in your spec_helper.rb something like this:
Capybara.current_driver = :webkit
Capybara.run_server = true #Whether start server when testing
#Capybara.server_port = 3000
#Capybara.default_selector = :css #:xpath #default selector , you can change to :css
Capybara.default_wait_time = 5 #When we testing AJAX, we can set a default wait time
Capybara.ignore_hidden_elements = false #Ignore hidden elements when testing, make helpful when you hide or show elements using javascript
Capybara.javascript_driver = :webkit
and the following spec will work correctly:
# -*- encoding : utf-8 -*-
require_relative '../spec_helper'
feature 'Shall check devise authorization' do
scenario "shall authorize User with correct email and password", js: true do
user = User.find(1)
visit new_user_session_path
fill_in "user[email]", with: user.email
fill_in "user[password]", with: user.password
page.execute_script("$('#sign_in_button').click()")
has_selector? 'input#created_at_first'
end
end
The capybara-webkit driver is for true headless testing. It uses QtWebKit to start a rendering engine process. It can execute JavaScript as well. It is significantly faster than drivers like Selenium since it does not load an entire browser
For more details please visit test-javascript-with-capybara-webkit
UPDATE:
Also you can browse this spec from capybara source, which can help you to understand how you can find elements or add selectors: