Skip to content

Commit

Permalink
None of the HTML expressions take arguments any more
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Jan 2, 2012
1 parent 422ff06 commit b6074b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 70 deletions.
56 changes: 11 additions & 45 deletions lib/xpath/html.rb
Expand Up @@ -7,12 +7,9 @@ module HTML
# #
# @param [String] locator # @param [String] locator
# Text, id, title, or image alt attribute of the link # Text, id, title, or image alt attribute of the link
# @option options [String] :href
# `href` attribute of the link
# #
def link(locator, options={}) def link(locator)
href = options[:href] link = descendant(:a)[attr(:href)]
link = descendant(:a)[href ? attr(:href).equals(href) : attr(:href)]
link[attr(:id).equals(locator) | string.n.is(locator) | attr(:title).is(locator) | descendant(:img)[attr(:alt).is(locator)]] link[attr(:id).equals(locator) | string.n.is(locator) | attr(:title).is(locator) | descendant(:img)[attr(:alt).is(locator)]]
end end


Expand Down Expand Up @@ -59,20 +56,10 @@ def fieldset(locator)
# #
# @param [String] locator # @param [String] locator
# Label, id, or name of field to match # Label, id, or name of field to match
# @option options [Bool] :checked #
# Match only if the input has a `checked` attribute def field(locator)
# @option options [Bool] :unchecked
# Match only if the input does not have a `checked` attribute
# @option options [String] :with
# Text that matches only elements with this value
# (`value` attribute or contained text)
#
def field(locator, options={})
xpath = descendant(:input, :textarea, :select)[~attr(:type).one_of('submit', 'image', 'hidden')] xpath = descendant(:input, :textarea, :select)[~attr(:type).one_of('submit', 'image', 'hidden')]
xpath = locate_field(xpath, locator) xpath = locate_field(xpath, locator)
xpath = xpath[attr(:checked)] if options[:checked]
xpath = xpath[~attr(:checked)] if options[:unchecked]
xpath = xpath[field_value(options[:with])] if options.has_key?(:with)
xpath xpath
end end


Expand All @@ -83,14 +70,10 @@ def field(locator, options={})
# #
# @param [String] locator # @param [String] locator
# Label, id, or name of field to match # Label, id, or name of field to match
# @option options [String] :with
# Text that matches only elements with this value
# (`value` attribute or contained text)
# #
def fillable_field(locator, options={}) def fillable_field(locator)
xpath = descendant(:input, :textarea)[~attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')] xpath = descendant(:input, :textarea)[~attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
xpath = locate_field(xpath, locator) xpath = locate_field(xpath, locator)
xpath = xpath[field_value(options[:with])] if options.has_key?(:with)
xpath xpath
end end


Expand All @@ -99,21 +82,9 @@ def fillable_field(locator, options={})
# #
# @param [String] locator # @param [String] locator
# Label, id, or name of the field to match # Label, id, or name of the field to match
# @option options [Array] :options
# @option options [Array] :selected
# #
def select(locator, options={}) def select(locator)
xpath = locate_field(descendant(:select), locator) locate_field(descendant(:select), locator)

options[:options].each do |option|
xpath = xpath[descendant(:option).equals(option)]
end if options[:options]

[options[:selected]].flatten.each do |option|
xpath = xpath[descendant(:option)[attr(:selected)].equals(option)]
end if options[:selected]

xpath
end end




Expand All @@ -122,7 +93,7 @@ def select(locator, options={})
# @param [String] locator # @param [String] locator
# Label, id, or name of the checkbox to match # Label, id, or name of the checkbox to match
# #
def checkbox(locator, options={}) def checkbox(locator)
xpath = locate_field(descendant(:input)[attr(:type).equals('checkbox')], locator) xpath = locate_field(descendant(:input)[attr(:type).equals('checkbox')], locator)
end end


Expand All @@ -132,7 +103,7 @@ def checkbox(locator, options={})
# @param [String] locator # @param [String] locator
# Label, id, or name of the radio button to match # Label, id, or name of the radio button to match
# #
def radio_button(locator, options={}) def radio_button(locator)
locate_field(descendant(:input)[attr(:type).equals('radio')], locator) locate_field(descendant(:input)[attr(:type).equals('radio')], locator)
end end


Expand All @@ -142,7 +113,7 @@ def radio_button(locator, options={})
# @param [String] locator # @param [String] locator
# Label, id, or name of the file field to match # Label, id, or name of the file field to match
# #
def file_field(locator, options={}) def file_field(locator)
locate_field(descendant(:input)[attr(:type).equals('file')], locator) locate_field(descendant(:input)[attr(:type).equals('file')], locator)
end end


Expand Down Expand Up @@ -174,7 +145,7 @@ def option(name)
# @option options [Array] :rows # @option options [Array] :rows
# Content of each cell in each row to match # Content of each cell in each row to match
# #
def table(locator, options={}) def table(locator)
descendant(:table)[attr(:id).equals(locator) | descendant(:caption).contains(locator)] descendant(:table)[attr(:id).equals(locator) | descendant(:caption).contains(locator)]
end end


Expand All @@ -184,10 +155,5 @@ def locate_field(xpath, locator)
locate_field = xpath[attr(:id).equals(locator) | attr(:name).equals(locator) | attr(:placeholder).equals(locator) | attr(:id).equals(anywhere(:label)[string.n.is(locator)].attr(:for))] locate_field = xpath[attr(:id).equals(locator) | attr(:name).equals(locator) | attr(:placeholder).equals(locator) | attr(:id).equals(anywhere(:label)[string.n.is(locator)].attr(:for))]
locate_field += descendant(:label)[string.n.is(locator)].descendant(xpath) locate_field += descendant(:label)[string.n.is(locator)].descendant(xpath)
end end

def field_value(value)
(string.n.is(value) & tag(:textarea)) | (attr(:value).equals(value) & ~tag(:textarea))
end

end end
end end
25 changes: 0 additions & 25 deletions spec/html_spec.rb
Expand Up @@ -33,8 +33,6 @@ def all(*args)
it("finds links by image's approximate alt attribute") { get('Alt').should == 'link-img' } it("finds links by image's approximate alt attribute") { get('Alt').should == 'link-img' }
it("prefers exact matches of image's alt attribute") { all('An image').should == ['link-img-exact', 'link-img-fuzzy'] } it("prefers exact matches of image's alt attribute") { all('An image').should == ['link-img-exact', 'link-img-fuzzy'] }
it("does not find links without href attriutes") { get('Wrong Link').should be_nil } it("does not find links without href attriutes") { get('Wrong Link').should be_nil }
it("finds links with an href") { get("Href-ed link", :href => 'http://www.example.com').should == 'link-href' }
it("does not find links with an incorrect href") { get("Href-ed link", :href => 'http://www.somewhere.com').should be_nil }
end end


describe '#button' do describe '#button' do
Expand Down Expand Up @@ -162,28 +160,6 @@ def all(*args)
it("does not find image buttons") { get('Input image with parent label').should be_nil } it("does not find image buttons") { get('Input image with parent label').should be_nil }
it("does not find hidden fields") { get('Input hidden with parent label').should be_nil } it("does not find hidden fields") { get('Input hidden with parent label').should be_nil }
end end

context "with :with option" do
it("finds inputs that match option") { get('input-with-id', :with => 'correct-value').should == 'input-with-id-data' }
it("omits inputs that don't match option") { get('input-with-id', :with => 'wrong-value').should be_nil }
it("finds textareas that match option") { get('textarea-with-id', :with => 'Correct value').should == 'textarea-with-id-data' }
it("omits textareas that don't match option") { get('textarea-with-id', :with => 'Wrong value').should be_nil }
end

context "with :checked option" do
context "when true" do
it("finds checked fields") {}
it("omits unchecked fields") {}
end
context "when false" do
it("finds unchecked fields") {}
it("omits checked fields") {}
end
context "when ommitted" do
it("finds unchecked fields") {}
it("finds checked fields") {}
end
end
end end


describe '#fillable_field' do describe '#fillable_field' do
Expand Down Expand Up @@ -242,7 +218,6 @@ def all(*args)
subject {:table} subject {:table}
it("finds tables by id") { get('table-with-id').should == 'table-with-id-data' } it("finds tables by id") { get('table-with-id').should == 'table-with-id-data' }
it("finds tables by caption") { get('Table with caption').should == 'table-with-caption-data' } it("finds tables by caption") { get('Table with caption').should == 'table-with-caption-data' }
it("finds cell content regardless of whitespace") { get('whitespaced-table', :rows => [["I have nested whitespace", "I don't"]]).should == 'table-with-whitespace' }
end end


end end

0 comments on commit b6074b3

Please sign in to comment.