-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelement.rb
65 lines (54 loc) · 2.42 KB
/
element.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'core_ext/hash/except'
require 'core_ext/hash/slice'
module Locator
class Element
autoload :Area, 'locator/element/area'
autoload :Button, 'locator/element/button'
autoload :CheckBox, 'locator/element/check_box'
autoload :Content, 'locator/element/content'
autoload :ElementsList, 'locator/element/elements_list'
autoload :Field, 'locator/element/field'
autoload :File, 'locator/element/file'
autoload :Form, 'locator/element/form'
autoload :FormElement, 'locator/element/form_element'
autoload :HiddenField, 'locator/element/hidden_field'
autoload :Input, 'locator/element/input'
autoload :Label, 'locator/element/label'
autoload :LabeledElement, 'locator/element/labeled_element'
autoload :Link, 'locator/element/link'
autoload :RadioButton, 'locator/element/radio_button'
autoload :Select, 'locator/element/select'
autoload :SelectOption, 'locator/element/select_option'
autoload :TextArea, 'locator/element/text_area'
attr_reader :name, :css, :locatables, :attributes
def initialize(*args)
@attributes, @name = args.last.is_a?(Hash) ? args.pop : {}, args.pop
@locatables = (attributes.delete(:matches) || [:content]) << :id
end
def locate(*args)
all(*args).first # || raise(ElementNotFound.new(*args))
end
def all(scope, *args)
attributes, selector = args.last.is_a?(Hash) ? args.pop : {}, args.pop
result = lookup(scope, selector, attributes)
result.sort! if selector
result
end
def xpath(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
attributes = self.attributes.merge(options.except(:xpath, :css)) # TODO move to Xpath?
xpath, css = options.values_at(:xpath, :css)
xpath ||= css ? ::Nokogiri::CSS.xpath_for(*css).first : args.pop
Xpath.new(xpath || name || '*', attributes).to_s
end
protected
def lookup(scope, selector, attributes = {})
attributes = Locator.decode_attributes(attributes) if Locator.decode_entities?
scope = scope.respond_to?(:elements_by_xpath) ? scope : Locator::Dom.page(scope)
xpath = xpath(attributes)
xpath = ".#{xpath}" unless xpath[0, 1] == '.'
elements = scope.elements_by_xpath(xpath)
Result.new(elements).filter!(selector, locatables)
end
end
end