📖 Scenario: You are building a simple blog application in Rails. You want to make sure that users can visit the homepage and see the welcome message correctly.
🎯 Goal: Create a system test using Capybara that visits the homepage and checks for the presence of the welcome text.
📋 What You'll Learn
Create a system test file for the homepage
Set up Capybara to visit the root path
Write a test that checks for the welcome message text
Run the test to verify it passes
💡 Why This Matters
🌍 Real World
System tests help ensure your web app works correctly from the user's point of view by simulating real browser actions.
💼 Career
Knowing how to write system tests with Capybara is valuable for Rails developers to maintain high-quality, reliable applications.
Progress0 / 4 steps
1
Create the system test file
Create a system test file called home_page_test.rb inside test/system/ folder with a class HomePageTest that inherits from ApplicationSystemTestCase.
Ruby on Rails
Hint
This file sets up the system test class where you will write your tests.
2
Add a test method to visit the homepage
Inside the HomePageTest class, add a test method called test_visiting_homepage that uses visit root_path to open the homepage.
Ruby on Rails
Hint
The visit method tells Capybara to open the page at the given path.
3
Check for the welcome message on the homepage
Inside the test_visiting_homepage method, add an assertion using assert_text "Welcome to the Blog!" to verify the welcome message is visible.
Ruby on Rails
Hint
The assert_text method checks if the given text appears anywhere on the page.
4
Run the system test
Run the system test by executing bin/rails test:system test/system/home_page_test.rb in your terminal to verify the test passes.
Ruby on Rails
Hint
Use the Rails command line to run system tests and see the results.
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?