Permalink
Browse files

adds capybara-webkit popup support

  • Loading branch information...
despo committed Feb 26, 2012
1 parent 4aadad2 commit 29fa8cc70f75d256c5056f5160e340c28d28a6b9
@@ -1,27 +1,21 @@
+require_relative 'popup/webkit'
+require_relative 'popup/selenium'
+
module Prickle
module Capybara
class Popup
- include ::Capybara::DSL
-
def initialize
- @popup = page.driver.browser.switch_to.alert
- end
-
- def confirm
- @popup.accept
- end
-
- def dismiss
- @popup.dismiss
- end
-
- def message
- @popup.text
+ return @popup = Popups::Webkit.new if ::Capybara::current_driver == :webkit
+ @popup = Popups::Selenium.new
end
- def contains_message? message
- raise Exceptions::MessageNotContainedInPopup.new(message) unless self.message.eql? message
+ def method_missing method, *args, &block
+ if @popup.respond_to? method
+ @popup.send method, *args, &block
+ else
+ super
+ end
end
end
end
@@ -0,0 +1,30 @@
+module Prickle
+ module Capybara
+ module Popups
+ class Selenium
+
+ include ::Capybara::DSL
+
+ def initialize
+ @popup = page.driver.browser.switch_to.alert
+ end
+
+ def confirm
+ @popup.accept
+ end
+
+ def dismiss
+ @popup.dismiss
+ end
+
+ def message
+ @popup.text
+ end
+
+ def contains_message? message
+ raise Exceptions::MessageNotContainedInPopup.new(message) unless self.message.eql? message
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,52 @@
+module Prickle
+ module Capybara
+ module Popups
+ class Webkit
+
+ include ::Capybara::DSL
+
+ def message
+ @message
+ end
+
+ def contains_message? message
+ raise Exceptions::MessageNotContainedInPopup.new(self.message) unless self.message.include? message
+ end
+
+ def accept &block
+ manage_js_popup true, &block
+ end
+
+ def dismiss &block
+ manage_js_popup false, &block
+ end
+
+ def accept_alert accept=true
+ page.execute_script "window.original_alert_function = window.alert"
+ page.execute_script "window.alert_msg = null"
+ page.execute_script "window.alert = function(msg) { window.alert_msg = msg; return #{!!accept}; }"
+ yield
+ page.execute_script "window.alert = window.original_alert_function"
+ @message = page.evaluate_script "window.alert_msg"
+ self
+ end
+
+ def message
+ @message
+ end
+
+ private
+ def manage_js_popup accept
+ page.execute_script "window.original_confirm_function = window.confirm"
+ page.execute_script "window.confirm_msg = null"
+ page.execute_script "window.confirm = function(msg) { window.confirm_msg = msg; return #{!!accept}; }"
+ yield
+ page.execute_script "window.confirm = window.original_confirm_function"
+ @message = page.evaluate_script "window.confirm_msg"
+ self
+ end
+
+ end
+ end
+ end
+end
View
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 2.8.0"
s.add_development_dependency "sinatra", "~> 1.3.2"
s.add_development_dependency "rake"
+ s.add_development_dependency "capybara-webkit"
s.add_development_dependency "reek"
end
@@ -12,7 +12,7 @@
prickly.visit '/'
end
- context 'Managing popups', :javascript => true do
+ context 'Managing selenium popups', :javascript => true do
it 'can confirm an alert box' do
prickly.click_by_name 'popups'
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe Prickle::Capybara::Popup do
+ let(:prickly) { Prickly.new }
+
+ before do
+ Capybara.default_driver = :webkit
+ Prickle::Capybara.wait_time = nil
+ end
+
+ before(:each) do
+ prickly.visit '/'
+ end
+
+ context 'Managing webkit popups', :js => true do
+ it 'can confirm an alert box', :js => true do
+
+ alert = prickly.popup.accept_alert {
+ prickly.click_by_name 'popups'
+ }
+
+ alert.contains_message? "Hello"
+ end
+
+ it 'can confirm a popup' do
+ alert = prickly.popup.accept {
+ prickly.click_by_name 'confirm_box'
+ }
+ alert.contains_message? "Click yes to continue"
+ end
+
+ it 'can dismiss a popup' do
+ alert = prickly.popup.dismiss {
+ prickly.click_by_name 'confirm_box'
+ }
+
+ alert.contains_message? "Click yes to continue"
+ end
+
+ context "matching text" do
+ it "can verify the content of a popup" do
+ alert = prickly.popup.accept_alert {
+ prickly.click_by_name "popups"
+ }
+
+ alert.contains_message? "Hel"
+ end
+ end
+ end
+end
View
@@ -1,6 +1,7 @@
require File.join(File.dirname(__FILE__), "..", "lib", "prickle", "capybara")
require 'rspec'
+require 'capybara-webkit'
require 'prickle/capybara'
class Prickly

0 comments on commit 29fa8cc

Please sign in to comment.