2018-11-27 20:52:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-02-25 18:50:24 -05:00
|
|
|
# rubocop:disable RSpec/InstanceVariable
|
2018-11-27 20:52:35 -05:00
|
|
|
RSpec.describe Capybara::Selector::XPathBuilder do
|
|
|
|
let :builder do
|
2018-11-28 14:39:58 -05:00
|
|
|
::Capybara::Selector::XPathBuilder.new(@xpath)
|
2018-11-27 20:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'add_attribute_conditions' do
|
|
|
|
it 'adds a single string condition to a single selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/div'
|
|
|
|
selector = builder.add_attribute_conditions(random: 'abc')
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq %((.//div)[(./@random = 'abc')])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds multiple string conditions to a single selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/div'
|
|
|
|
selector = builder.add_attribute_conditions(random: 'abc', other: 'def')
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq %(((.//div)[(./@random = 'abc')])[(./@other = 'def')])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a single string condition to a multiple selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:div, :ul)
|
|
|
|
selector = builder.add_attribute_conditions(random: 'abc')
|
|
|
|
expect(selector.to_s).to eq @xpath[XPath.attr(:random) == 'abc'].to_s
|
2018-11-27 20:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds multiple string conditions to a multiple selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:div, :ul)
|
|
|
|
selector = builder.add_attribute_conditions(random: 'abc', other: 'def')
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector.to_s).to eq %(.//*[self::div | self::ul][(./@random = 'abc')][(./@other = 'def')])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds simple regexp conditions to a single selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:div)
|
|
|
|
selector = builder.add_attribute_conditions(random: /abc/, other: /def/)
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector.to_s).to eq %(.//div[./@random[contains(., 'abc')]][./@other[contains(., 'def')]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds wildcard regexp conditions to a single selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/div'
|
|
|
|
selector = builder.add_attribute_conditions(random: /abc.*def/, other: /def.*ghi/)
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq %(((.//div)[./@random[(contains(., 'abc') and contains(., 'def'))]])[./@other[(contains(., 'def') and contains(., 'ghi'))]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds alternated regexp conditions to a single selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:div)
|
|
|
|
selector = builder.add_attribute_conditions(random: /abc|def/, other: /def|ghi/)
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector.to_s).to eq %(.//div[./@random[(contains(., 'abc') or contains(., 'def'))]][./@other[(contains(., 'def') or contains(., 'ghi'))]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds alternated regexp conditions to a multiple selector' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:div, :ul)
|
|
|
|
selector = builder.add_attribute_conditions(other: /def.*ghi|jkl/)
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector.to_s).to eq %(.//*[self::div | self::ul][./@other[((contains(., 'def') and contains(., 'ghi')) or contains(., 'jkl'))]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns original selector when regexp can't be substringed" do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/div'
|
|
|
|
selector = builder.add_attribute_conditions(other: /.+/)
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq '(.//div)[./@other]'
|
|
|
|
end
|
|
|
|
|
|
|
|
context ':class' do
|
|
|
|
it 'handles string' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/a'
|
|
|
|
selector = builder.add_attribute_conditions(class: 'my_class')
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq %((.//a)[contains(concat(' ', normalize-space(./@class), ' '), ' my_class ')])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles negated strings' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:a)
|
|
|
|
selector = builder.add_attribute_conditions(class: '!my_class')
|
|
|
|
expect(selector.to_s).to eq @xpath[!XPath.attr(:class).contains_word('my_class')].to_s
|
2018-11-27 20:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles array of strings' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = './/a'
|
|
|
|
selector = builder.add_attribute_conditions(class: %w[my_class my_other_class])
|
2018-11-27 20:52:35 -05:00
|
|
|
expect(selector).to eq %((.//a)[(contains(concat(' ', normalize-space(./@class), ' '), ' my_class ') and contains(concat(' ', normalize-space(./@class), ' '), ' my_other_class '))])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles array of string when negated included' do
|
2018-11-28 14:39:58 -05:00
|
|
|
@xpath = XPath.descendant(:a)
|
|
|
|
selector = builder.add_attribute_conditions(class: %w[my_class !my_other_class])
|
|
|
|
expect(selector.to_s).to eq @xpath[XPath.attr(:class).contains_word('my_class') & !XPath.attr(:class).contains_word('my_other_class')].to_s
|
2018-11-27 20:52:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-25 18:50:24 -05:00
|
|
|
# rubocop:enable RSpec/InstanceVariable
|