Overview
Capybara is good for Rails E2E (Feature) testing. It simulates user actions on a browser, like find a button and click it, wait for the next page. You can choose Selenium Driver or others as web driver.
SetUp
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
stop/pause headless Selenium testing with Capybara
n350071🇯🇵 ・ Sep 10 '19 ・ 1 min read
#capybara
#rspec
#rails
Usage
🔨 prepare
👁 look around your site
inside of HTML
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
Narrow down scope with `within` in Capybara Rails testing
n350071🇯🇵 ・ Sep 18 '19 ・ 1 min read
#rails
browser manipulation
✅ assert
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
capybara find.text method ignore newline (convert to a space)
n350071🇯🇵 ・ Sep 10 '19 ・ 1 min read
#capybara
#rspec
#rails
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
have_selector is good for waiting test in Capybara
n350071🇯🇵 ・ Sep 18 '19 ・ 1 min read
#capybara
#rspec
#rails
# get current path
current_path
# spec/rails_helpers/feature_helper.rb
module FeatureHelpers
def current_path_with_params
uri = URI.parse(current_url)
"#{uri.path}?#{uri.query}"
end
end
📝 form
![n350071 image](https://res.cloudinary.com/practicaldev/image/fetch/s--pbzTu4Iv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--GyggfQgU--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/72353/94a227c6-1417-46e0-9fa1-2d48ffadc37e.png)
[Capybara] find('input').set will have not done!
n350071🇯🇵 ・ Dec 17 '19 ・ 1 min read
#capybara
#rspec
#rails
# input type=text
fill_in 'nav-search', with: 'n350071 capybara'
# select box
select 'Most Views', from: 'dashhboard_sort'
# submit
page.execute_script("$('form#your-form').submit()")
🧙♂️ Manipulate
# Change CSS/Style in Capybara
page.execute_script("$('selector').css('display','block')");
🎁 Opinion
- We should use what users will see, instead of the source code of HTML.
- The HTML is an essential factor to test.
- We should align the HTML tag, CSS for testing. If it's different from a similar page, the testing is tough.
- We should use the correct name in HTML to support easy testing.
- Using
find('label[for="model_attributes"]'
is better than XPath, because this way is simulate the users way.
Top comments (0)