Skip to content

Commit

Permalink
Adding FormLocator. Removing some dead methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 30, 2008
1 parent 0eee6d7 commit c05c0f6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 94 deletions.
6 changes: 3 additions & 3 deletions lib/webrat/core/elements/field.rb
Expand Up @@ -131,7 +131,7 @@ def labels
def label_elements
return @label_elements unless @label_elements.nil?
@label_elements = []

parent = @element.parent
while parent.respond_to?(:parent)
if parent.name == 'label'
Expand All @@ -140,11 +140,11 @@ def label_elements
end
parent = parent.parent
end

unless id.blank?
@label_elements += Webrat::XML.css_search(form.element, "label[@for='#{id}']")
end

@label_elements
end

Expand Down
4 changes: 0 additions & 4 deletions lib/webrat/core/elements/form.rb
Expand Up @@ -32,10 +32,6 @@ def field_named(name, *field_types)
possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end

def matches_id?(id)
Webrat::XML.attribute(@element, "id") == id.to_s
end

protected

def fields_by_type(field_types)
Expand Down
29 changes: 2 additions & 27 deletions lib/webrat/core/elements/link.rb
Expand Up @@ -26,25 +26,8 @@ def click(options = {})
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

def id
Webrat::XML.attribute(@element, "id")
end
Expand Down Expand Up @@ -106,14 +89,6 @@ def http_method_from_fake_method_param
raise Webrat::WebratError.new("No HTTP method for _method param in #{onclick.inspect}")
end
end

private
def replace_nbsp(str)
str.gsub([0xA0].pack('U'), ' ')
end

def replace_nbsp_ref(str)
str.gsub(' ',' ').gsub(' ', ' ')
end

end
end
1 change: 1 addition & 0 deletions lib/webrat/core/locators.rb
Expand Up @@ -7,6 +7,7 @@
require "webrat/core/locators/select_option_locator"
require "webrat/core/locators/link_locator"
require "webrat/core/locators/field_locator"
require "webrat/core/locators/form_locator"

module Webrat
module Locators
Expand Down
19 changes: 19 additions & 0 deletions lib/webrat/core/locators/form_locator.rb
@@ -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
3 changes: 1 addition & 2 deletions lib/webrat/core/scope.rb
Expand Up @@ -261,8 +261,7 @@ def click_button(value = nil)
webrat_deprecate :clicks_button, :click_button

def submit_form(id)
form = forms.detect { |f| f.matches_id?(id) }
form.submit
FormLocator.new(self, id).locate.submit
end

def dom # :nodoc:
Expand Down
4 changes: 4 additions & 0 deletions lib/webrat/core/xml.rb
Expand Up @@ -71,6 +71,10 @@ def self.xpath_at(*args)
xpath_search(*args).first
end

def self.css_at(*args)
css_search(*args).first
end

def self.xpath_search(element, *searches)
searches.flatten.map do |search|
if Webrat.configuration.parse_with_nokogiri?
Expand Down
67 changes: 67 additions & 0 deletions spec/api/click_link_spec.rb
Expand Up @@ -398,4 +398,71 @@
webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_link "Jump to foo bar"
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
58 changes: 0 additions & 58 deletions spec/webrat/core/link_spec.rb
Expand Up @@ -21,62 +21,4 @@
link.click
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

0 comments on commit c05c0f6

Please sign in to comment.