Module: Wrapybara::Element
- Included in:
- Attribute, Button, Checkbox, Content, FileField, Form, Image, Label, Link, Option, PasswordField, RadioButton, Select, Table, TableBody, TableCell, TableHead, TextArea, TextField
- Defined in:
- lib/wrapybara/element.rb,
lib/wrapybara/ext/focus_tracking.rb
Instance Method Summary collapse
- #click ⇒ Object
- #default_how ⇒ Object
- #default_scope ⇒ Object
- #disabled? ⇒ Boolean
- #element ⇒ Object
- #element_identifier ⇒ Object
- #enabled? ⇒ Boolean
- #exists? ⇒ Boolean
- #focused? ⇒ Boolean
- #get_element(xpath, scope) ⇒ Object
- #parent_identifier ⇒ Object
- #path ⇒ Object
- #should_be_disabled ⇒ Object
- #should_be_enabled ⇒ Object
- #should_be_focused ⇒ Object
- #should_be_visible ⇒ Object
- #should_exist(message = nil) ⇒ Object
- #should_have_attribute(attribute, value = nil) ⇒ Object
- #should_not_be_disabled ⇒ Object
- #should_not_be_enabled ⇒ Object
- #should_not_be_focused ⇒ Object
- #should_not_be_visible ⇒ Object
- #should_not_exist(message = nil) ⇒ Object
- #should_not_have_attribute(attribute, value = nil) ⇒ Object
- #style ⇒ Object
- #visible? ⇒ Boolean
- #within(scope) ⇒ Object
Instance Method Details
#click ⇒ Object
37 38 39 |
# File 'lib/wrapybara/element.rb', line 37 def click element.click end |
#default_how ⇒ Object
146 147 148 |
# File 'lib/wrapybara/element.rb', line 146 def default_how 'labeled' end |
#default_scope ⇒ Object
142 143 144 |
# File 'lib/wrapybara/element.rb', line 142 def default_scope nil end |
#disabled? ⇒ Boolean
75 76 77 78 |
# File 'lib/wrapybara/element.rb', line 75 def disabled? self.should_exist ['true', 'disabled'].include?(@element[:disabled]) end |
#element ⇒ Object
25 26 27 |
# File 'lib/wrapybara/element.rb', line 25 def element @element end |
#element_identifier ⇒ Object
133 134 135 |
# File 'lib/wrapybara/element.rb', line 133 def element_identifier "#{@how} '#{@identifier}'#{self.within(@scope)}" end |
#enabled? ⇒ Boolean
71 72 73 |
# File 'lib/wrapybara/element.rb', line 71 def enabled? !disabled? end |
#exists? ⇒ Boolean
41 42 43 |
# File 'lib/wrapybara/element.rb', line 41 def exists? !@element.nil? end |
#focused? ⇒ Boolean
30 31 32 33 |
# 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) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 ⇒ Object
137 138 139 140 |
# File 'lib/wrapybara/element.rb', line 137 def parent_identifier return '' unless @parent "#{@parent.how} '#{@parent.identifier}'#{self.within(@parent.scope)}" end |
#path ⇒ Object
29 30 31 |
# File 'lib/wrapybara/element.rb', line 29 def path element.path end |
#should_be_disabled ⇒ Object
90 91 92 93 |
# File 'lib/wrapybara/element.rb', line 90 def should_be_disabled = "Expected element #{self.element_identifier} to be disabled" raise UnmetExpectation, unless self.disabled? end |
#should_be_enabled ⇒ Object
80 81 82 83 |
# File 'lib/wrapybara/element.rb', line 80 def should_be_enabled = "Expected element #{self.element_identifier} to be enabled" raise UnmetExpectation, unless self.enabled? end |
#should_be_focused ⇒ Object
35 36 37 38 |
# File 'lib/wrapybara/ext/focus_tracking.rb', line 35 def should_be_focused = "Expected element #{self.element_identifier} to be focused" raise UnmetExpectation, unless self.focused? end |
#should_be_visible ⇒ Object
119 120 121 122 |
# File 'lib/wrapybara/element.rb', line 119 def should_be_visible = "Expected content #{self.element_identifier} to be visible" raise UnmetExpectation, unless self.visible? end |
#should_exist(message = nil) ⇒ Object
45 46 47 48 |
# File 'lib/wrapybara/element.rb', line 45 def should_exist( = nil) ||= "Expected element #{self.element_identifier} to exist" raise UnmetExpectation, unless self.exists? end |
#should_have_attribute(attribute, value = nil) ⇒ Object
55 56 57 58 59 |
# 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 ⇒ Object
95 96 97 98 |
# File 'lib/wrapybara/element.rb', line 95 def should_not_be_disabled = "Did not expect element #{self.element_identifier} to be disabled" raise UnmetExpectation, if self.disabled? end |
#should_not_be_enabled ⇒ Object
85 86 87 88 |
# File 'lib/wrapybara/element.rb', line 85 def should_not_be_enabled = "Did not expect element #{self.element_identifier} to be enabled" raise UnmetExpectation, if self.enabled? end |
#should_not_be_focused ⇒ Object
40 41 42 43 |
# File 'lib/wrapybara/ext/focus_tracking.rb', line 40 def should_not_be_focused = "Did not expect element #{self.element_identifier} to be focused" raise UnmetExpectation, if self.focused? end |
#should_not_be_visible ⇒ Object
124 125 126 127 |
# File 'lib/wrapybara/element.rb', line 124 def should_not_be_visible = "Did not expect content #{self.element_identifier} to be visible" raise UnmetExpectation, if self.visible? end |
#should_not_exist(message = nil) ⇒ Object
50 51 52 53 |
# File 'lib/wrapybara/element.rb', line 50 def should_not_exist( = nil) ||= "Did not expect element #{self.element_identifier} to exist" raise UnmetExpectation, if self.exists? end |
#should_not_have_attribute(attribute, value = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# 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 ⇒ Object
33 34 35 |
# File 'lib/wrapybara/element.rb', line 33 def style element[:style] || '' end |
#visible? ⇒ Boolean
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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) ⇒ Object
129 130 131 |
# File 'lib/wrapybara/element.rb', line 129 def within(scope) scope ? " within '#{scope}'" : '' end |