Skip to content
Browse files

git-svn-id: http://svn.viney.net.nz/things/rails/plugins/validates_da…

…te@2 20afb1e0-9c0e-0410-9884-91ed27886737
  • Loading branch information...
0 parents commit 74edf8f8e8b67ee6458428dbd60c3cf09fb511c6 jonathan committed
Showing with 240 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +150 −0 README
  3. +14 −0 app/models/person.rb
  4. +4 −0 init.rb
  5. +41 −0 lib/validates_date.rb
  6. +11 −0 test/unit/person_test.rb
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Jonathan Viney
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
150 README
@@ -0,0 +1,150 @@
+= selenium_testing =
+
+This plugin extends the rails testing framework to allow you to write
+Selenium tests in the same way as unit and functional tests.
+
+
+Installation
+--
+
+Plugin:
+ script/plugin install http://svn.viney.net.nz/things/rails/plugins/selenium_testing
+
+Selenium:
+ rake install_selenium (or manually unpack Selenium into vendor/selenium)
+
+Generate a test stub:
+ script/generate selenium_test person
+ # Add your tests into test/selenium/person_test.rb, see below for details
+
+Start server with test environment:
+ # The plugin will only load in the test environment
+ script/server -e test
+
+Open your browser and run the tests:
+ http://localhost:3000/selenium
+
+
+Writing Tests
+--
+
+Tests are written in the same way as unit and functional tests. The method calls
+in the test (click, type, assert_visible etc... ) correspond to
+Selenium commands/assertions. See the Selenium documentation for a full
+listing of available commands/assertions. You can NOT use Test::Unit
+assertions (assert, assert_equal etc...), you must use Selenium assertions.
+
+ class PersonSeleniumTest < Test::Unit::TestCase
+ fixtures :people, :households
+
+ def setup
+ open :controller => 'person', :action => 'view', :id => 1
+ end
+
+ def test_add_new_person
+ click 'add_new_person_link'
+ assert_visible 'add_new_person'
+ type 'new_person_first_name', 'Pixel'
+ type 'new_person_last_name', 'the Cat'
+ select 'new_person_gender', 'Male'
+ click_and_wait 'submit_add_new_person'
+ assert_text 'person_name', 'Pixel the Cat'
+ end
+
+ def test_household
+ people(:jonathan).householders.each do |householder|
+ assert_visible "householder_#{householder.id}"
+ end
+ end
+ end
+
+
+Using Selenium IDE (Requires Firefox 1.5)
+--
+
+Selenium IDE (http://www.openqa.org/selenium-ide/) gives you a way of
+recording tests by simply using your application.
+Once you have installed the Firefox extension, select Tools -> Selenium IDE.
+Put Selenium IDE into Ruby mode by selecting Options -> Format -> Ruby.
+Selenium IDE will automatically be in record mode, so start clicking on
+things and using your application. You will see Selenium commands being generated.
+Once you are finished, flip to the Source tab and you will see a class definition
+with a single method. Just copy the method into your Selenium test class. Done!
+
+
+Test Suites
+--
+
+You can group tests into test suites so certain tests can easily be run together.
+To do this, add the file test/selenium/suites.rb. You can add entire TestCases into
+a suite, or select individual tests.
+
+suites.rb:
+
+ SeleniumTestSuite.new 'My test suite',
+ PersonSeleniumTest, # Run all the tests in this class
+ HouseholdSeleniumTest.new(:test_add_household) # Select a single test
+
+ # Define as many test suites as you like
+
+Once you have defined one or more test suites, you will see a select box at the top
+of the upper left frame in the browser. Just select the test suite you want to run
+and you're good to go.
+
+
+Continuous Integration
+--
+
+To automatically run all your Selenium tests, use the test_selenium rake task.
+You can define which browsers are used in config/environment.rb. Default paths are used
+unless you specify others.
+
+ SeleniumConfig.set do |c|
+ c.browser :ie
+ c.browser :firefox, 'C:\Program Files\Mozilla Firefox\firefox.exe' # Path is optional
+ end
+
+
+Misc
+--
+
+You can define the environment(s) that the plugin will load in using SeleniumConfig
+in config/environment.rb, eg:
+
+ SeleniumConfig.set do |c|
+ c.environments :test, :development
+ end
+
+
+User extensions
+--
+
+Selenium supports having a user-extensions.js file with custom commands and asertions.
+If you wish to use this feature, place your user-extensions.js file in test/selenium
+along with your test files.
+
+
+Subversion
+--
+
+http://svn.viney.net.nz/things/rails/plugins/selenium_testing
+
+
+Todo
+--
+
+ - Improve the continuous integration testing, probably using a driven
+ approach with the Selenium/Ruby driver. This will allow errors to be reported for tests,
+ as well as giving much more control over the entire procedure.
+ - More documentation/examples
+ - Tests!!
+
+
+Credits
+--
+Jonas Bengtsson http://andthennothing.net/archives/2006/02/05/selenium-on-rails
+
+
+Help
+--
+For more help and/or other questions, feel free to email me at jonathan@bluewire.net.nz
14 app/models/person.rb
@@ -0,0 +1,14 @@
+class Person < ActiveRecord::Base
+ def self.columns()
+ @columns ||= []
+ end
+
+ def self.column(name, sql_type = nil, default = nil, null = true)
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ end
+
+ column :name, :date_of_birth, :date_of_death
+
+ validates_presence_of :name
+ validates_date :date_of_birth
+end
4 init.rb
@@ -0,0 +1,4 @@
+if SeleniumConfig[:environments].map { |e| e.to_s }.include?(RAILS_ENV)
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/lib/controllers')
+ require File.dirname(__FILE__) + '/routes.rb'
+end
41 lib/validates_date.rb
@@ -0,0 +1,41 @@
+class ActiveRecord::Base
+ def self.validates_date(*attr_names)
+ configuration = { :message => 'is an invalid date', :on => :save }
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
+
+ validates_each(attr_names, configuration) do |record, attr_name, value|
+ if date = string_to_date(value)
+ record.send("#{attr_name}=", date)
+ else
+ record.errors.add(attr_name, configuration[:message])
+ end
+ end
+ end
+
+ private
+ def self.string_to_date(string)
+ return string unless string.is_a?(String)
+
+ string.strip!
+
+ unless string =~ /^\d{4}-\d{2}-\d{2}/ # Skip if already in 2006-01-01 format
+ # 22/1/06
+ if string =~ /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/
+ day, month, year = $1, $2, $3
+ end
+
+ # 22 Feb 06
+ if string =~ /^(\d{1,2}) (\w{3}) (\d{2,4})$/
+ day, month, year = $1, $2, $3
+ month = Date::ABBR_MONTHNAMES.index(month.capitalize)
+ end
+
+ year = year.to_i < 20 ? "20#{year}" : "19#{year}" if year.length == 2
+ string = "#{year}-#{month}-#{day}"
+ end
+
+ date_array = ParseDate.parsedate(string)
+ # treat 0000-00-00 as nil
+ Date.new(date_array[0], date_array[1], date_array[2]) rescue nil
+ end
+end
11 test/unit/person_test.rb
@@ -0,0 +1,11 @@
+
+
+class PersonTest < Test::Unit::TestCase
+ def test_no_date_checking
+ p = Person.new(:name => 'Jonathan')
+ assert p.valid?
+
+ p.date_of_birth = p.date_of_death = nil
+ assert p.valid?
+ end
+end

0 comments on commit 74edf8f

Please sign in to comment.
Something went wrong with that request. Please try again.