A comprehensive test automation framework using Ruby, RSpec, Capybara, and Selenium.
rspec/
├── Gemfile # Dependencies
├── .env # Environment variables
├── .rspec # RSpec configuration
├── lib/ # Application code (if any)
├── reports/ # Test reports and screenshots
│ └── screenshots/ # Failure screenshots
└── spec/
├── spec_helper.rb # Main RSpec configuration
├── support/ # Support files and helpers
│ ├── capybara_config.rb # Browser driver configuration
│ ├── helpers.rb # Reusable helper methods
│ └── api_helper.rb # API testing helpers
├── pages/ # Page Object Model classes
│ ├── base_page.rb # Base class for all pages
│ ├── login_page.rb # Login page object
│ ├── inventory_page.rb # Inventory/Products page
│ ├── cart_page.rb # Shopping cart page
│ └── checkout_page.rb # Checkout pages
├── fixtures/ # Test data (YAML)
│ ├── users.yml # User credentials
│ └── products.yml # Product data
├── factories/ # Data factories
│ └── user_factory.rb # Generate random user data
├── features/ # UI/Feature tests
│ ├── login_spec.rb # Login tests
│ ├── shopping_cart_spec.rb# Cart tests
│ ├── checkout_spec.rb # Checkout tests
│ └── sorting_spec.rb # Product sorting tests
├── api/ # API tests
│ ├── users_api_spec.rb # Users API tests
│ └── posts_api_spec.rb # Posts API tests
└── sauce_labs.rb # Your original test file
bundle installEdit .env file with your settings:
BASE_URL=https://www.saucedemo.com
BROWSER=chrome
HEADLESS=falserspecrspec spec/features/login_spec.rb
rspec spec/sauce_labs.rbrspec --tag smoke # Run smoke tests
rspec --tag regression # Run regression tests
rspec --tag ~slow # Exclude slow tests# Windows CMD
set BROWSER=chrome && rspec
set BROWSER=edge && rspec
set BROWSER=firefox && rspec
# PowerShell
$env:BROWSER="edge"; rspec# Windows CMD
set HEADLESS=true && rspec
# PowerShell
$env:HEADLESS="true"; rspecPages are represented as classes with:
- Selectors: Element locators as constants
- Actions: Methods that interact with elements
- Assertions: Methods that verify state
# Usage
login_page = LoginPage.new
login_page.visit_page
login_page.login('username', 'password')YAML Fixtures - Static test data:
test_data = YAML.load_file('spec/fixtures/users.yml')
user = test_data['valid_users']['standard_user']Factories - Dynamic/random data:
checkout_info = UserFactory.checkout_info
# => { first_name: "John", last_name: "Doe", postal_code: "12345" }Wait Helpers:
wait_for_element('.selector')
wait_for_text('Loading complete')
wait_for_url(/dashboard/)Form Helpers:
fill_form({ 'username' => 'user', 'password' => 'pass' })
slow_type('field', 'text', delay: 0.1)Screenshot Helpers:
take_screenshot('step_1')
# Automatic screenshots on failureresponse = api_get('/users')
expect_status(response, 200)
expect_field_value(response, :name, 'John')RSpec.describe 'Feature Name', type: :feature do
let(:page_object) { PageClass.new }
before do
# Setup - runs before each test
end
describe 'Scenario Group' do
context 'specific condition' do
it 'does something' do
# Test steps
expect(result).to eq(expected)
end
end
end
endit 'critical test', :smoke do
# ...
end
it 'detailed test', :regression do
# ...
end
it 'security test', :security do
# ...
endbinding.pry # Opens interactive consolepage.save_screenshot('debug.png')puts page.htmlSet in .env:
DETACH=true
- Use Page Objects - Encapsulate page logic
- Keep Tests Independent - Each test should stand alone
- Use Descriptive Names - Clear test descriptions
- Avoid Hardcoded Waits - Use explicit waits instead of
sleep - Use Tags - Organize tests with tags
- Data-Driven Testing - Use fixtures/factories for test data
- Screenshot on Failure - Automatically capture failures
# Add explicit wait
expect(page).to have_css('.element', wait: 10)
find('.element', wait: 10).click# Re-find element instead of storing reference
find('.element').click # Good
# element = find('.element'); element.click # Can go stale# Increase wait time for CI
Capybara.default_max_wait_time = ENV['CI'] ? 15 : 5