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

Add system style filter

This commit is contained in:
Thomas Walpole 2018-12-02 16:07:40 -08:00
parent 2a247630d1
commit 2c8c8cb9f8
4 changed files with 61 additions and 3 deletions

View file

@ -4,7 +4,7 @@ module Capybara
module Queries
class SelectorQuery < Queries::BaseQuery
attr_reader :expression, :selector, :locator, :options
VALID_KEYS = COUNT_KEYS + %i[text id class visible exact exact_text normalize_ws match wait filter_set]
VALID_KEYS = COUNT_KEYS + %i[text id class style visible exact exact_text normalize_ws match wait filter_set]
VALID_MATCH = %i[first smart prefer_exact one].freeze
def initialize(*args,
@ -237,6 +237,7 @@ module Capybara
conditions = {}
conditions[:id] = options[:id] if use_default_id_filter?
conditions[:class] = options[:class] if use_default_class_filter?
conditions[:style] = options[:style] if use_default_style_filter? && !options[:style].is_a?(Hash)
builder(expr).add_attribute_conditions(conditions)
end
@ -248,6 +249,10 @@ module Capybara
options.key?(:class) && !custom_keys.include?(:class)
end
def use_default_style_filter?
options.key?(:style) && !custom_keys.include?(:style)
end
def apply_expression_filters(expression)
unapplied_options = options.keys - valid_keys
expression_filters.inject(expression) do |expr, (name, ef)|
@ -300,6 +305,7 @@ module Capybara
matches_id_filter?(node) &&
matches_class_filter?(node) &&
matches_style_filter?(node) &&
matches_text_filter?(node) &&
matches_exact_text_filter?(node) &&
matches_visible_filter?(node)
@ -317,6 +323,28 @@ module Capybara
node[:class] =~ options[:class]
end
def matches_style_filter?(node)
case options[:style]
when String, nil
true
when Regexp
node[:style] =~ options[:style]
when Hash
matches_style?(node, options[:style])
end
end
def matches_style?(node, styles)
@actual_styles = node.style(*styles.keys)
styles.all? do |style, value|
if value.is_a? Regexp
@actual_styles[style.to_s] =~ value
else
@actual_styles[style.to_s] == value
end
end
end
def matches_text_filter?(node)
value = options[:text]
return true unless value

View file

@ -5,7 +5,7 @@ Capybara::SpecHelper.spec '#click_button' do
@session.visit('/form')
end
it 'should wait for asynchronous load', :focus_, requires: [:js] do
it 'should wait for asynchronous load', requires: [:js] do
@session.visit('/with_js')
@session.using_wait_time(1.5) do
@session.click_link('Click me')

View file

@ -38,6 +38,20 @@ Capybara::SpecHelper.spec '#has_css?' do
expect(@session).to have_css('li', class: /.*/)
end
context ':style option' do
it 'should support String' do
expect(@session).to have_css('p', style: 'line-height: 25px;')
end
it 'should support Regexp' do
expect(@session).to have_css('p', style: /-height: 2/)
end
it 'should support Hash', requires: [:css] do
expect(@session).to have_css('p', style: { 'line-height': '25px' })
end
end
it 'should support case insensitive :class and :id options' do
expect(@session).to have_css('li', class: /UiTaRI/i)
expect(@session).to have_css('h2', id: /2ON/i)
@ -118,6 +132,12 @@ Capybara::SpecHelper.spec '#has_css?' do
expect(@session).to have_css('li', class: /guitar|drummer/, count: 4)
expect(@session).to have_css('li', id: /john|paul/, class: /guitar|drummer/, count: 2)
expect(@session).to have_css('li', class: %w[beatle guitarist], count: 2)
expect(@session).to have_css('p', style: 'line-height: 25px;', count: 1)
expect(@session).to have_css('p', style: /-height: 2/, count: 1)
end
it 'should be true if the content occurs the given number of times in CSS processing drivers', requires: [:css] do
expect(@session).to have_css('p', style: { 'line-height': '25px' }, count: 1)
end
it 'should be false if the content occurs a different number of times than the given' do

View file

@ -31,7 +31,7 @@ RSpec.describe Capybara do
<input type="file" id="file" class=".special file"/>
<input type="hidden" id="hidden_field" value="this is hidden"/>
<input type="submit" value="click me" title="submit button"/>
<input type="button" value="don't click me" title="Other button 1"/>
<input type="button" value="don't click me" title="Other button 1" style="line-height: 30px;"/>
<a href="#">link</a>
<fieldset></fieldset>
<select id="select">
@ -304,6 +304,16 @@ RSpec.describe Capybara do
end
end
context 'with :style option' do
it 'accepts string for CSS based selectors' do
expect(string.find(:custom_css_selector, 'input', style: 'line-height: 30px;')[:title]).to eq 'Other button 1'
end
it 'accepts Regexp for CSS base selectors' do
expect(string.find(:custom_css_selector, 'input', style: /30px/)[:title]).to eq 'Other button 1'
end
end
# :css, :xpath, :id, :field, :fieldset, :link, :button, :link_or_button, :fillable_field, :radio_button, :checkbox, :select,
# :option, :file_field, :label, :table, :frame