commit 8435d19a3abfb559f25c94928bd810aec20592c9 Author: Jonas Nicklas and Kevin Fitzpatrick Date: Wed Nov 4 23:00:05 2009 +0100 Initial checkin diff --git a/History.txt b/History.txt new file mode 100644 index 00000000..01496bb3 --- /dev/null +++ b/History.txt @@ -0,0 +1,4 @@ +=== 0.0.1 2009-11-04 + +* 1 major enhancement: + * Initial release diff --git a/Manifest.txt b/Manifest.txt new file mode 100644 index 00000000..24be9fe6 --- /dev/null +++ b/Manifest.txt @@ -0,0 +1,19 @@ +History.txt +Manifest.txt +README.rdoc +Rakefile +examples/webcat.rb +lib/webcat.rb +lib/webcat/culerity_driver.rb +lib/webcat/rack_test_driver.rb +lib/webcat/server.rb +script/console +script/destroy +script/generate +spec/culerity_driver_spec.rb +spec/drivers_spec.rb +spec/rack_test_driver_spec.rb +spec/spec_helper.rb +spec/webcat_spec.rb +test/test_helper.rb +test/test_webcat.rb diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 00000000..cd3f6815 --- /dev/null +++ b/README.rdoc @@ -0,0 +1,49 @@ += webcat + +* http://github.com/jnicklas/webcat + +== DESCRIPTION: + +Webcat is a fledgeling replacement for webrat which aims to work with all +browser simulators as well as rack-test. + +== FEATURES/PROBLEMS: + +* FIX (list of features or problems) + +== SYNOPSIS: + + FIX (code sample of usage) + +== REQUIREMENTS: + +* FIX (list of requirements) + +== INSTALL: + +* FIX (sudo gem install, anything else) + +== LICENSE: + +(The MIT License) + +Copyright (c) 2009 Jonas Nicklas + +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. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..65a5990e --- /dev/null +++ b/Rakefile @@ -0,0 +1,26 @@ +require 'rubygems' +gem 'hoe', '>= 2.1.0' +require 'hoe' +require 'fileutils' +require './lib/webcat' + +Hoe.plugin :newgem +# Hoe.plugin :website +# Hoe.plugin :cucumberfeatures + +# Generate all the Rake tasks +# Run 'rake -T' to see list of generated tasks (from gem root directory) +$hoe = Hoe.spec 'webcat' do + self.developer 'Jonas Nicklas', 'jonas.nicklas@gmail.com' + self.rubyforge_name = self.name # TODO this is default value + + self.extra_deps = [['activesupport','>= 2.0.2']] + +end + +require 'newgem/tasks' +Dir['tasks/**/*.rake'].each { |t| load t } + +# TODO - want other tests/tasks run by default? Add them to the list +# remove_task :default +# task :default => [:spec, :features] diff --git a/examples/webcat.rb b/examples/webcat.rb new file mode 100644 index 00000000..7a3ab367 --- /dev/null +++ b/examples/webcat.rb @@ -0,0 +1,36 @@ +session = Webcat::Session.new('http://localhost:3000') + +session.visit '/' + +session.driver.trigger :click, '//div[@id=foo]//a' +session.driver.trigger :mouseover, '#foo a.bar' # will be ignored by drivers who do not support it + +nodelist = session.find 'li#foo a' +nodelist.empty? +nodelist.first.tag_name # => 'a' +nodelist.first.text # => 'a cute link' +nodelist.first.html # => 'a cute link' +nodelist.first.attributes # => { :href => '/blah' } +nodelist.first.trigger :click + +session.request.url # => '/blah' +session.response.ok? # => true + +# fancy stuff, just builds on the stuff above! + +session.click_link 'a cute link' +session.click_button 'an awesome button' +session.within '#foo' do + click_link 'a cute link' +end +session.fill_in 'foo', :with => 'bar' +session.choose 'Monkey' +session.check 'I am awesome' +session.wait_for '#fooo" + +# In cuke: + +When 'I am awesome' do + page.check 'I am awesome' + page.click_button 'FooBar' +end diff --git a/lib/webcat.rb b/lib/webcat.rb new file mode 100644 index 00000000..b2821c97 --- /dev/null +++ b/lib/webcat.rb @@ -0,0 +1,37 @@ +module Webcat + class << self + attr_accessor :debug + + def log(message) + puts message if debug + end + end + + class Session + attr_reader :mode, :app + + def initialize(mode, app) + @mode = mode + @app = app + end + + def driver + @driver ||= Webcat::Driver::RackTest.new(app) + end + + def get(path) + driver.get(path) + end + + def body + driver.response.body + end + end + + autoload :Server, 'webcat/server' + + module Driver + autoload :RackTest, 'webcat/rack_test_driver' + autoload :Culerity, 'webcat/culerity_driver' + end +end \ No newline at end of file diff --git a/lib/webcat/culerity_driver.rb b/lib/webcat/culerity_driver.rb new file mode 100644 index 00000000..8050f9d3 --- /dev/null +++ b/lib/webcat/culerity_driver.rb @@ -0,0 +1,50 @@ +require 'culerity' +require 'rack' +require 'net/http' + +class Webcat::Driver::Culerity + Response = Struct.new(:body) + + attr_reader :app, :rack_server + + def initialize(app) + @app = app + @rack_server = Webcat::Server.new(@app) + @rack_server.boot + end + + def get(path) + browser.goto(url(path)) + end + + def body + browser.text + end + +private + + def url(path) + rack_server.url(path) + end + + def server + unless @_server + @_server = ::Culerity::run_server + at_exit do + @_server.close + end + end + @_server + end + + def browser + unless @_browser + @_browser = ::Culerity::RemoteBrowserProxy.new server, {:browser => :firefox, :log_level => :off} + at_exit do + @_browser.exit + end + end + @_browser + end + +end \ No newline at end of file diff --git a/lib/webcat/rack_test_driver.rb b/lib/webcat/rack_test_driver.rb new file mode 100644 index 00000000..76faedd1 --- /dev/null +++ b/lib/webcat/rack_test_driver.rb @@ -0,0 +1,17 @@ +require 'rack/test' + +class Webcat::Driver::RackTest + include ::Rack::Test::Methods + attr_reader :app + + alias_method :response, :last_response + alias_method :request, :last_request + + def initialize(app) + @app = app + end + + def body + response.body + end +end \ No newline at end of file diff --git a/lib/webcat/server.rb b/lib/webcat/server.rb new file mode 100644 index 00000000..0600cbac --- /dev/null +++ b/lib/webcat/server.rb @@ -0,0 +1,48 @@ +class Webcat::Server + attr_reader :app + + def initialize(app) + @app = app + end + + def port + 9081 + end + + def host + 'localhost' + end + + def url(path) + "http://#{host}:#{port}#{path}" + end + + def boot + Webcat.log "[webcat] Booting Rack applicartion on port #{port}" + start_time = Time.now + Thread.new do + Rack::Handler::Mongrel.run @app, :Port => port + end + Webcat.log "[webcat] checking if application has booted" + loop do + begin + res = Net::HTTP.start(host, port) { |http| http.get('/') } + + if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection) + Webcat.log "[webcat] application has booted" + break + end + rescue Errno::ECONNREFUSED + end + + if Time.now - start_time > 5 + puts "[webcat] Rack application timed out during boot" + exit + end + + Webcat.log '.' + sleep 1 + end + end + +end \ No newline at end of file diff --git a/script/console b/script/console new file mode 100755 index 00000000..555c05db --- /dev/null +++ b/script/console @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby +# File: script/console +irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb' + +libs = " -r irb/completion" +# Perhaps use a console_lib to store any extra methods I may want available in the cosole +# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}" +libs << " -r #{File.dirname(__FILE__) + '/../lib/webcat.rb'}" +puts "Loading webcat gem" +exec "#{irb} #{libs} --simple-prompt" \ No newline at end of file diff --git a/script/destroy b/script/destroy new file mode 100755 index 00000000..e48464df --- /dev/null +++ b/script/destroy @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby +APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) + +begin + require 'rubigen' +rescue LoadError + require 'rubygems' + require 'rubigen' +end +require 'rubigen/scripts/destroy' + +ARGV.shift if ['--help', '-h'].include?(ARGV[0]) +RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit] +RubiGen::Scripts::Destroy.new.run(ARGV) diff --git a/script/generate b/script/generate new file mode 100755 index 00000000..c27f6559 --- /dev/null +++ b/script/generate @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby +APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) + +begin + require 'rubigen' +rescue LoadError + require 'rubygems' + require 'rubigen' +end +require 'rubigen/scripts/generate' + +ARGV.shift if ['--help', '-h'].include?(ARGV[0]) +RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit] +RubiGen::Scripts::Generate.new.run(ARGV) diff --git a/spec/culerity_driver_spec.rb b/spec/culerity_driver_spec.rb new file mode 100644 index 00000000..a0d5b1c5 --- /dev/null +++ b/spec/culerity_driver_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('spec_helper', File.dirname(__FILE__)) +require 'drivers_spec' + +describe Webcat::Driver::Culerity do + before do + @driver = Webcat::Driver::Culerity.new(TestApp) + end + + it_should_behave_like "driver" +end \ No newline at end of file diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb new file mode 100644 index 00000000..975ce0aa --- /dev/null +++ b/spec/drivers_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('spec_helper', File.dirname(__FILE__)) + +shared_examples_for 'driver' do + + describe '#get' do + it "should fetch a response" do + @driver.get('/') + @driver.body.should == 'Hello world!' + @driver.get('/foo') + @driver.body.should == 'Another World' + end + end + +end \ No newline at end of file diff --git a/spec/rack_test_driver_spec.rb b/spec/rack_test_driver_spec.rb new file mode 100644 index 00000000..865abfc8 --- /dev/null +++ b/spec/rack_test_driver_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('spec_helper', File.dirname(__FILE__)) +require 'drivers_spec' + +describe Webcat::Driver::RackTest do + before do + @driver = Webcat::Driver::RackTest.new(TestApp) + end + + it_should_behave_like "driver" +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..e22c1633 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,16 @@ +$:.unshift(File.expand_path('../lib', File.dirname(__FILE__))) + +require 'rubygems' +require 'webcat' +require 'sinatra/base' +require 'rack' + +class TestApp < Sinatra::Base + get '/' do + 'Hello world!' + end + + get '/foo' do + 'Another World' + end +end diff --git a/spec/webcat_spec.rb b/spec/webcat_spec.rb new file mode 100644 index 00000000..a7ba8a30 --- /dev/null +++ b/spec/webcat_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('spec_helper', File.dirname(__FILE__)) + +describe Webcat::Session do + before do + @session = Webcat::Session.new(:rack_test, TestApp) + end + + describe '#driver' do + it "should be a rack test driver" do + @session.driver.should be_an_instance_of(Webcat::Driver::RackTest) + end + end + + describe '#app' do + it "should remember the application" do + @session.app.should == TestApp + end + end + + describe '#mode' do + it "should remember the mode" do + @session.mode.should == :rack_test + end + end + + describe '#get' do + it "should fetch a response from the driver" do + @session.get('/') + @session.body.should == 'Hello world!' + @session.get('/foo') + @session.body.should == 'Another World' + end + end + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 00000000..5398b68f --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,3 @@ +require 'stringio' +require 'test/unit' +require File.dirname(__FILE__) + '/../lib/webcat' diff --git a/test/test_webcat.rb b/test/test_webcat.rb new file mode 100644 index 00000000..f2b62d13 --- /dev/null +++ b/test/test_webcat.rb @@ -0,0 +1,11 @@ +require File.dirname(__FILE__) + '/test_helper.rb' + +class TestWebcat < Test::Unit::TestCase + + def setup + end + + def test_truth + assert true + end +end