1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Changed to use selenium-webdriver and it works!

This commit is contained in:
Jonas Nicklas and Kevin Fitzpatrick 2009-11-14 10:44:46 +01:00 committed by Jonas Nicklas
parent feb29e2d64
commit ac7d044243
11 changed files with 135 additions and 141 deletions

9
lib/webcat/cucumber.rb Normal file
View file

@ -0,0 +1,9 @@
require 'webcat'
require 'webcat/dsl'
if defined?(Rails)
Webcat.app = ActionController::Dispatcher.new
end
World(Webcat)

View file

@ -1,61 +0,0 @@
module Webcat
module Selenium
class SeleniumRCServer
unless Kernel.respond_to?(:silence_stream)
def silence_stream(stream)
old_stream = stream.dup
stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
end
end
def booted?
@booted || false
end
def boot
return if booted?
silence_stream(STDOUT) do
remote_control.start :background => true
end
wait
@booted = true
at_exit do
silence_stream(STDOUT) do
remote_control.stop
end
end
end
def remote_control
return @remote_control if @remote_control
@remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 5041, 15)
@remote_control.jar_file = jar_path
return @remote_control
end
def jar_path
File.expand_path("selenium-server.jar", File.dirname(__FILE__))
end
def wait
return true
$stderr.print "==> Waiting for Selenium RC server on port #{Webrat.configuration.selenium_server_port}... "
TCPSocket.wait_for_service_with_timeout(:host => "localhost", :port => 5041, :timeout => 15)
$stderr.print "Ready!\n"
rescue SocketError
$stderr.puts "==> Failed to boot the Selenium RC server... exiting!"
exit
end
end
end
end

View file

@ -1,7 +1,4 @@
require 'selenium/client' require 'selenium-webdriver'
require 'webcat/driver/selenium/rc_server'
require 'webcat/core_ext/tcp_socket'
class Webcat::Driver::Selenium class Webcat::Driver::Selenium
class Node < Struct.new(:node) class Node < Struct.new(:node)
@ -9,13 +6,32 @@ class Webcat::Driver::Selenium
node.text node.text
end end
def attribute(name) def [](name)
value = if name.to_sym == :class if name == :value
node.class_name node.value
else else
node.send(name.to_sym) node.attribute(name)
end end
return value if value and not value.empty? rescue Selenium::WebDriver::Error::WebDriverError
nil
end
def set(value)
if tag_name == 'input' and %w(text password hidden file).include?(type)
node.clear
node.send_keys(value.to_s)
elsif tag_name == 'input' and type == 'radio'
node.select
elsif tag_name == 'input' and type == 'checkbox'
node.toggle
elsif tag_name == "textarea"
node.clear
node.send_keys(value.to_s)
end
end
def select(option)
node.find_element(:xpath, ".//option[text()='#{option}']").select
end end
def click def click
@ -23,35 +39,45 @@ class Webcat::Driver::Selenium
end end
def tag_name def tag_name
# FIXME: this might be the dumbest way ever of getting the tag name node.tag_name
# there has to be something better...
node.to_xml[/^\s*<([a-z0-9\-\:]+)/, 1]
end end
private
def type
self[:type]
end
end end
attr_reader :app, :rack_server attr_reader :app, :rack_server
def self.server def self.driver
@server ||= Webcat::Selenium::SeleniumRCServer.new unless @driver
@driver = Selenium::WebDriver.for :firefox
at_exit do
@driver.quit
end
end
@driver
end end
def initialize(app) def initialize(app)
@app = app @app = app
@rack_server = Webcat::Server.new(@app) @rack_server = Webcat::Server.new(@app)
@rack_server.boot @rack_server.boot
self.class.server.boot
end end
def visit(path) def visit(path)
browser.open(url(path)) driver.navigate.to(url(path))
end end
def body def body
browser.html driver.page_source
end end
def find(selector) def find(selector)
browser.elements_by_xpath(selector).map { |node| Node.new(node) } driver.find_elements(:xpath, selector).map { |node| Node.new(node) }
end end
private private
@ -60,21 +86,8 @@ private
rack_server.url(path) rack_server.url(path)
end end
def browser def driver
unless @_browser self.class.driver
@_browser = Selenium::Client::Driver.new :host => 'localhost',
:port => 4444,
:browser => "*firefox",
:url => rack_server.url('/'),
:timeout_in_second => 10
@_browser.start_new_browser_session
at_exit do
@_browser.close_current_browser_session
end
end
@_browser
end end
end end

View file

@ -12,6 +12,8 @@ class Webcat::Session
Webcat::Driver::RackTest.new(app) Webcat::Driver::RackTest.new(app)
when :culerity when :culerity
Webcat::Driver::Culerity.new(app) Webcat::Driver::Culerity.new(app)
when :selenium
Webcat::Driver::Selenium.new(app)
else else
raise Webcat::DriverNotFoundError, "no driver called #{mode} was found" raise Webcat::DriverNotFoundError, "no driver called #{mode} was found"
end end

View file

@ -5,6 +5,6 @@ describe Webcat::Driver::Selenium do
@driver = Webcat::Driver::Selenium.new(TestApp) @driver = Webcat::Driver::Selenium.new(TestApp)
end end
# it_should_behave_like "driver" it_should_behave_like "driver"
# it_should_behave_like "driver with javascript support" it_should_behave_like "driver with javascript support"
end end

View file

@ -30,7 +30,7 @@ shared_examples_for 'driver' do
end end
it "should find the correct number of elements" do it "should find the correct number of elements" do
@driver.find('//a').size.should == 2 @driver.find('//a').size.should == 3
end end
it "should extract node texts" do it "should extract node texts" do

1
spec/fixtures/test_file.txt vendored Normal file
View file

@ -0,0 +1 @@
ThisIsTheTestFile

View file

@ -0,0 +1,23 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Webcat::Session do
context 'with selenium driver' do
before do
@session = Webcat::Session.new(:selenium, TestApp)
end
describe '#driver' do
it "should be a rack test driver" do
@session.driver.should be_an_instance_of(Webcat::Driver::Selenium)
end
end
describe '#mode' do
it "should remember the mode" do
@session.mode.should == :selenium
end
end
it_should_behave_like "session"
end
end

View file

@ -1,6 +1,12 @@
require File.expand_path('spec_helper', File.dirname(__FILE__)) require File.expand_path('spec_helper', File.dirname(__FILE__))
require 'nokogiri'
shared_examples_for "session" do shared_examples_for "session" do
def extract_results(session)
YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.text
end
describe '#app' do describe '#app' do
it "should remember the application" do it "should remember the application" do
@session.app.should == TestApp @session.app.should == TestApp
@ -10,9 +16,9 @@ shared_examples_for "session" do
describe '#visit' do describe '#visit' do
it "should fetch a response from the driver" do it "should fetch a response from the driver" do
@session.visit('/') @session.visit('/')
@session.body.should == 'Hello world!' @session.body.should include('Hello world!')
@session.visit('/foo') @session.visit('/foo')
@session.body.should == 'Another World' @session.body.should include('Another World')
end end
end end
@ -24,21 +30,21 @@ shared_examples_for "session" do
context "with id given" do context "with id given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.click_link('foo') @session.click_link('foo')
@session.body.should == 'Another World' @session.body.should include('Another World')
end end
end end
context "with text given" do context "with text given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.click_link('labore') @session.click_link('labore')
@session.body.should == '<h1>Bar</h1>' @session.body.should include('<h1>Bar</h1>')
end end
end end
context "with title given" do context "with title given" do
it "should take user to the linked page" do it "should take user to the linked page" do
@session.click_link('awesome title') @session.click_link('awesome title')
@session.body.should == '<h1>Bar</h1>' @session.body.should include('<h1>Bar</h1>')
end end
end end
@ -64,7 +70,7 @@ shared_examples_for "session" do
context "with value given on a submit button" do context "with value given on a submit button" do
before do before do
@session.click_button('awesome') @session.click_button('awesome')
@results = YAML.load(@session.body) @results = extract_results(@session)
end end
it "should serialize and submit text fields" do it "should serialize and submit text fields" do
@ -121,24 +127,21 @@ shared_examples_for "session" do
context "with id given on a submit button" do context "with id given on a submit button" do
it "should submit the associated form" do it "should submit the associated form" do
@session.click_button('awe123') @session.click_button('awe123')
results = YAML.load(@session.body) extract_results(@session)['first_name'].should == 'John'
results['first_name'].should == 'John'
end end
end end
context "with value given on an image button" do context "with value given on an image button" do
it "should submit the associated form" do it "should submit the associated form" do
@session.click_button('okay') @session.click_button('okay')
results = YAML.load(@session.body) extract_results(@session)['first_name'].should == 'John'
results['first_name'].should == 'John'
end end
end end
context "with id given on an image button" do context "with id given on an image button" do
it "should submit the associated form" do it "should submit the associated form" do
@session.click_button('okay556') @session.click_button('okay556')
results = YAML.load(@session.body) extract_results(@session)['first_name'].should == 'John'
results['first_name'].should == 'John'
end end
end end
@ -156,39 +159,39 @@ shared_examples_for "session" do
it "should fill in a text field by id" do it "should fill in a text field by id" do
@session.fill_in('form_first_name', :with => 'Harry') @session.fill_in('form_first_name', :with => 'Harry')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['first_name'].should == 'Harry' extract_results(@session)['first_name'].should == 'Harry'
end end
it "should fill in a text field by label" do it "should fill in a text field by label" do
@session.fill_in('First Name', :with => 'Harry') @session.fill_in('First Name', :with => 'Harry')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['first_name'].should == 'Harry' extract_results(@session)['first_name'].should == 'Harry'
end end
it "should fill in a textarea by id" do it "should fill in a textarea by id" do
@session.fill_in('form_description', :with => 'Texty text') @session.fill_in('form_description', :with => 'Texty text')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['description'].should == 'Texty text' extract_results(@session)['description'].should == 'Texty text'
end end
it "should fill in a textarea by label" do it "should fill in a textarea by label" do
@session.fill_in('Description', :with => 'Texty text') @session.fill_in('Description', :with => 'Texty text')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['description'].should == 'Texty text' extract_results(@session)['description'].should == 'Texty text'
end end
it "should fill in a password field by id" do it "should fill in a password field by id" do
pending "Culerity doesn't like password fields for some reason" if @session.mode == :culerity pending "Culerity doesn't like password fields for some reason" if @session.mode == :culerity
@session.fill_in('form_password', :with => 'supasikrit') @session.fill_in('form_password', :with => 'supasikrit')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['password'].should == 'supasikrit' extract_results(@session)['password'].should == 'supasikrit'
end end
it "should fill in a password field by label" do it "should fill in a password field by label" do
pending "Culerity doesn't like password fields for some reason" if @session.mode == :culerity pending "Culerity doesn't like password fields for some reason" if @session.mode == :culerity
@session.fill_in('Password', :with => 'supasikrit') @session.fill_in('Password', :with => 'supasikrit')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['password'].should == 'supasikrit' extract_results(@session)['password'].should == 'supasikrit'
end end
end end
@ -200,13 +203,13 @@ shared_examples_for "session" do
it "should choose a radio button by id" do it "should choose a radio button by id" do
@session.choose("gender_male") @session.choose("gender_male")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['gender'].should == 'male' extract_results(@session)['gender'].should == 'male'
end end
it "should choose a radio button by label" do it "should choose a radio button by label" do
@session.choose("Both") @session.choose("Both")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['gender'].should == 'both' extract_results(@session)['gender'].should == 'both'
end end
end end
@ -219,7 +222,7 @@ shared_examples_for "session" do
pending "Culerity doesn't like hidden fields for some reason" if @session.mode == :culerity pending "Culerity doesn't like hidden fields for some reason" if @session.mode == :culerity
@session.set_hidden_field("form_token", :to => 'test567') @session.set_hidden_field("form_token", :to => 'test567')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['token'].should == 'test567' extract_results(@session)['token'].should == 'test567'
end end
end end
@ -231,13 +234,13 @@ shared_examples_for "session" do
it "should check a checkbox by id" do it "should check a checkbox by id" do
@session.check("form_pets_cat") @session.check("form_pets_cat")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['pets'].should include('dog', 'cat', 'hamster') extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
end end
it "should check a checkbox by label" do it "should check a checkbox by label" do
@session.check("Cat") @session.check("Cat")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['pets'].should include('dog', 'cat', 'hamster') extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
end end
end end
@ -250,16 +253,16 @@ shared_examples_for "session" do
pending "Culerity doesn't seem to uncheck this" if @session.mode == :culerity pending "Culerity doesn't seem to uncheck this" if @session.mode == :culerity
@session.uncheck("form_pets_hamster") @session.uncheck("form_pets_hamster")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['pets'].should include('dog') extract_results(@session)['pets'].should include('dog')
YAML.load(@session.body)['pets'].should_not include('hamster') extract_results(@session)['pets'].should_not include('hamster')
end end
it "should uncheck a checkbox by label" do it "should uncheck a checkbox by label" do
pending "Culerity doesn't seem to uncheck this" if @session.mode == :culerity pending "Culerity doesn't seem to uncheck this" if @session.mode == :culerity
@session.uncheck("Hamster") @session.uncheck("Hamster")
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['pets'].should include('dog') extract_results(@session)['pets'].should include('dog')
YAML.load(@session.body)['pets'].should_not include('hamster') extract_results(@session)['pets'].should_not include('hamster')
end end
end end
@ -271,13 +274,13 @@ shared_examples_for "session" do
it "should select an option from a select box by id" do it "should select an option from a select box by id" do
@session.select("Finish", :from => 'form_locale') @session.select("Finish", :from => 'form_locale')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['locale'].should == 'fi' extract_results(@session)['locale'].should == 'fi'
end end
it "should select an option from a select box by label" do it "should select an option from a select box by label" do
@session.select("Finish", :from => 'Locale') @session.select("Finish", :from => 'Locale')
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['locale'].should == 'fi' extract_results(@session)['locale'].should == 'fi'
end end
end end
@ -305,27 +308,31 @@ shared_examples_for "session" do
it "should set a file path by id" do it "should set a file path by id" do
@session.attach_file "form_image", __FILE__ @session.attach_file "form_image", __FILE__
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['image'].should == File.basename(__FILE__) extract_results(@session)['image'].should == File.basename(__FILE__)
end end
it "should set a file path by label" do it "should set a file path by label" do
@session.attach_file "Image", __FILE__ @session.attach_file "Image", __FILE__
@session.click_button('awesome') @session.click_button('awesome')
YAML.load(@session.body)['image'].should == File.basename(__FILE__) extract_results(@session)['image'].should == File.basename(__FILE__)
end end
end end
context "with multipart form" do context "with multipart form" do
before do
@test_file_path = File.expand_path('fixtures/test_file.txt', File.dirname(__FILE__))
end
it "should set a file path by id" do it "should set a file path by id" do
@session.attach_file "form_document", __FILE__ @session.attach_file "form_document", @test_file_path
@session.click_button('Upload') @session.click_button('Upload')
@session.body.should == File.read(__FILE__) @session.body.should include(File.read(@test_file_path))
end end
it "should set a file path by label" do it "should set a file path by label" do
@session.attach_file "Document", __FILE__ @session.attach_file "Document", @test_file_path
@session.click_button('Upload') @session.click_button('Upload')
@session.body.should == File.read(__FILE__) @session.body.should include(File.read(@test_file_path))
end end
end end

View file

@ -46,7 +46,7 @@ class TestApp < Sinatra::Base
end end
post '/form' do post '/form' do
params[:form].to_yaml '<pre id="results">' + params[:form].to_yaml + '</pre>'
end end
post '/upload' do post '/upload' do