0
0
Ruby on Railsframework~10 mins

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

Choose your learning style9 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.