1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

move selector description to the selector object

This commit is contained in:
Thomas Walpole 2014-07-02 11:17:53 -07:00
parent 01dd683c04
commit 0a007820b1
4 changed files with 83 additions and 19 deletions

View file

@ -34,7 +34,7 @@ module Capybara
def description
@description = "#{label} #{locator.inspect}"
@description << " with text #{options[:text].inspect}" if options[:text]
@description << " with value #{options[:with].inspect}" if options[:with]
@description << selector.description(options)
@description
end

View file

@ -5,6 +5,7 @@ module Capybara
@name = name
@block = block
@options = options
@options[:valid_values] = [true,false] if options[:boolean]
end
def default?
@ -16,6 +17,9 @@ module Capybara
end
def matches?(node, value)
if @options.has_key?(:valid_values) && !Array(@options[:valid_values]).include?(value)
warn "Invalid value #{value.inspect} passed to filter #{@name}"
end
@block.call(node, value)
end
end
@ -42,6 +46,7 @@ module Capybara
@match = nil
@label = nil
@failure_message = nil
@description = nil
instance_eval(&block)
end
@ -68,6 +73,10 @@ module Capybara
@label
end
def description(options={})
(@description && @description.call(options)).to_s
end
def call(locator)
if @format==:css
@css.call(locator)
@ -83,6 +92,10 @@ module Capybara
def filter(name, options={}, &block)
@custom_filters[name] = Filter.new(name, block, options)
end
def describe &block
@description = block
end
end
end
@ -100,9 +113,9 @@ end
Capybara.add_selector(:field) do
xpath { |locator| XPath::HTML.field(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:checked, boolean: true) { |node, value| not(value ^ node.checked?) }
filter(:unchecked, boolean: true) { |node, value| (value ^ node.checked?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
filter(:with) { |node, with| node.value == with.to_s }
filter(:type) do |node, type|
if ['textarea', 'select'].include?(type)
@ -111,6 +124,16 @@ Capybara.add_selector(:field) do
node[:type] == type
end
end
describe do |options|
desc, states = "", []
desc << " of type #{options[:type].inspect}" if options[:type]
desc << " with value #{options[:with].to_s.inspect}" if options.has_key?(:with)
states << 'checked' if options[:checked] || (options.has_key?(:unchecked) && !options[:unchecked])
states << 'not checked' if options[:unchecked] || (options.has_key?(:checked) && !options[:checked])
states << 'disabled' if options[:disabled]
desc << " that is #{states.join(' and ')}" unless states.empty?
desc
end
end
Capybara.add_selector(:fieldset) do
@ -120,7 +143,8 @@ end
Capybara.add_selector(:link_or_button) do
label "link or button"
xpath { |locator| XPath::HTML.link_or_button(locator) }
filter(:disabled, :default => false) { |node, value| node.tag_name == "a" or not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| node.tag_name == "a" or not(value ^ node.disabled?) }
describe { |options| " that is disabled" if options[:disabled] }
end
Capybara.add_selector(:link) do
@ -128,34 +152,55 @@ Capybara.add_selector(:link) do
filter(:href) do |node, href|
node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href.to_s)])
end
describe { |options| " with href #{options[:href].inspect}" if options[:href] }
end
Capybara.add_selector(:button) do
xpath { |locator| XPath::HTML.button(locator) }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe { |options| " that is disabled" if options[:disabled] }
end
Capybara.add_selector(:fillable_field) do
label "field"
xpath { |locator| XPath::HTML.fillable_field(locator) }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe { |options| " that is disabled" if options[:disabled] }
end
Capybara.add_selector(:radio_button) do
label "radio button"
xpath { |locator| XPath::HTML.radio_button(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
filter(:checked, boolean: true) { |node, value| not(value ^ node.checked?) }
filter(:unchecked, boolean: true) { |node, value| (value ^ node.checked?) }
filter(:option) { |node, value| node.value == value.to_s }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe do |options|
desc, states = "", []
desc << " with value #{options[:option].inspect}" if options[:option]
states << 'checked' if options[:checked] || (options.has_key?(:unchecked) && !options[:unchecked])
states << 'not checked' if options[:unchecked] || (options.has_key?(:checked) && !options[:checked])
states << 'disabled' if options[:disabled]
desc << " that is #{states.join(' and ')}" unless states.empty?
desc
end
end
Capybara.add_selector(:checkbox) do
xpath { |locator| XPath::HTML.checkbox(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
filter(:checked, boolean: true) { |node, value| not(value ^ node.checked?) }
filter(:unchecked, boolean: true) { |node, value| (value ^ node.checked?) }
filter(:option) { |node, value| node.value == value.to_s }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe do |options|
desc, states = "", []
desc << " with value #{options[:option].inspect}" if options[:option]
states << 'checked' if options[:checked] || (options.has_key?(:unchecked) && !options[:unchecked])
states << 'not checked' if options[:unchecked] || (options.has_key?(:checked) && !options[:checked])
states << 'disabled' if options[:disabled]
desc << " that is #{states.join(' and ')}" unless states.empty?
desc
end
end
Capybara.add_selector(:select) do
@ -170,7 +215,15 @@ Capybara.add_selector(:select) do
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
[selected].flatten.sort == actual.sort
end
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe do |options|
desc = ""
desc << " with options #{options[:options].inspect}" if options[:options]
desc << " with at least options #{options[:with_options].inspect}" if options[:with_options]
desc << " with #{options[:selected].inspect} selected" if options[:selected]
desc << " that is disabled" if options[:disabled]
desc
end
end
Capybara.add_selector(:option) do
@ -180,7 +233,8 @@ end
Capybara.add_selector(:file_field) do
label "file field"
xpath { |locator| XPath::HTML.file_field(locator) }
filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) }
filter(:disabled, default: false, boolean: true) { |node, value| not(value ^ node.disabled?) }
describe { |options| " that is disabled" if options[:disabled] }
end
Capybara.add_selector(:table) do

View file

@ -19,6 +19,12 @@ Capybara::SpecHelper.spec '#find_field' do
end.to raise_error(Capybara::ElementNotFound)
end
it "should warn if filter option is invalid" do
expect_any_instance_of(Kernel).to receive(:warn).
with('Invalid value nil passed to filter disabled')
@session.find_field('Dog', disabled: nil)
end
it "should be aliased as 'field_labeled' for webrat compatibility" do
expect(@session.field_labeled('Dog').value).to eq('dog')
expect do

View file

@ -597,7 +597,7 @@ RSpec.describe Capybara::RSpecMatchers do
it "gives proper description for a given value" do
expect(have_field('Text field', with: 'some value').description).to eq("have field \"Text field\" with value \"some value\"")
end
it "passes if there is such a field" do
expect(html).to have_field('Text field')
end
@ -639,9 +639,9 @@ RSpec.describe Capybara::RSpecMatchers do
end
it "gives proper description" do
expect(have_checked_field('it is checked').description).to eq("have field \"it is checked\"")
expect(have_checked_field('it is checked').description).to eq("have field \"it is checked\" that is checked")
end
context "with should" do
it "passes if there is such a field and it is checked" do
expect(html).to have_checked_field('it is checked')
@ -688,7 +688,7 @@ RSpec.describe Capybara::RSpecMatchers do
end
it "gives proper description" do
expect(have_unchecked_field('unchecked field').description).to eq("have field \"unchecked field\"")
expect(have_unchecked_field('unchecked field').description).to eq("have field \"unchecked field\" that is not checked")
end
context "with should" do
@ -736,6 +736,10 @@ RSpec.describe Capybara::RSpecMatchers do
it "gives proper description" do
expect(have_select('Select Box').description).to eq("have select box \"Select Box\"")
end
it "gives proper description for a given selected value" do
expect(have_select('Select Box', selected: 'some value').description).to eq("have select box \"Select Box\" with \"some value\" selected")
end
it "passes if there is such a select" do
expect(html).to have_select('Select Box')