2016-03-08 00:52:19 +00:00
# frozen_string_literal: true
2016-01-07 01:11:00 +00:00
require 'spec_helper'
RSpec . describe Capybara do
describe 'Selectors' do
let :string do
Capybara . string <<-STRING
< html >
< head >
< title > selectors < / title>
< / head>
< body >
< div class = " a " id = " page " >
< div class = " b " id = " content " >
< h1 class = " a " > Totally awesome < / h1>
< p > Yes it is < / p>
< / div>
< p class = " b " > Some Content < / p>
< p class = " b " > < / p>
< / div>
2016-03-24 18:18:12 +00:00
< input type = " checkbox " / >
< input type = " radio " / >
< input type = " text " / >
< input type = " file " / >
< a href = " # " > link < / a>
< fieldset > < / fieldset>
< select >
< option value = " a " > A < / option>
< / select>
< table >
< tr > < td > < / td>< / tr >
< / table
2016-01-07 01:11:00 +00:00
< / body>
< / html>
STRING
end
before do
Capybara . add_selector :custom_selector do
css { | css_class | " div. #{ css_class } " }
filter ( :not_empty , boolean : true , default : true , skip_if : :all ) { | node , value | value ^ ( node . text == '' ) }
end
end
2016-02-01 17:03:47 +00:00
describe " modify_selector " do
2016-01-07 01:11:00 +00:00
it " allows modifying a selector " do
el = string . find ( :custom_selector , 'a' )
expect ( el . tag_name ) . to eq 'div'
Capybara . modify_selector :custom_selector do
css { | css_class | " h1. #{ css_class } " }
end
el = string . find ( : custom_selector , 'a' )
expect ( el . tag_name ) . to eq 'h1'
end
it " doesn't change existing filters " do
Capybara . modify_selector :custom_selector do
css { | css_class | " p. #{ css_class } " }
end
expect ( string ) . to have_selector ( :custom_selector , 'b' , count : 1 )
expect ( string ) . to have_selector ( :custom_selector , 'b' , not_empty : false , count : 1 )
expect ( string ) . to have_selector ( :custom_selector , 'b' , not_empty : :all , count : 2 )
end
end
2016-03-24 18:18:12 +00:00
describe " builtin selectors with nil locators " do
it " devolves to just finding element types " do
selectors = {
field : " .//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')] " ,
fieldset : " .//fieldset " ,
link : " .//a[./@href] " ,
link_or_button : " .//a[./@href] | .//input[./@type = 'submit' or ./@type = 'reset' or ./@type = 'image' or ./@type = 'button'] | .//button " ,
fillable_field : " .//*[self::input | self::textarea][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'radio' or ./@type = 'checkbox' or ./@type = 'hidden' or ./@type = 'file')] " ,
radio_button : " .//input[./@type = 'radio'] " ,
checkbox : " .//input[./@type = 'checkbox'] " ,
select : " .//select " ,
option : " .//option " ,
file_field : " .//input[./@type = 'file'] " ,
table : " .//table "
}
selectors . each do | selector , xpath |
results = string . all ( selector , nil ) . to_a . map & :native
expect ( results . size ) . to be > 0
expect ( results ) . to eq string . all ( :xpath , xpath ) . to_a . map ( & :native )
end
end
end
2016-01-07 01:11:00 +00:00
end
end