Added Rspec and Webrat plugins and started porting Selenium on Rails tests to Rspec Plain Text Stories driving Webrat driving Selenium.

This commit is contained in:
Luke Melia 2008-06-18 02:57:57 -04:00 committed by Simon Rozet
parent 7b432a74ed
commit 2c09db45c5
602 changed files with 47788 additions and 29 deletions

115
stories/helper.rb Normal file
View file

@ -0,0 +1,115 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
$:.unshift File.join(File.dirname(__FILE__), *%w[.. vendor plugings rspec lib])
require 'test_help'
require 'test/unit/testresult'
require 'spec'
require 'spec/rails'
require 'spec/story'
require 'webrat/selenium'
module Spec
module Story
class StepGroup
def include_steps_for(name)
require File.expand_path(File.dirname(__FILE__) + "/steps/#{name}")
step_matchers = rspec_story_steps[name.to_sym]
warn "WARNING: 0 step matchers found for include_steps_for(:#{name}). Are you missing an include?" if step_matchers.empty?
self << step_matchers
end
end
end
end
Test::Unit.run = true
class SeleniumRailsStory < Test::Unit::TestCase
include Spec::Matchers
include Spec::Rails::Matchers
def initialize #:nodoc:
# TODO - eliminate this hack, which is here to stop
# Rails Stories from dumping the example summary.
Spec::Runner::Options.class_eval do
def examples_should_be_run?
false
end
end
@_result = Test::Unit::TestResult.new
end
def should_see(text_or_regexp)
if text_or_regexp.is_a?(Regexp)
response.should have_tag("*", text_or_regexp)
else
response.should have_tag("*", /#{Regexp.escape(text_or_regexp)}/i)
end
end
def response
webrat_session.response_body
end
def method_missing(name, *args)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args)
else
super
end
end
protected
def webrat_session
@webrat_session ||= begin
Webrat::SeleniumSession.new(SeleniumDriverManager.instance.running_selenium_driver)
end
end
end
class DatabaseResetListener
include Singleton
def scenario_started(*args)
if defined?(ActiveRecord::Base)
connection = ActiveRecord::Base.connection
%w[users].each do |table|
connection.execute "DELETE FROM #{table}"
end
end
end
def scenario_succeeded(*args)
end
alias :scenario_pending :scenario_succeeded
alias :scenario_failed :scenario_succeeded
end
class CookieResetListener
include Singleton
def scenario_started(*args)
%w[tracks_login auth_token _session_id].each do |cookie_name|
SeleniumDriverManager.instance.running_selenium_driver.get_eval("window.document.cookie = '#{cookie_name}=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';")
end
end
def scenario_succeeded(*args)
end
alias :scenario_pending :scenario_succeeded
alias :scenario_failed :scenario_succeeded
end
class Spec::Story::Runner::ScenarioRunner
def initialize
@listeners = [DatabaseResetListener.instance, CookieResetListener.instance]
end
end
class Spec::Story::GivenScenario
def perform(instance, name = nil)
scenario = Spec::Story::Runner::StoryRunner.scenario_from_current_story @name
runner = Spec::Story::Runner::ScenarioRunner.new
runner.instance_variable_set(:@listeners,[])
runner.run(scenario, instance)
end
end

View file

@ -0,0 +1,15 @@
Story: First run shows admin signup
As a user who just installed Tracks
I want to create an admin account
So that I have control over all preferences and users
Scenario: Successful signup
Given no users exist
And a visitor named Reinier
When Reinier visits the site
Then he should see a signup form
When Reinier successfully submits the signup form
Then Reinier should see the tasks listing page
And Reinier should be an admin

View file

@ -0,0 +1,22 @@
Story: Existing user logging in
As an existing user
I want to log in with my username and password
So that I can securely get things done
Scenario: Login success
Given an admin user Reinier with the password abracadabra
And Reinier is not logged in
When Reinier visits the login page
And Reinier successfully submits the login form
Then Reinier should see the tasks listing page
And Reinier should see the message Login successful
Scenario: Login failure
Given an admin user Reinier with the password abracadabra
And Reinier is not logged in
When Reinier visits the login page
And Reinier submits the login form with an incorrect password
Then Reinier should see the login page again
And Reinier should see the message Login unsuccessful

68
stories/steps/login.rb Normal file
View file

@ -0,0 +1,68 @@
steps_for :login do
Given "an admin user Reinier with the password abracadabra" do
@reinier = User.create!(:login => 'reinier', :password => 'abracadabra', :password_confirmation => 'abracadabra', :is_admin => true)
@reinier.create_preference
end
Given "Reinier is not logged in" do
end
Given "no users exist" do
User.delete_all
end
Given "a visitor named Reinier" do
end
When "Reinier submits the login form with an incorrect password" do
fills_in 'Login', :with => 'reinier'
fills_in 'Password', :with => 'incorrectpass'
clicks_button
end
When "Reinier visits the login page" do
visits '/login'
end
When "Reinier successfully submits the login form" do
fills_in 'Login', :with => 'reinier'
fills_in 'Password', :with => 'abracadabra'
clicks_button
end
When "Reinier visits the site" do
visits '/'
end
When "Reinier successfully submits the signup form" do
fills_in 'Desired login', :with => 'reinier'
fills_in 'Choose password', :with => 'abracadabra'
fills_in 'Confirm password', :with => 'abracadabra'
clicks_button
end
Then "he should see a signup form" do
should_see 'create an admin account'
end
Then "Reinier should see the tasks listing page" do
response.should have_tag('title', /list tasks/i)
end
Then "Reinier should be an admin" do
response.should have_tag('a', /Admin/i)
end
Then "Reinier should see the message Login successful" do
should_see 'Login successful'
end
Then "Reinier should see the login page again" do
response.should have_tag('title', /login/i)
end
Then "Reinier should see the message Login unsuccessful" do
should_see 'Login unsuccessful'
end
end