awetestlib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,185 @@
1
+ module PageData
2
+
3
+ =begin rdoc
4
+ :category: Page Data
5
+ :tags: data, DOM, page
6
+
7
+ _Parameters_::
8
+
9
+ *browser* is any container element, usually the browser window or a div within it. Best to use is the smallest that contains the desired data.
10
+
11
+ *types* is an array that defaults to all of: :text, :textarea, :select_list, :span, :hidden, :checkbox, and :radio.
12
+ Set types to an array of a subset of these if fewer elements are desired.
13
+
14
+ No positive validations are reported but failure is rescued and reported.
15
+ =end
16
+ def capture_page_data(browser, types = [:text, :textarea, :select_list, :span, :hidden, :checkbox, :radio])
17
+ start = Time.now
18
+ debug_to_log("Begin #{__method__}")
19
+ data = Hash.new
20
+ data[:id] = Hash.new
21
+ data[:name] = Hash.new
22
+ data[:index] = Hash.new
23
+ types.each do |type|
24
+ #debug_to_log("#{__method__}: #{type}. . .")
25
+ data[:id][type], data[:name][type], data[:index][type] = parse_elements(browser, type)
26
+ end
27
+ data
28
+ rescue
29
+ failed_to_log("#{__method__}: '#{$!}'")
30
+ ensure
31
+ stop = Time.now
32
+ passed_to_log("#{__method__.to_s.titleize} finished. (#{"%.5f" % (stop - start)} secs)")
33
+ #debug_to_log("End #{__method__}")
34
+ end
35
+
36
+ def compare_page_data(before, after, how, desc = '')
37
+ [:text, :textarea, :select_list, :span, :checkbox, :radio].each do |type|
38
+ before[how][type].each_key do |what|
39
+ msg = "#{desc} #{type} #{how}=#{what}: Expected '#{before[how][type][what]}'."
40
+ if after[how][type][what] == before[how][type][what]
41
+ passed_to_log(msg)
42
+ else
43
+ failed_to_log("#{msg} Found '#{after[how][type][what]}'")
44
+ end
45
+ end
46
+ end
47
+ rescue
48
+ failed_to_log("Unable to compare before and after page data. '#{$!}'")
49
+ end
50
+
51
+ =begin rdoc
52
+ :category: Page Data
53
+ :tags:data, DOM
54
+
55
+ *data* is the hash returned by capture_page_data().
56
+
57
+ *how* is one of :id, :name, :index
58
+
59
+ *what* is the target value for how. It can be a string or a regular expression
60
+
61
+ *type* is one of :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio
62
+
63
+ *get_text* determines whether selected option's text or value is returned. Default is true, i.e., return the selected text.
64
+ This only applies when the *type* is :select_list.
65
+
66
+ =end
67
+ def fetch_page_data(data, how, what, type, get_text = true)
68
+ rslt = data[how][type][what]
69
+ if type == :select_list
70
+ value, text = rslt.split('::')
71
+ if get_text
72
+ rslt = text
73
+ else
74
+ rslt = value
75
+ end
76
+ end
77
+ rslt
78
+ end
79
+
80
+ =begin rdoc
81
+ :category: Page Data
82
+ :tags:data, DOM
83
+
84
+ *browser* is any container element. best to use is the smallest that contains the desired data.
85
+
86
+ *type* is one of these symbols: :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio
87
+
88
+ Returns three hashes: id[type][id] = value, name[type][id] = value, index[type][id] = value
89
+
90
+ A given element appears once in the set of hashes depending on how is is found: id first
91
+ then name, then index.
92
+
93
+ Select list value is in the form 'value::text'. parse with x.split('::')
94
+
95
+ No positive validations are reported but failure is rescued and reported.
96
+ =end
97
+ def parse_elements(browser, type)
98
+ id = Hash.new
99
+ name = Hash.new
100
+ index = Hash.new
101
+ idx = 0
102
+ #debug_to_log("#{__method__}: #{type}")
103
+ case type
104
+ when :span
105
+ collection = browser.spans
106
+ when :select_list
107
+ collection = browser.select_lists
108
+ when :radio
109
+ collection = browser.radios
110
+ when :checkbox
111
+ collection = browser.checkboxes
112
+ else
113
+ collection = browser.elements(:type, type.to_s)
114
+ end
115
+ #debug_to_log("#{__method__}: collection: #{collection.inspect}")
116
+ collection.each do |e|
117
+ case type
118
+ when :span
119
+ vlu = e.text
120
+ when :select_list
121
+ vlu = "#{e.value}::#{e.selected_options[0]}"
122
+ when :radio
123
+ vlu = e.set?
124
+ when :checkbox
125
+ vlu = e.set?
126
+ else
127
+ vlu = e.value
128
+ end
129
+ idx += 1
130
+ if e.id.length > 0 and not e.id =~ /^__[A-Z]/
131
+ id[e.id] = vlu
132
+ elsif e.name.length > 0 and not e.name =~ /^__[A-Z]/
133
+ name[e.name] = vlu
134
+ else
135
+ index[idx] = vlu if not type == :hidden
136
+ end
137
+ end
138
+ [id, name, index]
139
+
140
+ rescue
141
+ failed_to_log("#{__method__}: '#{$!}'")
142
+ end
143
+
144
+ def get_textfield_value(browser, how, what, desc = '')
145
+ msg = "Return value in textfield #{how}='#{what}'"
146
+ msg << " #{desc}" if desc.length > 0
147
+ tf = browser.text_field(how, what)
148
+ if validate(browser, @myName, __LINE__)
149
+ if tf
150
+ debug_to_log("#{tf.inspect}")
151
+ vlu = tf.value
152
+ passed_to_log("#{msg} Value='#{vlu}'")
153
+ vlu
154
+ else
155
+ failed_to_log("#{msg}")
156
+ end
157
+ end
158
+ rescue
159
+ failed_to_log("Unable to #{msg}: '#{$!}'")
160
+ end
161
+
162
+ def get_textfield_value_by_name(browser, strg, desc = '')
163
+ get_textfield_value(browser, :name, strg, desc)
164
+ end
165
+
166
+ def get_textfield_value_by_id(browser, strg)
167
+ get_textfield_value(browser, :id, strg)
168
+ end
169
+
170
+ def get_element_text(browser, element, how, what, desc = '')
171
+ msg = "Return text in #{element} #{how}='#{what}'"
172
+ msg << " #{desc}" if desc.length > 0
173
+ text = browser.element(how, what).text
174
+ if validate(browser, @myName, __LINE__)
175
+ passed_to_log("#{msg} text='#{text}'")
176
+ text
177
+ end
178
+ rescue
179
+ failed_to_log("Unable to #{msg}: '#{$!}'")
180
+ end
181
+
182
+
183
+
184
+
185
+ end
@@ -0,0 +1,278 @@
1
+ require 'regression/browser'
2
+ require 'regression/find'
3
+ require 'regression/user_input'
4
+ require 'regression/waits'
5
+ require 'regression/tables'
6
+ require 'regression/page_data'
7
+ require 'regression/drag_and_drop'
8
+ require 'regression/utilities'
9
+ require 'regression/logging'
10
+ require 'regression/validations'
11
+
12
+ #require 'rbconfig'
13
+ require 'ostruct'
14
+ require 'active_support'
15
+ require 'active_support/inflector'
16
+
17
+ module Awetestlib
18
+
19
+ class Runner
20
+
21
+ # order matters here
22
+ include Logging
23
+ include Browser
24
+ include Find
25
+ include UserInput
26
+ include Waits
27
+ include Tables
28
+ include PageData
29
+ include DragAndDrop
30
+ include Utilities
31
+ include Validations
32
+
33
+ ::DEBUG = 0
34
+ ::INFO = 1
35
+ ::WARN = 2
36
+ ::ERROR = 3
37
+ ::FATAL = 4
38
+ ::UNKNOWN = 5
39
+
40
+ ::TOP_LEVEL = 7
41
+ ::SECOND_LEVEL = ::TOP_LEVEL - 1
42
+
43
+ ::WAIT = 20
44
+ ::PASS = '-PASS'
45
+ ::FAIL = '-FAIL'
46
+
47
+ puts ENV["OS"]
48
+
49
+ attr_accessor :browser, :browser_abbrev, :version, :env,
50
+ :library, :script_type, :script_file,
51
+ :log_properties, :log_queue, :log_class,
52
+ :notify_queue, :notify_class, :notify_id,
53
+ :screencap_path, :xls_path, :script_path, :user_token, :root_path,
54
+ :debug_on_fail,
55
+ :environment, :environment_name, :environment_url, :environment_nodename,
56
+ :cycle, :browser_sequence,
57
+ :output_to_log, :log_path_subdir, :report_all_test_refs,
58
+ :timeout
59
+
60
+ #def self.build(options)
61
+ # #build_class = "Awetestlib::#{script_module_for options[:script_type]}::Runner".constantize
62
+ # build_class = "Awetestlib::Runner".constantize
63
+ # #options = options.merge(:script_file => options[:script_file])
64
+ # #if build_class.respond_to?(:runner_class)
65
+ # # build_class.runner_class(options)
66
+ # #else
67
+ # build_class.new(options)
68
+ # #end
69
+ #end
70
+
71
+ # TODO: Encapsulate in some kind of config
72
+ ###################################
73
+ def setup_global_test_vars(options)
74
+ @my_failed_count = 0
75
+ @my_passed_count = 0
76
+ @my_error_references = Hash.new
77
+ @my_error_hits = Hash.new
78
+
79
+ @report_all_refs = options[:report_all_test_refs]
80
+
81
+ if options[:environment]
82
+ @myAppEnv = OpenStruct.new(
83
+ :name => options[:environment]['name'],
84
+ :url => options[:environment]['url'],
85
+ :nodename => options[:environment]['nodename']
86
+ )
87
+ @runenv = options[:environment]['nodename'] || options[:environment]['name']
88
+ @myURL = options[:environment]['url']
89
+ else
90
+ @runenv = options[:environment_name]
91
+ end
92
+
93
+ @targetBrowser = browser_to_use(options[:browser], options[:version])
94
+ @targetVersion = @targetBrowser.version
95
+ @browserAbbrev = @targetBrowser.abbrev
96
+ @myRoot = options[:root_path]
97
+ @myName = File.basename(options[:script_file]).sub(/\.rb$/, '')
98
+
99
+ if options[:output_to_log]
100
+ log_path = "#{@myRoot}/"
101
+ log_path << "#{options[:log_path_subdir]}/" if options[:log_path_subdir]
102
+ log_spec = File.join log_path, "#{@myName}_#{Time.now.strftime("%Y%m%d%H%M%S")}.log"
103
+ @myLog = init_logger(log_spec, @myName)
104
+ #@start_timestamp = Time.now
105
+ #start_to_log(@start_timestamp)
106
+ end
107
+
108
+ if options[:xls_path]
109
+ @xls_path = options[:xls_path]
110
+ end
111
+
112
+ #TODO need to find way to calculate these on the fly
113
+ # window top border 30
114
+ # IE toolbars 86
115
+ @vertical_hack_ie = 117
116
+ # FF toolbars 114
117
+ @vertical_hack_ff = 144
118
+ # window left border 4
119
+ @horizontal_hack_ie = 5
120
+ @horizontal_hack_ff = 4
121
+ #
122
+ # @x_tolerance = 12
123
+ # @y_tolerance = 12
124
+ require_gems
125
+ end
126
+
127
+ #def self.runner_class(options)
128
+ # script_file = options[:script_file]
129
+ # load script_file # force a load
130
+ #
131
+ # runner_module = self.module_for script_file
132
+ # klass_name = "#{runner_module.to_s}::Runner"
133
+ #
134
+ # # Define a Runner class in the test script's module inheriting from AwetestLegacy::Runner
135
+ # runner_module.module_eval do
136
+ # eval <<-RUBY
137
+ # class #{klass_name} < Awetestlib::Runner
138
+ # def initialize(options)
139
+ # #super(options)
140
+ # setup_global_test_vars(options)
141
+ # end
142
+ # end
143
+ # RUBY
144
+ # end
145
+ #
146
+ # runner = runner_module::Runner.new(options)
147
+ #
148
+ # if options[:library]
149
+ # lib_file = options[:library]
150
+ # load lib_file
151
+ # lib_module = self.module_for lib_file
152
+ # runner.extend(lib_module)
153
+ # end
154
+ #
155
+ # # Add in the methods defined in the script's module
156
+ # runner.extend(runner_module)
157
+ # runner
158
+ #end
159
+
160
+ def initialize(options)
161
+ options.each_pair do |k, v|
162
+ self.send("#{k}=", v)
163
+ end
164
+ setup_global_test_vars(options)
165
+
166
+ # load and extend with library module if it exists
167
+ if options[:library]
168
+ lib_file = options[:library]
169
+ load lib_file # force a fresh load
170
+ lib_module = module_for lib_file
171
+ self.extend(lib_module)
172
+ end
173
+
174
+ # load and extend with script
175
+ script_file = options[:script_file]
176
+ load script_file # force a fresh load
177
+ runner_module = module_for script_file
178
+ self.extend(runner_module)
179
+
180
+ end
181
+
182
+ def browser_to_use(browser, browser_version = nil)
183
+ platform = ''
184
+ platform = 'Windows' if !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) || (RUBY_PLATFORM =~ /mswin|mingw/))
185
+ platform = 'OSX' if RUBY_PLATFORM =~ /darwin/
186
+
187
+ browser_abbrev =
188
+ Awetestlib::BROWSER_ALTERNATES[platform][browser] ?
189
+ Awetestlib::BROWSER_ALTERNATES[platform][browser] : browser
190
+ if not browser_version
191
+ case browser_abbrev
192
+ when 'IE'
193
+ browser_version = 8
194
+ when 'FF'
195
+ browser_version = 11
196
+ when 'C', 'GC'
197
+ browser_version = 10
198
+ when 'S'
199
+ browser_version = 10
200
+ end
201
+ end
202
+ return OpenStruct.new(
203
+ :name => (Awetestlib::BROWSER_MAP[browser_abbrev]),
204
+ :abbrev => browser_abbrev,
205
+ :version => browser_version
206
+ )
207
+ end
208
+
209
+ def require_gems
210
+
211
+ case @targetBrowser.abbrev
212
+
213
+ when 'IE'
214
+ if version.to_f >= 9.0
215
+ require 'watir-webdriver'
216
+ else
217
+ require 'watir/ie'
218
+ require 'watir'
219
+ require 'watir/process'
220
+ require 'watirloo'
221
+ require 'patches/watir'
222
+ Watir::IE.visible = true
223
+ end
224
+ when 'FF'
225
+ if @targetBrowser.version.to_f < 4.0
226
+ require 'firewatir'
227
+ require 'patches/firewatir'
228
+ else
229
+ require 'watir-webdriver'
230
+ end
231
+
232
+ when 'S'
233
+ require 'safariwatir'
234
+
235
+ when 'C', 'GC'
236
+ require 'watir-webdriver'
237
+
238
+ # when 'CL'
239
+ # require 'celerity'
240
+ # require 'watir-webdriver'
241
+
242
+ end
243
+
244
+ if USING_WINDOWS
245
+ require 'watir/win32ole'
246
+ @ai = ::WIN32OLE.new('AutoItX3.Control')
247
+ require 'pry'
248
+ else
249
+ # TODO: Need alternative for Mac?
250
+ @ai = ''
251
+ end
252
+
253
+ end
254
+
255
+ def module_for(script_file)
256
+ File.read(script_file).match(/^module\s+(\w+)/)[1].constantize
257
+ end
258
+
259
+ def before_run
260
+ start_run
261
+ end
262
+
263
+ def start
264
+ before_run
265
+ run
266
+ rescue Exception => e
267
+ failed_to_log(e.to_s)
268
+ ensure
269
+ after_run
270
+ end
271
+
272
+ def after_run
273
+ finish_run
274
+ @myLog.close if @myLog
275
+ end
276
+
277
+ end
278
+ end