forked from teamcapybara/capybara
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request teamcapybara#2055 from teamcapybara/css_style
Add Element method for getting specific CSS styles
- Loading branch information
Showing
21 changed files
with
265 additions
and
7 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
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Capybara | ||
# @api private | ||
module Queries | ||
class StyleQuery < BaseQuery | ||
def initialize(expected_styles, session_options:, **options) | ||
@expected_styles = expected_styles.each_with_object({}) { |(style, value), str_keys| str_keys[style.to_s] = value } | ||
@options = options | ||
@actual_styles = {} | ||
super(@options) | ||
self.session_options = session_options | ||
|
||
assert_valid_keys | ||
end | ||
|
||
def resolves_for?(node) | ||
@node = node | ||
@actual_styles = node.style(*@expected_styles.keys) | ||
@expected_styles.all? do |style, value| | ||
if value.is_a? Regexp | ||
@actual_styles[style] =~ value | ||
else | ||
@actual_styles[style] == value | ||
end | ||
end | ||
end | ||
|
||
def failure_message | ||
+"Expected node to have styles #{@expected_styles.inspect}. " \ | ||
"Actual styles were #{@actual_styles.inspect}" | ||
end | ||
|
||
private | ||
|
||
def valid_keys | ||
%i[wait] | ||
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
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
Capybara::SpecHelper.spec '#assert_style', requires: [:css] do | ||
it "should not raise if the elements style contains the given properties" do | ||
@session.visit('/with_html') | ||
expect do | ||
@session.find(:css, '#first').assert_style(display: 'block') | ||
end.not_to raise_error | ||
end | ||
|
||
it "should raise error if the elements style doesn't contain the given properties" do | ||
@session.visit('/with_html') | ||
expect do | ||
@session.find(:css, '#first').assert_style(display: 'inline') | ||
end.to raise_error(Capybara::ExpectationNotMet, 'Expected node to have styles {"display"=>"inline"}. Actual styles were {"display"=>"block"}') | ||
end | ||
|
||
it "should wait for style", requires: %i[css js] do | ||
@session.visit('/with_js') | ||
el = @session.find(:css, '#change') | ||
@session.click_link("Change size") | ||
expect do | ||
el.assert_style({ 'font-size': '50px' }, wait: 3) | ||
end.not_to raise_error | ||
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
Capybara::SpecHelper.spec '#has_style?', requires: [:css] do | ||
before do | ||
@session.visit('/with_html') | ||
end | ||
|
||
it "should be true if the element has the given style" do | ||
expect(@session.find(:css, '#first')).to have_style(display: 'block') | ||
expect(@session.find(:css, '#first').has_style?(display: 'block')).to be true | ||
expect(@session.find(:css, '#second')).to have_style('display' => 'inline') | ||
expect(@session.find(:css, '#second').has_style?('display' => 'inline')).to be true | ||
end | ||
|
||
it "should be false if the element does not have the given style" do | ||
expect(@session.find(:css, '#first').has_style?('display' => 'inline')).to be false | ||
expect(@session.find(:css, '#second').has_style?(display: 'block')).to be false | ||
end | ||
|
||
it "allows Regexp for value matching" do | ||
expect(@session.find(:css, '#first')).to have_style(display: /^bl/) | ||
expect(@session.find(:css, '#first').has_style?('display' => /^bl/)).to be true | ||
expect(@session.find(:css, '#first').has_style?(display: /^in/)).to be false | ||
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
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
Oops, something went wrong.