Bird
Raised Fist0
Ruby on Railsframework~10 mins

System tests with Capybara in Ruby on Rails - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - System tests with Capybara
Start Test Suite
↓
Setup Test Environment
↓
Launch Browser Simulator
↓
Visit Web Page
↓
Find Elements & Interact
↓
Check Expected Outcomes
↓
Pass or Fail Test
↓
Close Browser Simulator
↓
End Test Suite
This flow shows how a system test with Capybara runs: it sets up, opens a simulated browser, visits pages, interacts with elements, checks results, and finishes.
Execution Sample
Ruby on Rails
require "application_system_test_case"

class HomePageTest < ApplicationSystemTestCase
  test "visiting the home page" do
    visit "/"
    assert_selector "h1", text: "Welcome"
  end
end
This test opens the home page and checks if an <h1> with 'Welcome' text is present.
Execution Table
StepActionEvaluationResult
1Start test 'visiting the home page'N/ATest begins
2Setup test environmentN/ATest environment ready
3Launch browser simulatorN/ABrowser simulator opened
4Visit '/' pageN/AHome page loaded
5Find <h1> element with text 'Welcome'Element found?Yes, element found
6Assert element text equals 'Welcome'Text matches?Yes, assertion passes
7Test passesN/ATest marked as passed
8Close browser simulatorN/ABrowser closed
9End testN/ATest suite ends
💡 Test ends after assertion passes and browser simulator closes
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
page_urlnil"/""/""/""/"
element_foundfalsefalsetruetruetrue
assertion_passedfalsefalsefalsetruetrue
Key Moments - 3 Insights
Why do we need to launch a browser simulator in system tests?
Because Capybara simulates a real user interacting with the app through a browser, so launching a browser simulator lets the test behave like a real user. See execution_table step 3.
What happens if the <h1> element with text 'Welcome' is not found?
The assertion fails at step 6, causing the test to fail and stop. The test won't reach step 7 or beyond.
Why do we close the browser simulator at the end?
To clean up resources and avoid leftover browser processes. This happens at step 8 to keep tests isolated and efficient.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'element_found' after step 5?
Atrue
Bfalse
Cnil
Dundefined
💡 Hint
Check variable_tracker row for 'element_found' at 'After Step 5'
At which step does the test confirm the page has loaded?
AStep 2
BStep 4
CStep 6
DStep 8
💡 Hint
Look at execution_table action descriptions for page loading
If the assertion fails, which step would the test NOT reach?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
See execution_table steps after assertion passes
Concept Snapshot
System tests with Capybara simulate user actions in a browser.
Use 'visit' to open pages and 'assert_selector' to check elements.
Tests run in a browser simulator for realistic interaction.
Close the simulator after tests to clean up.
Assertions decide if tests pass or fail.
Full Transcript
System tests with Capybara run by starting a test, setting up the environment, and launching a browser simulator. The test visits a web page, finds elements like headers, and checks if they have expected text. If the checks pass, the test passes. The browser simulator closes after the test to keep things clean. This process helps test the app like a real user would, ensuring the interface works correctly.

Practice

(1/5)
1. What is the main purpose of system tests with Capybara in a Rails application?
easy
A. To test individual Ruby methods for correctness
B. To measure server response time under load
C. To check database schema migrations
D. To simulate user actions in a real browser and check app behavior

Solution

  1. Step 1: Understand system tests role

    System tests simulate how a real user interacts with the app through a browser.
  2. Step 2: Compare with other test types

    Unlike unit tests or performance tests, system tests focus on full app behavior with UI.
  3. Final Answer:

    To simulate user actions in a real browser and check app behavior -> Option D
  4. Quick Check:

    System tests = simulate user in browser [OK]
Hint: System tests mimic real user clicks and typing [OK]
Common Mistakes:
  • Confusing system tests with unit tests
  • Thinking system tests check only backend code
  • Assuming system tests measure performance
2. Which of the following is the correct way to visit the homepage in a Capybara system test?
easy
A. navigate_to root_url
B. go_to '/'
C. visit root_path
D. open_page '/'

Solution

  1. Step 1: Recall Capybara visit syntax

    Capybara uses the method visit with a path or URL to open a page.
  2. Step 2: Match correct method and argument

    root_path is the Rails helper for homepage path, so visit root_path is correct.
  3. Final Answer:

    visit root_path -> Option C
  4. Quick Check:

    Capybara visit + path helper = visit root_path [OK]
Hint: Use 'visit' with Rails path helpers to open pages [OK]
Common Mistakes:
  • Using non-existent methods like go_to or open_page
  • Confusing root_path with root_url in system tests
  • Forgetting to use visit before path
3. Given this system test code snippet, what will be the output on the page?
visit root_path
fill_in 'Email', with: 'user@example.com'
fill_in 'Password', with: 'secret'
click_button 'Log in'
expect(page).to have_content('Welcome, user@example.com')
medium
A. Test passes if 'Welcome, user@example.com' appears after login
B. Test fails because 'fill_in' cannot find fields
C. Test raises error due to missing 'visit' method
D. Test passes without checking page content

Solution

  1. Step 1: Analyze test steps

    The test visits the homepage, fills in login form fields, clicks login, then checks for welcome message.
  2. Step 2: Understand Capybara behavior

    If the login works and page shows the welcome text, have_content will pass and test passes.
  3. Final Answer:

    Test passes if 'Welcome, user@example.com' appears after login -> Option A
  4. Quick Check:

    fill_in + click_button + have_content = success check [OK]
Hint: Check page content after actions to confirm success [OK]
Common Mistakes:
  • Assuming fill_in fails without checking form existence
  • Forgetting to call visit before actions
  • Ignoring the expect(page).to have_content assertion
4. You wrote this system test but it fails with "Capybara::ElementNotFound" error:
visit root_path
fill_in 'Username', with: 'testuser'
click_button 'Submit'

What is the most likely cause?
medium
A. Capybara does not support fill_in method
B. The form does not have a field labeled 'Username'
C. The button 'Submit' is missing from the page
D. You forgot to call visit before fill_in

Solution

  1. Step 1: Understand Capybara::ElementNotFound meaning

    This error means Capybara could not find the form field or button with given label.
  2. Step 2: Check test code and error cause

    Since visit is called, the likely cause is the field labeled 'Username' does not exist on the page.
  3. Final Answer:

    The form does not have a field labeled 'Username' -> Option B
  4. Quick Check:

    ElementNotFound = missing field or button [OK]
Hint: Check labels exactly match form fields and buttons [OK]
Common Mistakes:
  • Assuming visit was not called when it was
  • Thinking Capybara lacks fill_in support
  • Ignoring exact label spelling and case
5. You want to write a system test that logs in a user, visits their profile page, and verifies their username is shown. Which sequence correctly uses Capybara commands?
hard
A. visit login_path; fill_in 'Email', with: user.email; fill_in 'Password', with: user.password; click_button 'Log in'; visit profile_path(user); expect(page).to have_content(user.username)
B. fill_in 'Email', with: user.email; click_button 'Log in'; visit profile_path(user); expect(page).to have_content(user.username)
C. visit profile_path(user); fill_in 'Email', with: user.email; click_button 'Log in'; expect(page).to have_content(user.username)
D. visit login_path; click_button 'Log in'; fill_in 'Email', with: user.email; fill_in 'Password', with: user.password; expect(page).to have_content(user.username)

Solution

  1. Step 1: Identify correct login flow

    You must visit the login page first, then fill in email and password, then click login.
  2. Step 2: Visit profile after login and check content

    After successful login, visit the profile page and check the username is displayed.
  3. Final Answer:

    visit login_path; fill_in 'Email', with: user.email; fill_in 'Password', with: user.password; click_button 'Log in'; visit profile_path(user); expect(page).to have_content(user.username) -> Option A
  4. Quick Check:

    Login then visit profile then check content [OK]
Hint: Always visit pages before interacting or checking content [OK]
Common Mistakes:
  • Trying to fill_in before visiting login page
  • Visiting profile before logging in
  • Clicking buttons before filling required fields
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard