Capybara is a web-based automation framework tool for testing Rails apps. It helps you to create that simulate how users would interact with your application through the browser. functional tests It is a fantastic tool for testing your application through the browser! doesn’t interact with the website, it’s just a layer between you and the web driver. For this, you can use web driver or any of the other drivers that Capybara supports. Capybara Selenium Configuring Capybara can easily integrate with common test frameworks used in Rails. It is mostly used together with for writing feature and, but less commonly, . Capybara RSpec controller test Before you get started, you need to add the following gems to the :test group in your and run : Gemfile bundle install group :test do gem 'capybara' gem 'selenium-webdriver'end Testing with Capybara Capybara comes with a user-friendly ( omain pecific anguage), which offers helper methods for extracting information, inputting data, testing, or clicking around. DSL D S L By default, Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements. You can read more about this at rubydoc.info . Common DSL options Here we will cover the most commonly used for interacting with elements in your web application: DSL actions specific elements and manipulate them: Find find_field('field_name').valuefind_field(id: 'field_id').valuefind_link('link_name', visible: all).visible? find_button('button_name').click This is one of the most important functions should know if you want to manipulate your elements. You can use it to find your links, buttons and fields and check if they are visible to the user, or click on them. will always wait for an element to appear on the page before raising an error. every good programmer Find Interact with the web application by : following links and buttons click_link('link_id')click_link('link_text')click_button('button_name')click_on('link_text') # clicks on either link or button Always use one of these options when you want to access a link or a button. You can do this by using id, title, text within tag or value of the element. Interact with : form elements fill_in('Title', :with => 'Example')choose('radio_button')check('checkbox') uncheck('checkbox')attach_file('image', 'image_path') # upload a fileselect('option', from: 'select_box') # select from dropdown Whenever you want to fill up your form you should use one of these possibilities. Depending on the type of the element the present values are: id, name or related label element. The only exeption is attach_file, it can only accept id and name. with: Navigate trough pages visit('url_name') You can easily navigate trough page with this simple command or only access the current page with: expect(page).to have_current_path(path_name) the page for the existence of certain elements: Query expect(page).to have_selector('table tr')expect(page).to have_content('content_name') Restrict certain actions within a specific area of the page with : scoping within(:xpath, 'actual_xpath') do fill_in 'Title', with: 'Example'end Use the method when you want to use actions only in a specific area or use these special methods: for a specific fieldset and for a specific table. within within_fieldset within_table with the following methods: Debug save_and_open_page # current snapshot of the page save_sceenshot('screenshot_name.png')save_and_open_screenshot # saves and automatically opens the screenshot When you want to save the snapshot of the page, just add one of these lines to your code, or you can also retrieve the current state of an element with: print page_name.html Matchers When trying to find an element by using the DSL or XPath, it is common to have two or more . You can customize the way Capybara find elements by using: matches — Finds exact matches of an element. Capybara.exact — Controls how Capybara behaves when multiple elements match. Capybara.match To avoid getting error when more than one match is found, you should consider using the following matching strategies supported by Capybara: Ambiguous match — This will simply pick the first element that matches. :first — This will raise an error if more than one element is found. :one — This finds all matching elements but only the first exactly matching element is returned and the rest is discarded. :prefer_exact — If is set to , it will behave like . Otherwise, it will try to find the exact element. Once again, an error is raised if multiple elements are found. If no element is found, a new search is performed. :smart Capybara.exact true :one JavaScript and Asynchronous Calls allows you to interact with an element on the page that isn’t present yet. This can happen when using JavaScript to creates a new element on the page: Asynchronous JavaScript click_link('test')click_link('foo') Once you click on the ‘test’ link it will trigger an asynchronous process, which will show the ‘foo’ link. Clicking on the ‘foo’ will most likely raise an error since that link doesn’t exist yet. Capybara can handle this by waiting for a brief period of time. You can adjust the default waiting period (the default is 2 seconds) with: . Capybara.default_max_wait_time Capybara and RSpec Using is as simple as adding in the file. To be able to do this, you will need beforehand. Capybara together with RSpec require 'capybara/rails' rspec_helper.rb RSpec installed describe 'users' do let! :each do @user = FactoryGirl.create(:user) login_as(@user) end describe GET user#edit' do it 'should update user' do visit edit_user_path(@user) within('#edit_user_path') do fill_in 'email', with: '[email protected]' end click_button 'Save' find_button('Save').click expect(page).to have_content 'Success' end endend This was a simple example that shows how you can use Capybara and RSpec at the same time by editing the user. Hope this article helped you understand what Capybara the rodent can do to your test environment! Subscribe and stay tuned for the upcoming articles! Originally published at kolosek.com on February 19, 2018.