Adding FormLocator. Removing some dead methods

This commit is contained in:
Bryan Helmkamp 2008-11-30 15:24:50 -05:00
parent 0eee6d75e4
commit c05c0f6c73
9 changed files with 97 additions and 94 deletions

View File

@ -32,10 +32,6 @@ module Webrat
possible_fields.detect { |possible_field| possible_field.matches_name?(name) } possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end end
def matches_id?(id)
Webrat::XML.attribute(@element, "id") == id.to_s
end
protected protected
def fields_by_type(field_types) def fields_by_type(field_types)

View File

@ -26,25 +26,8 @@ module Webrat
end end
end end
def matches_text?(link_text)
if link_text.is_a?(Regexp)
matcher = link_text
else
matcher = /#{Regexp.escape(link_text.to_s)}/i
end
replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
end
def inner_html
Webrat::XML.inner_html(@element)
end
def text
Webrat::XML.all_inner_text(@element)
end
protected protected
def id def id
Webrat::XML.attribute(@element, "id") Webrat::XML.attribute(@element, "id")
end end
@ -107,13 +90,5 @@ module Webrat
end end
end end
private
def replace_nbsp(str)
str.gsub([0xA0].pack('U'), ' ')
end
def replace_nbsp_ref(str)
str.gsub(' ',' ').gsub(' ', ' ')
end
end end
end end

View File

@ -7,6 +7,7 @@ require "webrat/core/locators/field_by_id_locator"
require "webrat/core/locators/select_option_locator" require "webrat/core/locators/select_option_locator"
require "webrat/core/locators/link_locator" require "webrat/core/locators/link_locator"
require "webrat/core/locators/field_locator" require "webrat/core/locators/field_locator"
require "webrat/core/locators/form_locator"
module Webrat module Webrat
module Locators module Locators

View File

@ -0,0 +1,19 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class FormLocator < Locator
def locate
Form.load(@scope.session, form_element)
end
def form_element
Webrat::XML.css_at(@scope.dom, "#" + @value)
end
end
end
end

View File

@ -261,8 +261,7 @@ module Webrat
webrat_deprecate :clicks_button, :click_button webrat_deprecate :clicks_button, :click_button
def submit_form(id) def submit_form(id)
form = forms.detect { |f| f.matches_id?(id) } FormLocator.new(self, id).locate.submit
form.submit
end end
def dom # :nodoc: def dom # :nodoc:

View File

@ -71,6 +71,10 @@ module Webrat #:nodoc:
xpath_search(*args).first xpath_search(*args).first
end end
def self.css_at(*args)
css_search(*args).first
end
def self.xpath_search(element, *searches) def self.xpath_search(element, *searches)
searches.flatten.map do |search| searches.flatten.map do |search|
if Webrat.configuration.parse_with_nokogiri? if Webrat.configuration.parse_with_nokogiri?

View File

@ -398,4 +398,71 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page?foo=bar", {}) webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_link "Jump to foo bar" click_link "Jump to foo bar"
end end
it "should matches_text? on regexp" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include &nbsp;" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
pending "need to update these"
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
end end

View File

@ -21,62 +21,4 @@ describe Webrat::Link do
link.click link.click
end end
it "should matches_text? on regexp" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include &nbsp;" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
end end