Class: Capybara::Harness::Dom::Reader

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/capybara/harness/dom/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReader

Returns a new instance of Reader.



7
8
9
# File 'lib/capybara/harness/dom/reader.rb', line 7

def initialize
  self.attributes = {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/capybara/harness/dom/reader.rb', line 5

def attributes
  @attributes
end

#finder_attr_nameObject

Returns the value of attribute finder_attr_name.



5
6
7
# File 'lib/capybara/harness/dom/reader.rb', line 5

def finder_attr_name
  @finder_attr_name
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/capybara/harness/dom/reader.rb', line 5

def name
  @name
end

Instance Method Details

#attribute(name, options = {}, &block) ⇒ Object



11
12
13
14
15
# File 'lib/capybara/harness/dom/reader.rb', line 11

def attribute(name, options = {}, &block)
  self.finder_attr_name = name.to_sym if options.delete(:finder)
  options.merge!(:derived_value_block => block)
  attributes[name] = Capybara::Harness::Dom::Attribute.new(name, options)
end

#find_element(values = {}) ⇒ Object

Public: Scans the DOM and finds the node that represents the subject.



18
19
20
# File 'lib/capybara/harness/dom/reader.rb', line 18

def find_element(values = {})
  page.find(".#{name} .#{finder_attr_name}", :visible => true, :text => finder_attr.derive_value(values)).find(:xpath, ".//ancestor::*[@class='#{name}']")
end

#find_element_through_list(values = {}) ⇒ Object



27
28
29
# File 'lib/capybara/harness/dom/reader.rb', line 27

def find_element_through_list(values = {})
  find_list.find_element(values)
end

#find_listObject

Public: Scans the DOM and finds the node that represents the subject’s list element.



23
24
25
# File 'lib/capybara/harness/dom/reader.rb', line 23

def find_list
  find("##{selector.to_s.pluralize}")
end

#first_in_the_list?Boolean

Public: Returns true if the subject’s element is located first in its list.

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/capybara/harness/dom/reader.rb', line 38

def first_in_the_list?
  node = list.first(".#{selector}")
  return false if node.nil?
  has_attrs?(node)
end

#has_attrs?(values = {}) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capybara/harness/dom/reader.rb', line 51

def has_attrs?(values = {})
  return false unless page.has_css?(".#{name} .#{finder_attr_name}", :visible => true, :text => finder_attr.derive_value(values))

  node = find_element(values)
  attributes.each do |attr_name, attr|
    text = attr.derive_value(values)
    return false unless node.has_css?(".#{attr_name}", :visible => true, :text => text)
  end

  true
end

#in_the_list?Boolean

Public: Returns true if the subject’s element is in the page’s subject list.

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/capybara/harness/dom/reader.rb', line 32

def in_the_list?
  return false unless list.has_css?(".#{selector} .#{finder_attr_name}", :text => finder_attr)
  has_attrs?(element(list))
end

#last_in_the_list?Boolean

Public: Returns true if the subject’s element is located last in its list.

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/capybara/harness/dom/reader.rb', line 45

def last_in_the_list?
  node = list.all(".#{selector}").last
  return false if node.nil?
  has_attrs?(node)
end