System tests check how your whole app works from a user's view. Capybara helps you write these tests by simulating clicks, typing, and page visits.
You want to make sure a user can log in successfully.
You want to test if a form submits and shows a success message.
You want to check navigation between pages works as expected.
You want to verify that buttons and links behave correctly.
You want to catch bugs that happen only when the app runs fully.
Syntax
Ruby on Rails
require "application_system_test_case"class ExampleTest < ApplicationSystemTestCase
test "user can visit homepage" do
visit root_path
assert_selector "h1", text: "Welcome"
end
end
Use visit to open a page by its path or URL.
Use assert_selector to check if an element with specific text is on the page.
Examples
This example shows how to visit the login page, fill in fields, and click a button.
This system test simulates a user visiting the login page, entering their email and password, clicking the login button, and then checking if a welcome message appears.
Ruby on Rails
require "application_system_test_case"class LoginTest < ApplicationSystemTestCase
test "user logs in successfully" do
visit login_path
fill_in "Email", with: "test@example.com"
fill_in "Password", with: "password123"
click_button "Log in"
assert_text "Welcome, test@example.com"
end
end
OutputSuccess
Important Notes
System tests run slower than unit tests because they open a browser and simulate real user actions.
Use descriptive test names to explain what the test checks.
Capybara automatically waits for elements to appear, so you rarely need manual delays.
Summary
System tests check your app like a real user would.
Capybara helps write these tests with simple commands like visit, fill_in, and click_button.
Use assertions like assert_text to confirm expected page content.
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
Step 1: Understand system tests role
System tests simulate how a real user interacts with the app through a browser.
Step 2: Compare with other test types
Unlike unit tests or performance tests, system tests focus on full app behavior with UI.
Final Answer:
To simulate user actions in a real browser and check app behavior -> Option D
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
Step 1: Recall Capybara visit syntax
Capybara uses the method visit with a path or URL to open a page.
Step 2: Match correct method and argument
root_path is the Rails helper for homepage path, so visit root_path is correct.
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
Step 1: Understand Capybara::ElementNotFound meaning
This error means Capybara could not find the form field or button with given label.
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.
Final Answer:
The form does not have a field labeled 'Username' -> Option B
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?