Class: Watir::Locators::Element::SelectorBuilder::XPath
- Inherits:
-
Object
- Object
- Watir::Locators::Element::SelectorBuilder::XPath
show all
- Defined in:
- lib/watir-webdriver/locators/element/selector_builder/xpath.rb
Instance Method Summary
collapse
Constructor Details
#initialize(should_use_label_element) ⇒ XPath
Returns a new instance of XPath.
6
7
8
|
# File 'lib/watir-webdriver/locators/element/selector_builder/xpath.rb', line 6
def initialize(should_use_label_element)
@should_use_label_element = should_use_label_element
end
|
Instance Method Details
#attribute_expression(building, selectors) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/watir-webdriver/locators/element/selector_builder/xpath.rb', line 27
def attribute_expression(building, selectors)
f = selectors.map do |key, val|
if val.is_a?(Array)
"(" + val.map { |v| equal_pair(building, key, v) }.join(" or ") + ")"
else
equal_pair(building, key, val)
end
end
f.join(" and ")
end
|
#build(selectors) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/watir-webdriver/locators/element/selector_builder/xpath.rb', line 10
def build(selectors)
xpath = ".//"
xpath << (selectors.delete(:tag_name) || '*').to_s
selectors.delete :index
unless selectors.empty?
xpath << "[" << attribute_expression(nil, selectors) << "]"
end
p xpath: xpath, selectors: selectors if $DEBUG
[:xpath, xpath]
end
|
#equal_pair(building, key, value) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/watir-webdriver/locators/element/selector_builder/xpath.rb', line 39
def equal_pair(building, key, value)
if key == :class
klass = XpathSupport.escape " #{value} "
"contains(concat(' ', @class, ' '), #{klass})"
elsif key == :label && @should_use_label_element
text = "normalize-space()=#{XpathSupport.escape value}"
"(@id=//label[#{text}]/@for or parent::label[#{text}])"
else
"#{lhs_for(building, key)}=#{XpathSupport.escape value}"
end
end
|
#lhs_for(_building, key) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/watir-webdriver/locators/element/selector_builder/xpath.rb', line 53
def lhs_for(_building, key)
case key
when :text, 'text'
'normalize-space()'
when :href
'normalize-space(@href)'
when :type
XpathSupport.downcase('@type')
else
"@#{key.to_s.tr("_", "-")}"
end
end
|