DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on • Edited on

My Capybara note

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

Usage

๐Ÿ”จ prepare

๐Ÿ‘ look around your site

inside of HTML

browser manipulation

โœ… assert





# 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 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ form

# 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()") 
Enter fullscreen mode Exit fullscreen mode

๐Ÿง™โ€โ™‚๏ธ Manipulate

# Change CSS/Style in Capybara page.execute_script("$('selector').css('display','block')"); 
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ 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)