Class: Capybara::Result
- Inherits:
-
Object
- Object
- Capybara::Result
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/capybara/result.rb
Instance Method Summary collapse
- #failure_message ⇒ Object
- #find! ⇒ Object
- #find_error ⇒ Object
-
#initialize(elements, query) ⇒ Result
constructor
A new instance of Result.
- #matches_count? ⇒ Boolean
- #negative_failure_message ⇒ Object
Constructor Details
#initialize(elements, query) ⇒ Result
Returns a new instance of Result.
8 9 10 11 12 13 |
# File 'lib/capybara/result.rb', line 8 def initialize(elements, query) @elements = elements @result = elements.select { |node| query.matches_filters?(node) } @rest = @elements - @result @query = query end |
Instance Method Details
#failure_message ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/capybara/result.rb', line 34 def = if @query.[:count] "expected #{@query.description} to be found #{@query.[:count]} #{declension("time", "times", @query.[:count])}" elsif @query.[:between] "expected #{@query.description} to be found between #{@query.[:between].first} and #{@query.[:between].last} times" elsif @query.[:maximum] "expected #{@query.description} to be found at most #{@query.[:maximum]} #{declension("time", "times", @query.[:maximum])}" elsif @query.[:minimum] "expected #{@query.description} to be found at least #{@query.[:minimum]} #{declension("time", "times", @query.[:minimum])}" else "expected to find #{@query.description}" end if count > 0 << ", found #{count} #{declension("match", "matches")}: " << @result.map(&:text).map(&:inspect).join(", ") else << " but there were no matches" end unless @rest.empty? elements = @rest.map(&:text).map(&:inspect).join(", ") << ". Also found " << elements << ", which matched the selector but not all filters." end end |
#find! ⇒ Object
21 22 23 24 |
# File 'lib/capybara/result.rb', line 21 def find! raise find_error if @result.size != 1 @result.first end |
#find_error ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/capybara/result.rb', line 26 def find_error if @result.size == 0 Capybara::ElementNotFound.new("Unable to find #{@query.description}") elsif @result.size > 1 Capybara::Ambiguous.new("Ambiguous match, found #{size} elements matching #{@query.description}") end end |
#matches_count? ⇒ Boolean
17 18 19 |
# File 'lib/capybara/result.rb', line 17 def matches_count? @query.matches_count?(@result.size) end |
#negative_failure_message ⇒ Object
58 59 60 |
# File 'lib/capybara/result.rb', line 58 def "expected not to find #{@query.description}, but there #{declension("was", "were")} #{count} #{declension("match", "matches")}" end |