module Wrapybara::Element

Public Instance Methods

click() click to toggle source
# File lib/wrapybara/element.rb, line 37
def click
        element.click
end
default_how() click to toggle source
# File lib/wrapybara/element.rb, line 146
def default_how
        'labeled'
end
default_scope() click to toggle source
# File lib/wrapybara/element.rb, line 142
def default_scope
        nil
end
disabled?() click to toggle source
# File lib/wrapybara/element.rb, line 75
def disabled?
        self.should_exist
        ['true', 'disabled'].include?(@element[:disabled])
end
element() click to toggle source
# File lib/wrapybara/element.rb, line 25
def element
        @element
end
element_identifier() click to toggle source
# File lib/wrapybara/element.rb, line 133
def element_identifier
        "#{@how} '#{@identifier}'#{self.within(@scope)}"
end
enabled?() click to toggle source
# File lib/wrapybara/element.rb, line 71
def enabled?
        !disabled?
end
exists?() click to toggle source
# File lib/wrapybara/element.rb, line 41
def exists?
        !@element.nil?
end
focused?() click to toggle source
# File lib/wrapybara/ext/focus_tracking.rb, line 30
def focused?
        self.should_exist
        element[Wrapybara.focus_attribute] == Wrapybara.focus_value
end
get_element(xpath, scope) click to toggle source
# File lib/wrapybara/element.rb, line 3
def get_element(xpath, scope)
        if scope
                # if scope starts with '/html', let's assume (for now) that it's already an xpath
                unless scope =~ /^\/html/
                        # if there are multiple scopes (ie, "div1 div2 para1"), the following makes
                        # xpath look like "id('div1')/*[@id='div2']/*[@id='para1']"

                        scope = scope.split(' ')
                        id = scope.shift
                        scope = "id('#{id}')" + scope.inject('') { |output, s| output += "//*[@id='#{s}']" }
                end

                # xpath might come in like //something, and since we would be looking for an element
                # in the scope of another element, we need to make sure we aren't looking at the whole
                # document by adding the dot at the beginning
                xpath = ".#{xpath}" if xpath[0..1] == '//'
                Capybara.find(scope).find(xpath) rescue nil
        else
                Capybara.find(xpath) rescue nil
        end
end
parent_identifier() click to toggle source
# File lib/wrapybara/element.rb, line 137
def parent_identifier
        return '' unless @parent
        "#{@parent.how} '#{@parent.identifier}'#{self.within(@parent.scope)}"
end
path() click to toggle source
# File lib/wrapybara/element.rb, line 29
def path
        element.path
end
should_be_disabled() click to toggle source
# File lib/wrapybara/element.rb, line 90
def should_be_disabled
        message = "Expected element #{self.element_identifier} to be disabled"
        raise UnmetExpectation, message unless self.disabled?
end
should_be_enabled() click to toggle source
# File lib/wrapybara/element.rb, line 80
def should_be_enabled
        message = "Expected element #{self.element_identifier} to be enabled"
        raise UnmetExpectation, message unless self.enabled?
end
should_be_focused() click to toggle source
# File lib/wrapybara/ext/focus_tracking.rb, line 35
def should_be_focused
        message = "Expected element #{self.element_identifier} to be focused"
        raise UnmetExpectation, message unless self.focused?
end
should_be_visible() click to toggle source
# File lib/wrapybara/element.rb, line 119
def should_be_visible
        message = "Expected content #{self.element_identifier} to be visible"
        raise UnmetExpectation, message unless self.visible?
end
should_exist(message = nil) click to toggle source
# File lib/wrapybara/element.rb, line 45
def should_exist(message = nil)
        message ||= "Expected element #{self.element_identifier} to exist"
        raise UnmetExpectation, message unless self.exists?
end
should_have_attribute(attribute, value = nil) click to toggle source
# File lib/wrapybara/element.rb, line 55
def should_have_attribute(attribute, value = nil)
        the_attribute = Attribute.new(self, attribute)
        the_attribute.should_exist
        the_attribute.value.should_be value if value
end
should_not_be_disabled() click to toggle source
# File lib/wrapybara/element.rb, line 95
def should_not_be_disabled
        message = "Did not expect element #{self.element_identifier} to be disabled"
        raise UnmetExpectation, message if self.disabled?
end
should_not_be_enabled() click to toggle source
# File lib/wrapybara/element.rb, line 85
def should_not_be_enabled
        message = "Did not expect element #{self.element_identifier} to be enabled"
        raise UnmetExpectation, message if self.enabled?
end
should_not_be_focused() click to toggle source
# File lib/wrapybara/ext/focus_tracking.rb, line 40
def should_not_be_focused
        message = "Did not expect element #{self.element_identifier} to be focused"
        raise UnmetExpectation, message if self.focused?
end
should_not_be_visible() click to toggle source
# File lib/wrapybara/element.rb, line 124
def should_not_be_visible
        message = "Did not expect content #{self.element_identifier} to be visible"
        raise UnmetExpectation, message if self.visible?
end
should_not_exist(message = nil) click to toggle source
# File lib/wrapybara/element.rb, line 50
def should_not_exist(message = nil)
        message ||= "Did not expect element #{self.element_identifier} to exist"
        raise UnmetExpectation, message if self.exists?
end
should_not_have_attribute(attribute, value = nil) click to toggle source
# File lib/wrapybara/element.rb, line 61
def should_not_have_attribute(attribute, value = nil)
        the_attribute = Attribute.new(self, attribute)
        if value
                the_attribute.should_exist
                the_attribute.value.should_not_be value
        else
                the_attribute.should_not_exist
        end
end
style() click to toggle source
# File lib/wrapybara/element.rb, line 33
def style
        element[:style] || ''
end
visible?() click to toggle source
# File lib/wrapybara/element.rb, line 100
def visible?
        self.should_exist
        
        # determining visibility of something is challenging, to say the least. this approach is to start with the subject element
        # and check its style attribute for display: none or visibility: hidden. if neither is found, work backward up the
        # object hierarchy checking each parent.
        
        return false if self.style.downcase.gsub(' ', '') =~ /(display:none|visibility:hidden)/

        parent = get_element(self.path + '/..', nil)
        while parent && parent.path != '/html'
                style = parent[:style] || ''
                return false if style.downcase.gsub(' ', '') =~ /(display:none|visibility:hidden)/
                parent = get_element(parent.path + '/..', nil)
        end
        
        return true
end
within(scope) click to toggle source
# File lib/wrapybara/element.rb, line 129
def within(scope)
        scope ? " within '#{scope}'" : ''
end