From 36567032bff88b777f7e7df8ae45d848418a4b8b Mon Sep 17 00:00:00 2001 From: Matthias Marschall Date: Wed, 27 May 2009 14:02:09 +0200 Subject: [PATCH 1/3] click_button in selenium works now same as in headless mode --- .../location_strategy_javascript/button.js | 21 +++++++++++++------ 1 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/webrat/selenium/location_strategy_javascript/button.js b/lib/webrat/selenium/location_strategy_javascript/button.js index add1679..1af6900 100644 --- a/lib/webrat/selenium/location_strategy_javascript/button.js +++ b/lib/webrat/selenium/location_strategy_javascript/button.js @@ -1,12 +1,19 @@ if (locator == '*') { return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow) } +var buttons = inDocument.getElementsByTagName('button'); var inputs = inDocument.getElementsByTagName('input'); -return $A(inputs).find(function(candidate){ - inputType = candidate.getAttribute('type'); - if (inputType == 'submit' || inputType == 'image') { - var buttonText = $F(candidate); - return (PatternMatcher.matches(locator, buttonText)); - } - return false; +var result = $A(inputs).concat($A(buttons)).find(function(candidate){ + var type = candidate.getAttribute('type'); + if (type == 'submit' || type == 'image' || type == 'button') { + var matches_id = PatternMatcher.matches(locator, candidate.id); + var matches_value = PatternMatcher.matches(locator, candidate.value); + var matches_html = PatternMatcher.matches(locator, candidate.innerHTML); + var matches_alt = PatternMatcher.matches(locator, candidate.alt); + if (matches_id || matches_value || matches_html || matches_alt) { + return true; + } + } + return false; }); +return result; -- 1.6.1 From d37c950b0e22d13736e5643f994ce40922fe6d31 Mon Sep 17 00:00:00 2001 From: Matthias Marschall Date: Fri, 29 May 2009 10:24:51 +0200 Subject: [PATCH 2/3] made selenium integration tests run applying larrytheliquid's patch: http://github.com/larrytheliquid/webrat/commit/627913708c563f136f17d7da1b85b961f20f143b --- lib/webrat/selenium.rb | 8 +------- lib/webrat/selenium/silence_stream.rb | 20 ++++++++++++-------- .../rails/test/integration/webrat_test.rb | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/webrat/selenium.rb b/lib/webrat/selenium.rb index 2564645..d47c583 100644 --- a/lib/webrat/selenium.rb +++ b/lib/webrat/selenium.rb @@ -1,13 +1,7 @@ require "webrat" gem "selenium-client", ">=1.2.14" require "selenium/client" - -# active_support already defines silence_stream, no need to do that again if it's already present. -# http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb -unless Kernel.respond_to?(:silence_stream) - require "webrat/selenium/silence_stream" -end - +require "webrat/selenium/silence_stream" require "webrat/selenium/selenium_session" require "webrat/selenium/matchers" require "webrat/core_extensions/tcp_socket" diff --git a/lib/webrat/selenium/silence_stream.rb b/lib/webrat/selenium/silence_stream.rb index a458306..f5e54d8 100644 --- a/lib/webrat/selenium/silence_stream.rb +++ b/lib/webrat/selenium/silence_stream.rb @@ -1,14 +1,18 @@ module Webrat module Selenium module SilenceStream - 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 + # active_support already defines silence_stream, no need to do that again if it's already present. + # http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb + 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 end end end \ No newline at end of file diff --git a/spec/integration/rails/test/integration/webrat_test.rb b/spec/integration/rails/test/integration/webrat_test.rb index b1449ca..9d858c6 100644 --- a/spec/integration/rails/test/integration/webrat_test.rb +++ b/spec/integration/rails/test/integration/webrat_test.rb @@ -2,9 +2,12 @@ require 'test_helper' class WebratTest < ActionController::IntegrationTest - test "should visit fully qualified urls" do - visit root_url(:host => "chunkybacon.example.com") - assert_equal "chunkybacon", request.subdomains.first + #Firefox raises a security concern under Selenium + unless ENV['WEBRAT_INTEGRATION_MODE'] == 'selenium' + test "should visit fully qualified urls" do + visit root_url(:host => "chunkybacon.example.com") + assert_equal "chunkybacon", request.subdomains.first + end end test "should visit pages" do @@ -68,9 +71,12 @@ class WebratTest < ActionController::IntegrationTest assert_have_selector "h1" end - test "should detect infinite redirects" do - assert_raises Webrat::InfiniteRedirectError do - visit infinite_redirect_path + # Firefox detects and prevents infinite redirects under Selenium + unless ENV['WEBRAT_INTEGRATION_MODE'] == 'selenium' + test "should detect infinite redirects" do + assert_raises Webrat::InfiniteRedirectError do + visit infinite_redirect_path + end end end -- 1.6.1 From 1348d4c10c36b39102a75dcfa3881dc8298e417c Mon Sep 17 00:00:00 2001 From: Matthias Marschall Date: Fri, 29 May 2009 12:07:10 +0200 Subject: [PATCH 3/3] added integration tests for all cases of click_button (by id, by html, by value, by alt for buttons and inputs of types image, button and submit) --- .../rails/app/controllers/webrat_controller.rb | 3 + .../rails/app/views/webrat/buttons.html.erb | 11 +++ spec/integration/rails/config/routes.rb | 1 + .../rails/test/integration/button_click_test.rb | 80 ++++++++++++++++++++ 4 files changed, 95 insertions(+), 0 deletions(-) create mode 100644 spec/integration/rails/app/views/webrat/buttons.html.erb create mode 100644 spec/integration/rails/test/integration/button_click_test.rb diff --git a/spec/integration/rails/app/controllers/webrat_controller.rb b/spec/integration/rails/app/controllers/webrat_controller.rb index b117ae8..e9f9ad4 100644 --- a/spec/integration/rails/app/controllers/webrat_controller.rb +++ b/spec/integration/rails/app/controllers/webrat_controller.rb @@ -9,6 +9,9 @@ class WebratController < ApplicationController def form end + def buttons + end + def submit render :text => "OK Test Link Text" end diff --git a/spec/integration/rails/app/views/webrat/buttons.html.erb b/spec/integration/rails/app/views/webrat/buttons.html.erb new file mode 100644 index 0000000..e127059 --- /dev/null +++ b/spec/integration/rails/app/views/webrat/buttons.html.erb @@ -0,0 +1,11 @@ +

Webrat Buttons Form

+ +<% form_tag submit_path do %> + + + + + + + +<% end %> \ No newline at end of file diff --git a/spec/integration/rails/config/routes.rb b/spec/integration/rails/config/routes.rb index ced0b4f..e330d3f 100644 --- a/spec/integration/rails/config/routes.rb +++ b/spec/integration/rails/config/routes.rb @@ -9,6 +9,7 @@ ActionController::Routing::Routes.draw do |map| webrat.redirect_to_show_params "/redirect_to_show_params", :action => "redirect_to_show_params" webrat.show_params "/show_params", :action => "show_params" + webrat.buttons "/buttons", :action => "buttons" webrat.root :action => "form" end end diff --git a/spec/integration/rails/test/integration/button_click_test.rb b/spec/integration/rails/test/integration/button_click_test.rb new file mode 100644 index 0000000..3cd0c6c --- /dev/null +++ b/spec/integration/rails/test/integration/button_click_test.rb @@ -0,0 +1,80 @@ +require 'test_helper' + +class ButtonClickTest < ActionController::IntegrationTest + #