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

Instance Method Details

#clickObject



37
38
39
# File 'lib/wrapybara/element.rb', line 37

def click
	element.click
end

#default_howObject



146
147
148
# File 'lib/wrapybara/element.rb', line 146

def default_how
	'labeled'
end

#default_scopeObject



142
143
144
# File 'lib/wrapybara/element.rb', line 142

def default_scope
	nil
end

#disabled?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/wrapybara/element.rb', line 75

def disabled?
	self.should_exist
	['true', 'disabled'].include?(@element[:disabled])
end

#elementObject



25
26
27
# File 'lib/wrapybara/element.rb', line 25

def element
	@element
end

#element_identifierObject



133
134
135
# File 'lib/wrapybara/element.rb', line 133

def element_identifier
	"#{@how} '#{@identifier}'#{self.within(@scope)}"
end

#enabled?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/wrapybara/element.rb', line 71

def enabled?
	!disabled?
end

#exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/wrapybara/element.rb', line 41

def exists?
	!@element.nil?
end

#focused?Boolean

Returns:

  • (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_identifierObject



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

#pathObject



29
30
31
# File 'lib/wrapybara/element.rb', line 29

def path
	element.path
end

#should_be_disabledObject

Raises:



90
91
92
93
# 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_enabledObject

Raises:



80
81
82
83
# 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_focusedObject

Raises:



35
36
37
38
# 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_visibleObject

Raises:



119
120
121
122
# 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) ⇒ Object

Raises:



45
46
47
48
# 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) ⇒ 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_disabledObject

Raises:



95
96
97
98
# 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_enabledObject

Raises:



85
86
87
88
# 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_focusedObject

Raises:



40
41
42
43
# 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_visibleObject

Raises:



124
125
126
127
# 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) ⇒ Object

Raises:



50
51
52
53
# 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) ⇒ 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

#styleObject



33
34
35
# File 'lib/wrapybara/element.rb', line 33

def style
	element[:style] || ''
end

#visible?Boolean

Returns:

  • (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