Skip to content

xtiancordero/rspec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RSpec Test Automation Framework

A comprehensive test automation framework using Ruby, RSpec, Capybara, and Selenium.

Project Structure

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

Setup

1. Install Dependencies

bundle install

2. Configure Environment

Edit .env file with your settings:

BASE_URL=https://www.saucedemo.com
BROWSER=chrome
HEADLESS=false

Running Tests

Run All Tests

rspec

Run Specific Test File

rspec spec/features/login_spec.rb
rspec spec/sauce_labs.rb

Run Tests by Tag

rspec --tag smoke          # Run smoke tests
rspec --tag regression     # Run regression tests
rspec --tag ~slow          # Exclude slow tests

Run with Different Browsers

# Windows CMD
set BROWSER=chrome && rspec
set BROWSER=edge && rspec
set BROWSER=firefox && rspec

# PowerShell
$env:BROWSER="edge"; rspec

Run Headless

# Windows CMD
set HEADLESS=true && rspec

# PowerShell
$env:HEADLESS="true"; rspec

Key Concepts

Page Object Model (POM)

Pages 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')

Test Data Management

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" }

Helpers

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 failure

API Testing

response = api_get('/users')
expect_status(response, 200)
expect_field_value(response, :name, 'John')

Writing Tests

Basic Structure

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
end

Tagging Tests

it 'critical test', :smoke do
  # ...
end

it 'detailed test', :regression do
  # ...
end

it 'security test', :security do
  # ...
end

Debugging

Pause Test Execution

binding.pry  # Opens interactive console

Take Screenshot

page.save_screenshot('debug.png')

Print Page HTML

puts page.html

Keep Browser Open

Set in .env:

DETACH=true

Best Practices

  1. Use Page Objects - Encapsulate page logic
  2. Keep Tests Independent - Each test should stand alone
  3. Use Descriptive Names - Clear test descriptions
  4. Avoid Hardcoded Waits - Use explicit waits instead of sleep
  5. Use Tags - Organize tests with tags
  6. Data-Driven Testing - Use fixtures/factories for test data
  7. Screenshot on Failure - Automatically capture failures

Troubleshooting

Element Not Found

# Add explicit wait
expect(page).to have_css('.element', wait: 10)
find('.element', wait: 10).click

Stale Element

# Re-find element instead of storing reference
find('.element').click  # Good
# element = find('.element'); element.click  # Can go stale

Timing Issues in CI

# Increase wait time for CI
Capybara.default_max_wait_time = ENV['CI'] ? 15 : 5

About

This is a repository for learning and practicing automated testing using Rspec

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages