Merge branch 'master' of git://github.com/brynary/webrat

This commit is contained in:
Thomas Jack 2009-06-02 17:52:14 -05:00
commit 57326e5846
26 changed files with 261 additions and 56 deletions

View File

@ -11,7 +11,11 @@
* Filled in tests on click link lh 195 (diabolo)
* Added current_url to selenium session lh 215 (Luke Amdor)
* Added silence spec to selenium lh 238 (Martin Gamsjaeger aka snusnu)
* Added ability to configure the browser startup timeout for selenium lh 242 (Mike Gaffney)
* Added ability to configure the browser startup timeout for selenium lh 242 (Mike Gaffney, Mark Dodwell)
* Added delegation for field_named lh194 (pivotal labs)
* Added fix to keep from escaping field values in mechanize mode lh256 (jish)
* Adding fixes for click button/link and made the integration specs pass for the same in selenium lh254 (Ldiehl, Matthias Marschall)
* Adding clicking link by id in selenium mode lh221 (larrytheliquid)
== 0.4.4 / 2009-04-06

View File

@ -87,6 +87,8 @@ module Webrat
parse_rails_request_params("#{name}=#{escaped_value}")
when :merb
::Merb::Parse.query("#{name}=#{escaped_value}")
when :mechanize
{ name => value }
else
{ name => escaped_value }
end

View File

@ -57,9 +57,7 @@ module Webrat
:field_by_xpath,
:field_with_id,
:selenium,
:simulate, :automate
:simulate, :automate,
:field_named
end
end

View File

@ -255,6 +255,7 @@ For example:
def_delegators :current_scope, :field_by_xpath
def_delegators :current_scope, :field_with_id
def_delegators :current_scope, :select_option
def_delegators :current_scope, :field_named
private

View File

@ -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"

View File

@ -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;

View File

@ -1,6 +1,9 @@
var links = inDocument.getElementsByTagName('a');
var candidateLinks = $A(links).select(function(candidateLink) {
return PatternMatcher.matches(locator, getText(candidateLink));
var textMatched = PatternMatcher.matches(locator, getText(candidateLink));
var idMatched = PatternMatcher.matches(locator, candidateLink.id);
var titleMatched = PatternMatcher.matches(locator, candidateLink.title);
return textMatched || idMatched || titleMatched;
});
if (candidateLinks.length == 0) {
return null;

View File

@ -41,7 +41,7 @@ module Webrat
webrat_deprecate :visits, :visit
def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}"
locator = "webrat=#{field_identifier}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.type(locator, "#{options[:with]}")
end

View File

@ -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

View File

@ -0,0 +1,7 @@
class ButtonsController < ApplicationController
def show
end
def create
render :text => "success"
end
end

View File

@ -0,0 +1,4 @@
class FieldsController < ApplicationController
def show
end
end

View File

@ -0,0 +1,7 @@
class LinksController < ApplicationController
def show
if params[:value]
render :text => "Link:#{params[:value]}"
end
end
end

View File

@ -5,35 +5,35 @@ class FakeModel
end
class WebratController < ApplicationController
def form
end
def submit
render :text => "OK <a href='/' id='link_id'>Test Link Text</a>"
end
def internal_redirect
redirect_to submit_path
end
def infinite_redirect
redirect_to infinite_redirect_path
end
def external_redirect
redirect_to "http://google.com"
end
def before_redirect_form
end
def redirect_to_show_params
redirect_to show_params_path(:custom_param => "123")
end
def show_params
render :text => params.to_json
end
end

View File

@ -0,0 +1,2 @@
module ButtonsHelper
end

View File

@ -0,0 +1,2 @@
module FieldsHelper
end

View File

@ -0,0 +1,2 @@
module LinksHelper
end

View File

@ -0,0 +1,11 @@
<h1 id='form_title' class='form title'>Webrat Buttons Form</h1>
<% form_tag "/buttons" do %>
<input type="button" id="input_button_id" value="input_button_value" />
<input type="submit" id="input_submit_id" value="input_submit_value" />
<input type="image" id="input_image_id" value="input_image_value" alt="input_image_alt" src="" />
<button type="button" id="button_button_id" value="button_button_value">button_button_text</button>
<button type="submit" id="button_submit_id" value="button_submit_value">button_submit_text</button>
<button type="image" id="button_image_id" value="button_image_value">button_image_text</button>
<% end %>

View File

@ -0,0 +1,8 @@
<h1>Webrat Fields Page</h1>
<h2>Filling In</h2>
<ul>
<li><input type="text" name="field_by_name" value="" /></li>
<li><input type="text" name="rails[naming]" value="" /></li>
<li><input type="text" id="field_by_id" value="" /></li>
<li><label for="field_by_label_id">FieldByLabelId</label><input type="text" id="field_by_label_id" /></li>
</ul>

View File

@ -0,0 +1,4 @@
<h1>Webrat Links Page</h1>
<a href="/links?value=LinkByText">LinkByText</a><br />
<a href="/links?value=link_by_id" id="link_by_id">id</a><br />
<a href="/links?value=LinkByTitle" title="LinkByTitle">title</a>

View File

@ -0,0 +1,11 @@
<h1 id='form_title' class='form title'>Webrat Buttons Form</h1>
<% form_tag submit_path do %>
<input type="button" id="input_button_id" value="input_button_value">
<input type="submit" id="input_submit_id" value="input_submit_value">
<input type="image" id="input_image_id" value="input_image_value" alt="input_image_alt" src="">
<button type="button" id="button_button_id" value="button_button_value">button_button_text</button>
<button type="submit" id="button_submit_id" value="button_submit_value">button_submit_text</button>
<button type="image" id="button_image_id" value="button_image_value">button_image_text</button>
<% end %>

View File

@ -1,4 +1,7 @@
ActionController::Routing::Routes.draw do |map|
map.resource 'links', :only => [:show]
map.resource 'buttons', :only => [:show, :create]
map.resource 'fields', :only => [:show]
map.with_options :controller => "webrat" do |webrat|
webrat.submit "/submit", :action => "submit"
webrat.internal_redirect "/internal_redirect", :action => "internal_redirect"

View File

@ -0,0 +1,80 @@
require 'test_helper'
class ButtonClickTest < ActionController::IntegrationTest
# <button type="button" ...>
test "should click button with type button by id" do
visit buttons_path
click_button "button_button_id"
end
test "should click button with type button by value" do
visit buttons_path
click_button "button_button_value"
end
test "should click button with type button by html" do
visit buttons_path
click_button "button_button_text"
end
# <button type="submit" ...>
test "should click button with type submit by id" do
visit buttons_path
click_button "button_submit_id"
end
test "should click button with type submit by value" do
visit buttons_path
click_button "button_submit_value"
end
test "should click button with type submit by html" do
visit buttons_path
click_button "button_submit_text"
end
# <button type="image" ...>
test "should click button with type image by id" do
visit buttons_path
click_button "button_image_id"
end
test "should click button with type image by value" do
visit buttons_path
click_button "button_image_value"
end
test "should click button with type image by html" do
visit buttons_path
click_button "button_image_text"
end
# <input type="button" ...>
test "should click image with type button by id" do
visit buttons_path
click_button "input_button_id"
end
test "should click input with type button by value" do
visit buttons_path
click_button "input_button_value"
end
# <input type="submit" ...>
test "should click input with type submit by id" do
visit buttons_path
click_button "input_submit_id"
end
test "should click input with type submit by value" do
visit buttons_path
click_button "input_submit_value"
end
# <input type="image" ...>
test "should click input with type image by id" do
visit buttons_path
click_button "input_image_id"
end
test "should click input with type image by value" do
visit buttons_path
click_button "input_image_value"
end
test "should click input with type image by alt" do
visit buttons_path
click_button "input_image_alt"
end
end

View File

@ -0,0 +1,20 @@
require 'test_helper'
class FillInTest < ActionController::IntegrationTest
test "should fill in text field by name" do
visit fields_path
fill_in "field_by_name", :with => "value"
end
test "should fill in text field by name, rails naming lh257" do
visit fields_path
fill_in "rails[naming]", :with => "value"
end
test "should fill in text field by id" do
visit fields_path
fill_in "field_by_id", :with => "value"
end
test "should fill in text field by label via id" do
visit fields_path
fill_in "FieldByLabelId", :with => "value"
end
end

View File

@ -0,0 +1,21 @@
require 'test_helper'
class LinkClickTest < ActionController::IntegrationTest
test "should click link by text" do
visit links_path
click_link "LinkByText"
assert_contain("Link:LinkByText")
end
test "should click link by id" do
visit links_path
click_link "link_by_id"
assert_contain("Link:link_by_id")
end
test "should click link by title" do
visit links_path
click_link "LinkByTitle"
assert_contain("Link:LinkByTitle")
end
end

View File

@ -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
@ -46,18 +49,6 @@ class WebratTest < ActionController::IntegrationTest
assert response.redirect?
end
test "should click link by text" do
visit internal_redirect_path
click_link "Test Link Text"
assert_contain("Webrat Form")
end
test "should click link by id" do
visit internal_redirect_path
click_link "link_id"
assert_contain("Webrat Form")
end
test "should be able to assert xpath" do
visit root_path
assert_have_xpath "//h1"
@ -68,9 +59,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

View File

@ -66,4 +66,20 @@ module Webrat
radio_button.should_not be_checked
end
end
describe TextField do
it 'should not escape values in mechanize mode' do
Webrat.configuration.mode = :mechanize
html = <<-HTML
<html>
<input type="text" name="email" value="user@example.com" />
</html>
HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), 'input').first
text_field = TextField.new(nil, element)
text_field.to_param.should == { 'email' => 'user@example.com' }
end
end
end