watir-nokogiri 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +65 -0
  3. data/lib/watir-nokogiri.rb +69 -0
  4. data/lib/watir-nokogiri/aliases.rb +6 -0
  5. data/lib/watir-nokogiri/attribute_helper.rb +145 -0
  6. data/lib/watir-nokogiri/cell_container.rb +31 -0
  7. data/lib/watir-nokogiri/container.rb +50 -0
  8. data/lib/watir-nokogiri/document.rb +171 -0
  9. data/lib/watir-nokogiri/element_collection.rb +93 -0
  10. data/lib/watir-nokogiri/elements/button.rb +71 -0
  11. data/lib/watir-nokogiri/elements/checkbox.rb +61 -0
  12. data/lib/watir-nokogiri/elements/dlist.rb +12 -0
  13. data/lib/watir-nokogiri/elements/element.rb +319 -0
  14. data/lib/watir-nokogiri/elements/file_field.rb +45 -0
  15. data/lib/watir-nokogiri/elements/font.rb +11 -0
  16. data/lib/watir-nokogiri/elements/form.rb +17 -0
  17. data/lib/watir-nokogiri/elements/frame.rb +75 -0
  18. data/lib/watir-nokogiri/elements/generated.rb +2662 -0
  19. data/lib/watir-nokogiri/elements/hidden.rb +24 -0
  20. data/lib/watir-nokogiri/elements/image.rb +59 -0
  21. data/lib/watir-nokogiri/elements/input.rb +34 -0
  22. data/lib/watir-nokogiri/elements/link.rb +7 -0
  23. data/lib/watir-nokogiri/elements/option.rb +83 -0
  24. data/lib/watir-nokogiri/elements/radio.rb +44 -0
  25. data/lib/watir-nokogiri/elements/select.rb +126 -0
  26. data/lib/watir-nokogiri/elements/table.rb +44 -0
  27. data/lib/watir-nokogiri/elements/table_cell.rb +36 -0
  28. data/lib/watir-nokogiri/elements/table_row.rb +46 -0
  29. data/lib/watir-nokogiri/elements/table_section.rb +15 -0
  30. data/lib/watir-nokogiri/elements/text_area.rb +22 -0
  31. data/lib/watir-nokogiri/elements/text_field.rb +44 -0
  32. data/lib/watir-nokogiri/exception.rb +20 -0
  33. data/lib/watir-nokogiri/locators/button_locator.rb +54 -0
  34. data/lib/watir-nokogiri/locators/child_cell_locator.rb +24 -0
  35. data/lib/watir-nokogiri/locators/child_row_locator.rb +29 -0
  36. data/lib/watir-nokogiri/locators/element_locator.rb +298 -0
  37. data/lib/watir-nokogiri/locators/text_area_locator.rb +20 -0
  38. data/lib/watir-nokogiri/locators/text_field_locator.rb +71 -0
  39. data/lib/watir-nokogiri/row_container.rb +42 -0
  40. data/lib/watir-nokogiri/user_editable.rb +38 -0
  41. data/lib/watir-nokogiri/version.rb +3 -0
  42. data/lib/watir-nokogiri/xpath_support.rb +22 -0
  43. metadata +102 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Justin Ko
2
+ Copyright (c) 2009-2013 Jari Bakken (code copied from the watir-webdriver gem)
3
+ Copyright (c) 2009-2013 FINN.no AS (code copied from the watirspec gem)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ Watir-Nokogiri
2
+ ==============
3
+
4
+ Watir-Nokogiri is an HTML parser using Watir's API.
5
+
6
+ Background
7
+ -----------
8
+
9
+ Watir (classic and webdriver) can have performance issues when parsing large HTML pages. One solution has been to extract the page's HTML using Watir and then parse the HTML with an HTML parser (ex Nokogiri). This required learning two APIs - Watir and the HTML parser. Watir-Nokogiri eliminates the need to learn an HTML parser API by applying the Watir API to the HTML parser Nokogiri.
10
+
11
+ Installation
12
+ -----------
13
+
14
+ ```ruby
15
+ gem install 'watir-nokogiri'
16
+ ```
17
+
18
+ Usage
19
+ -----------
20
+
21
+ To use Watir-Nokogiri:
22
+
23
+ ```ruby
24
+ require 'watir-nokogiri'
25
+ ```
26
+
27
+ Watir-Nokogiri can parse a string containing HTML.
28
+
29
+ This is typically supplied by a Watir browser:
30
+
31
+ ```ruby
32
+ doc = WatirNokogiri::Document.new(browser.html)
33
+ ```
34
+
35
+ However, you can also read from a file:
36
+
37
+ ```ruby
38
+ doc = WatirNokogiri::Document.start('C:\some_file.html')
39
+ ```
40
+
41
+ With the parsed document, you can use the standard Watir API to locate elements and validate text/attributes.
42
+
43
+ Check if an element exists
44
+
45
+ ```ruby
46
+ doc.div(:id => 'my_id').exists?
47
+ ```
48
+
49
+ Get the text of an element
50
+
51
+ ```ruby
52
+ doc.span(:id => 'my_span').text
53
+ ```
54
+
55
+ Get an attribute value of an element
56
+
57
+ ```ruby
58
+ doc.span(:id => 'my_span').attribute_value('name')
59
+ ```
60
+
61
+ Count the number of elements
62
+
63
+ ```ruby
64
+ doc.text_fields.length
65
+ ```
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+ require 'nokogiri'
3
+
4
+ require_relative 'watir-nokogiri/version'
5
+ require_relative 'watir-nokogiri/exception'
6
+ require_relative 'watir-nokogiri/xpath_support'
7
+ require_relative 'watir-nokogiri/container'
8
+ require_relative 'watir-nokogiri/locators/element_locator'
9
+ require_relative 'watir-nokogiri/locators/button_locator'
10
+ require_relative 'watir-nokogiri/locators/text_area_locator'
11
+ require_relative 'watir-nokogiri/locators/text_field_locator'
12
+ require_relative 'watir-nokogiri/locators/child_row_locator'
13
+ require_relative 'watir-nokogiri/locators/child_cell_locator'
14
+ require_relative 'watir-nokogiri/document'
15
+
16
+ module WatirNokogiri
17
+ class << self
18
+
19
+ #
20
+ # @api private
21
+ #
22
+
23
+ def tag_to_class
24
+ @tag_to_class ||= {}
25
+ end
26
+
27
+ #
28
+ # @api private
29
+ #
30
+
31
+ def element_class_for(tag_name)
32
+ tag_to_class[tag_name.to_sym] || HTMLElement
33
+ end
34
+
35
+ end
36
+ end #WatirNokogiri
37
+
38
+ require_relative 'watir-nokogiri/attribute_helper'
39
+ require_relative 'watir-nokogiri/row_container'
40
+ require_relative 'watir-nokogiri/cell_container'
41
+ require_relative 'watir-nokogiri/user_editable'
42
+ require_relative 'watir-nokogiri/element_collection'
43
+ require_relative 'watir-nokogiri/elements/element'
44
+ require_relative 'watir-nokogiri/elements/generated'
45
+
46
+ require_relative 'watir-nokogiri/elements/button'
47
+ require_relative 'watir-nokogiri/elements/checkbox'
48
+ require_relative 'watir-nokogiri/elements/dlist'
49
+ require_relative 'watir-nokogiri/elements/file_field'
50
+ require_relative 'watir-nokogiri/elements/font'
51
+ require_relative 'watir-nokogiri/elements/form'
52
+ require_relative 'watir-nokogiri/elements/frame'
53
+ require_relative 'watir-nokogiri/elements/hidden'
54
+ require_relative 'watir-nokogiri/elements/image'
55
+ require_relative 'watir-nokogiri/elements/input'
56
+ require_relative 'watir-nokogiri/elements/link'
57
+ require_relative 'watir-nokogiri/elements/option'
58
+ require_relative 'watir-nokogiri/elements/radio'
59
+ require_relative 'watir-nokogiri/elements/select'
60
+ require_relative 'watir-nokogiri/elements/table'
61
+ require_relative 'watir-nokogiri/elements/table_cell'
62
+ require_relative 'watir-nokogiri/elements/table_row'
63
+ require_relative 'watir-nokogiri/elements/table_section'
64
+ require_relative 'watir-nokogiri/elements/text_area'
65
+ require_relative 'watir-nokogiri/elements/text_field'
66
+
67
+ require_relative 'watir-nokogiri/aliases'
68
+
69
+ WatirNokogiri.tag_to_class.freeze
@@ -0,0 +1,6 @@
1
+ module WatirNokogiri
2
+ module Container
3
+ alias_method :field_set, :fieldset
4
+ alias_method :field_sets, :fieldsets
5
+ end # Container
6
+ end # WatirNokogiri
@@ -0,0 +1,145 @@
1
+ module WatirNokogiri
2
+
3
+ #
4
+ # @private
5
+ #
6
+ # Extended by Element, provides methods for defining attributes on the element classes.
7
+ #
8
+
9
+ module AttributeHelper
10
+
11
+ IGNORED_ATTRIBUTES = [:text, :hash]
12
+
13
+ def typed_attributes
14
+ @typed_attributes ||= Hash.new { |hash, type| hash[type] = [] }
15
+ end
16
+
17
+ def attribute_list
18
+ @attribute_list ||= (
19
+ list = typed_attributes.values.flatten
20
+ list += ancestors[1..-1].map do |e|
21
+ e.attribute_list if e.respond_to?(:attribute_list)
22
+ end.compact.flatten
23
+ ).uniq
24
+ end
25
+
26
+ def attributes(attribute_map = nil)
27
+ attribute_map or return attribute_list
28
+
29
+ add_attributes attribute_map
30
+
31
+ attribute_map.each do |type, attribs|
32
+ attribs.each do |name|
33
+ # we don't want to override methods like :text or :hash
34
+ next if IGNORED_ATTRIBUTES.include?(name)
35
+ define_attribute(type, name)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def self.extended(klass)
43
+ klass.class_eval do
44
+ # undefine deprecated methods to use them for Element attributes
45
+ [:id, :type].each { |m| undef_method m if method_defined? m }
46
+ end
47
+ end
48
+
49
+ def define_attribute(type, name)
50
+ method_name = method_name_for(type, name)
51
+ attribute_name = attribute_for_method(name)
52
+
53
+ (@attributes ||= []) << attribute_name
54
+
55
+ case type
56
+ when :string
57
+ define_string_attribute(method_name, attribute_name)
58
+ when :bool
59
+ define_boolean_attribute(method_name, attribute_name)
60
+ when :int
61
+ define_int_attribute(method_name, attribute_name)
62
+ when :float
63
+ define_float_attribute(method_name, attribute_name)
64
+ else
65
+ # $stderr.puts "treating #{type.inspect} as string for now"
66
+ end
67
+ end
68
+
69
+ def define_string_attribute(mname, aname)
70
+ define_method mname do
71
+ assert_exists
72
+ @element.get_attribute(aname).to_s
73
+ end
74
+ end
75
+
76
+ def define_boolean_attribute(mname, aname)
77
+ define_method mname do
78
+ assert_exists
79
+ @element.get_attribute(aname)
80
+ end
81
+ end
82
+
83
+ def define_int_attribute(mname, aname)
84
+ define_method mname do
85
+ assert_exists
86
+ value = @element.get_attribute(aname)
87
+ value && Integer(value)
88
+ end
89
+ end
90
+
91
+ def define_float_attribute(mname, aname)
92
+ define_method mname do
93
+ assert_exists
94
+ value = @element.get_attribute(aname)
95
+ value && Float(value)
96
+ end
97
+ end
98
+
99
+ def add_attributes(attributes)
100
+ attributes.each do |type, attr_list|
101
+ typed_attributes[type] += attr_list
102
+ end
103
+ end
104
+
105
+ def method_name_for(type, attribute)
106
+ # http://github.com/jarib/watir-webdriver/issues/issue/26
107
+ name = case attribute
108
+ when :html_for
109
+ 'for'
110
+ when :col_span
111
+ 'colspan'
112
+ when :row_span
113
+ 'rowspan'
114
+ else
115
+ attribute.to_s
116
+ end
117
+
118
+ name << "?" if type == :bool
119
+
120
+ name
121
+ end
122
+
123
+ def attribute_for_method(method)
124
+ # http://github.com/jarib/watir-webdriver/issues/issue/26
125
+
126
+ case method.to_sym
127
+ when :class_name
128
+ 'class'
129
+ when :html_for
130
+ 'for'
131
+ when :read_only
132
+ 'readonly'
133
+ when :http_equiv
134
+ 'http-equiv'
135
+ when :col_span
136
+ 'colspan'
137
+ when :row_span
138
+ 'rowspan'
139
+ else
140
+ method.to_s
141
+ end
142
+ end
143
+
144
+ end # AttributeHelper
145
+ end # Watir
@@ -0,0 +1,31 @@
1
+ module WatirNokogiri
2
+ module CellContainer
3
+
4
+ #
5
+ # Returns table cell.
6
+ #
7
+ # @return [TableCell]
8
+ #
9
+
10
+ def cell(*args)
11
+ cell = TableCell.new(self, extract_selector(args).merge(:tag_name => /^(th|td)$/))
12
+ cell.locator_class = ChildCellLocator
13
+
14
+ cell
15
+ end
16
+
17
+ #
18
+ # Returns table cells collection.
19
+ #
20
+ # @return [TableCell]
21
+ #
22
+
23
+ def cells(*args)
24
+ cells = TableCellCollection.new(self, extract_selector(args).merge(:tag_name => /^(th|td)$/))
25
+ cells.locator_class = ChildCellLocator
26
+
27
+ cells
28
+ end
29
+
30
+ end # CellContainer
31
+ end # Watir
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ module WatirNokogiri
3
+ module Container
4
+
5
+ #
6
+ # Returns element.
7
+ #
8
+ # @example
9
+ # browser.element(:data_bind => 'func')
10
+ #
11
+ # @return [HTMLElement]
12
+ #
13
+
14
+ def element(*args)
15
+ HTMLElement.new(self, extract_selector(args))
16
+ end
17
+
18
+ #
19
+ # Returns element collection.
20
+ #
21
+ # @example
22
+ # browser.elements(:data_bind => 'func')
23
+ #
24
+ # @return [HTMLElementCollection]
25
+ #
26
+
27
+ def elements(*args)
28
+ HTMLElementCollection.new(self, extract_selector(args))
29
+ end
30
+
31
+ #
32
+ # @api private
33
+ #
34
+
35
+ def extract_selector(selectors)
36
+ case selectors.size
37
+ when 2
38
+ return { selectors[0] => selectors[1] }
39
+ when 1
40
+ obj = selectors.first
41
+ return obj if obj.kind_of? Hash
42
+ when 0
43
+ return {}
44
+ end
45
+
46
+ raise ArgumentError, "expected Hash or (:how, 'what'), got #{selectors.inspect}"
47
+ end
48
+
49
+ end # Container
50
+ end # WatirNokogiri
@@ -0,0 +1,171 @@
1
+ # encoding: utf-8
2
+ module WatirNokogiri
3
+
4
+ #
5
+ # The main class through which you control the html.
6
+ #
7
+
8
+ class Document
9
+ include Container
10
+
11
+ attr_reader :driver
12
+ alias_method :nokogiri, :driver # ensures duck typing with WatirNokogiri::Element
13
+
14
+ class << self
15
+ def start(file_path)
16
+ b = new()
17
+ b.goto file_path
18
+
19
+ b
20
+ end
21
+ end
22
+
23
+ #
24
+ # Creates a WatirNokogiri::HTML instance.
25
+ #
26
+
27
+ def initialize(html = '')
28
+ @driver = Nokogiri::HTML.parse(html)
29
+ end
30
+
31
+ def inspect
32
+ '#<%s:0x%x>' % [self.class, hash*2]
33
+ rescue
34
+ '#<%s:0x%x closed=%s>' % [self.class, hash*2, exist?.to_s]
35
+ end
36
+
37
+ #
38
+ # Reads the given file as HTML.
39
+ #
40
+ # @example
41
+ # browser.goto "www.google.com"
42
+ #
43
+ # @param [String] uri The url.
44
+ # @return [String] The url you end up at.
45
+ #
46
+
47
+ def goto(file_path)
48
+ html = File.read(file_path)
49
+ @driver = Nokogiri::HTML.parse(html)
50
+ end
51
+
52
+ def back
53
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
54
+ end
55
+
56
+ def forward
57
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
58
+ end
59
+
60
+ def url
61
+ assert_exists
62
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
63
+ end
64
+
65
+ def title
66
+ @driver.title
67
+ end
68
+
69
+ def close
70
+ @driver = nil
71
+ end
72
+ alias_method :quit, :close # TODO: close vs quit
73
+
74
+ def cookies
75
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
76
+ end
77
+
78
+ def name
79
+ :nokogiri
80
+ end
81
+
82
+ #
83
+ # Returns text of page body.
84
+ #
85
+ # @return [String]
86
+ #
87
+
88
+ def text
89
+ @driver.at_css('body').text
90
+ end
91
+
92
+ #
93
+ # Returns HTML code of current page.
94
+ #
95
+ # @return [String]
96
+ #
97
+
98
+ def html
99
+ # use body.html instead?
100
+ @driver.at_css('html').to_html
101
+ end
102
+
103
+ def alert
104
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
105
+ end
106
+
107
+ def refresh
108
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
109
+ end
110
+
111
+ def wait(timeout = 5)
112
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
113
+ end
114
+
115
+ def ready_state
116
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
117
+ end
118
+
119
+ def status
120
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
121
+ end
122
+
123
+ def execute_script(script, *args)
124
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
125
+ end
126
+
127
+ def send_keys(*args)
128
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
129
+ end
130
+
131
+ def screenshot
132
+ Screenshot.new driver
133
+ end
134
+
135
+ def add_checker(checker = nil, &block)
136
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
137
+ end
138
+
139
+ def disable_checker(checker)
140
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
141
+ end
142
+
143
+ def run_checkers
144
+ raise NotImplementedError, "not currently supported by WatirNokogiri"
145
+ end
146
+
147
+ #
148
+ # Returns true if html has been supplied and false otherwise.
149
+ #
150
+ # @return [Boolean]
151
+ #
152
+
153
+ def exist?
154
+ !@driver.nil?
155
+ end
156
+ alias_method :exists?, :exist?
157
+
158
+ def assert_exists
159
+ true
160
+ end
161
+
162
+ def reset!
163
+ # no-op
164
+ end
165
+
166
+ def document
167
+ self
168
+ end
169
+
170
+ end # Document
171
+ end # WatirNokogiri