-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move css/xpath attribute requirement generation into the builders
- Loading branch information
Showing
6 changed files
with
237 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Capybara::Selector::CSSBuilder do | ||
let :builder do | ||
::Capybara::Selector::CSSBuilder | ||
end | ||
|
||
context 'add_attribute_conditions' do | ||
it 'adds a single string condition to a single selector' do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, random: 'abc') | ||
expect(selector).to eq %(div[random='abc']) | ||
end | ||
|
||
it 'adds multiple string conditions to a single selector' do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, random: 'abc', other: 'def') | ||
expect(selector).to eq %(div[random='abc'][other='def']) | ||
end | ||
|
||
it 'adds a single string condition to a multiple selector' do | ||
css = 'div, ul' | ||
selector = builder.add_attribute_conditions(css, random: 'abc') | ||
expect(selector).to eq %(div[random='abc'], ul[random='abc']) | ||
end | ||
|
||
it 'adds multiple string conditions to a multiple selector' do | ||
css = 'div, ul' | ||
selector = builder.add_attribute_conditions(css, random: 'abc', other: 'def') | ||
expect(selector).to eq %(div[random='abc'][other='def'], ul[random='abc'][other='def']) | ||
end | ||
|
||
it 'adds simple regexp conditions to a single selector' do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, random: /abc/, other: /def/) | ||
expect(selector).to eq %(div[random*='abc'][other*='def']) | ||
end | ||
|
||
it 'adds wildcard regexp conditions to a single selector' do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, random: /abc.*def/, other: /def.*ghi/) | ||
expect(selector).to eq %(div[random*='abc'][random*='def'][other*='def'][other*='ghi']) | ||
end | ||
|
||
it 'adds alternated regexp conditions to a single selector' do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, random: /abc|def/, other: /def|ghi/) | ||
expect(selector).to eq %(div[random*='abc'][other*='def'], div[random*='abc'][other*='ghi'], div[random*='def'][other*='def'], div[random*='def'][other*='ghi']) | ||
end | ||
|
||
it 'adds alternated regexp conditions to a multiple selector' do | ||
css = 'div,ul' | ||
selector = builder.add_attribute_conditions(css, other: /def.*ghi|jkl/) | ||
expect(selector).to eq %(div[other*='def'][other*='ghi'], div[other*='jkl'], ul[other*='def'][other*='ghi'], ul[other*='jkl']) | ||
end | ||
|
||
it "returns original selector when regexp can't be substringed" do | ||
css = 'div' | ||
selector = builder.add_attribute_conditions(css, other: /.+/) | ||
expect(selector).to eq 'div' | ||
end | ||
|
||
context ':class' do | ||
it 'handles string with CSS .' do | ||
css = 'a' | ||
selector = builder.add_attribute_conditions(css, class: 'my_class') | ||
expect(selector).to eq 'a.my_class' | ||
end | ||
|
||
it 'handles negated string with CSS .' do | ||
css = 'a' | ||
selector = builder.add_attribute_conditions(css, class: '!my_class') | ||
expect(selector).to eq 'a:not(.my_class)' | ||
end | ||
|
||
it 'handles array of string with CSS .' do | ||
css = 'a' | ||
selector = builder.add_attribute_conditions(css, class: %w[my_class my_other_class]) | ||
expect(selector).to eq 'a.my_class.my_other_class' | ||
end | ||
|
||
it 'handles array of string with CSS . when negated included' do | ||
css = 'a' | ||
selector = builder.add_attribute_conditions(css, class: %w[my_class !my_other_class]) | ||
expect(selector).to eq 'a.my_class:not(.my_other_class)' | ||
end | ||
end | ||
|
||
context ':id' do | ||
it 'handles string with CSS #' do | ||
css = 'ul' | ||
selector = builder.add_attribute_conditions(css, id: 'my_id') | ||
expect(selector).to eq 'ul#my_id' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Capybara::Selector::XPathBuilder do | ||
let :builder do | ||
::Capybara::Selector::XPathBuilder | ||
end | ||
|
||
context 'add_attribute_conditions' do | ||
it 'adds a single string condition to a single selector' do | ||
xpath = './/div' | ||
selector = builder.add_attribute_conditions(xpath, random: 'abc') | ||
expect(selector).to eq %((.//div)[(./@random = 'abc')]) | ||
end | ||
|
||
it 'adds multiple string conditions to a single selector' do | ||
xpath = './/div' | ||
selector = builder.add_attribute_conditions(xpath, random: 'abc', other: 'def') | ||
expect(selector).to eq %(((.//div)[(./@random = 'abc')])[(./@other = 'def')]) | ||
end | ||
|
||
it 'adds a single string condition to a multiple selector' do | ||
xpath = XPath.descendant(:div, :ul) | ||
selector = builder.add_attribute_conditions(xpath, random: 'abc') | ||
expect(selector.to_s).to eq xpath[XPath.attr(:random) == 'abc'].to_s | ||
end | ||
|
||
it 'adds multiple string conditions to a multiple selector' do | ||
xpath = XPath.descendant(:div, :ul) | ||
selector = builder.add_attribute_conditions(xpath, random: 'abc', other: 'def') | ||
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 | ||
xpath = XPath.descendant(:div) | ||
selector = builder.add_attribute_conditions(xpath, random: /abc/, other: /def/) | ||
expect(selector.to_s).to eq %(.//div[./@random[contains(., 'abc')]][./@other[contains(., 'def')]]) | ||
end | ||
|
||
it 'adds wildcard regexp conditions to a single selector' do | ||
xpath = './/div' | ||
selector = builder.add_attribute_conditions(xpath, random: /abc.*def/, other: /def.*ghi/) | ||
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 | ||
xpath = XPath.descendant(:div) | ||
selector = builder.add_attribute_conditions(xpath, random: /abc|def/, other: /def|ghi/) | ||
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 | ||
xpath = XPath.descendant(:div, :ul) | ||
selector = builder.add_attribute_conditions(xpath, other: /def.*ghi|jkl/) | ||
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 | ||
xpath = './/div' | ||
selector = builder.add_attribute_conditions(xpath, other: /.+/) | ||
expect(selector).to eq '(.//div)[./@other]' | ||
end | ||
|
||
context ':class' do | ||
it 'handles string' do | ||
xpath = './/a' | ||
selector = builder.add_attribute_conditions(xpath, class: 'my_class') | ||
expect(selector).to eq %((.//a)[contains(concat(' ', normalize-space(./@class), ' '), ' my_class ')]) | ||
end | ||
|
||
it 'handles negated strings' do | ||
xpath = XPath.descendant(:a) | ||
selector = builder.add_attribute_conditions(xpath, class: '!my_class') | ||
expect(selector.to_s).to eq xpath[!XPath.attr(:class).contains_word('my_class')].to_s | ||
end | ||
|
||
it 'handles array of strings' do | ||
xpath = './/a' | ||
selector = builder.add_attribute_conditions(xpath, class: %w[my_class my_other_class]) | ||
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 | ||
xpath = XPath.descendant(:a) | ||
selector = builder.add_attribute_conditions(xpath, 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 | ||
end | ||
end | ||
end | ||
end |