Class: Bauxite::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/bauxite/application.rb

Overview

equals the number of failed test cases (again, zero indicates success).

Class Method Summary collapse

Class Method Details

.startObject

:nodoc:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/bauxite/application.rb', line 176

def self.start #:nodoc:
	options = {
		:logger       => [],
		:verbose      => false,
		:break        => false,
		:driver       => 'firefox',
		:debug        => false,
		:timeout      => 10,
		:open_timeout => 60,
		:reset        => false,
		:selector     => 'sid',
		:driver_opt   => {},
		:logger_opt   => {},
		:extensions   => []
	}
	
	default_logger = (ENV['TERM'] == 'xterm') ?  'xterm' : 'terminal'
	
	OptionParser.new do |opts|
		opts.banner = "Usage: bauxite [options] [files...]"
		
		def opts.o=(o)
			@o = o
		end
		def opts.single(*args)
			on(*args[1..-1]) do |v|
				@o[args[0]] = v
			end
			self
		end
		def opts.hash(*args)
			on(*args[1..-1]) do |v|
				n,v = v.split('=', 2)
				@o[args[0]][n.to_sym] = v || true
			end
			self
		end
		def opts.multi(*args)
			on(*args[1..-1]) do |v|
				@o[args[0]] << v
			end
			self
		end
		opts.o = options

		opts.separator ""
		opts.separator "Options: "
		opts
		.single(:verbose     , "-v",  "--verbose", "Show verbose errors")
		.single(:timeout     , "-t",  "--timeout SECONDS", "Selector timeout (#{options[:timeout]}s)")
		.single(:open_timeout, "-o",  "--open-timeout SECONDS", "Open timeout (#{options[:open_timeout]}s)")
		.single(:debug       , "-d",  "--debug", "Break to debug on error. "+
										"Start the debug console if no input files given.")
		.single(:driver      , "-p",  "--provider PROVIDER"     , "Driver provider")
		.hash(  :driver_opt  , "-P",  "--provider-option OPTION", "Provider options (name=value)")
		.multi( :logger      , "-l",  "--logger LOGGER"         , "Logger type ('#{default_logger}')")
		.hash(  :logger_opt  , "-L",  "--logger-option OPTION"  , "Logger options (name=value)")
		.single(:reset       , "-r",  "--reset"                 , "Reset driver between tests")
		.single(:wait        , "-w",  "--wait"                  , "Wait for ENTER before exiting")
		.single(:selector    , "-s",  "--selector SELECTOR"     , "Default selector ('#{options[:selector]}')")
		.single(:capture     , "-c",  "--capture"               , "If the test fails, capture a screenshot")
		.single(:output      , "--output DIR"                   , "Output directory for generated artifacts")
		.multi( :extensions  , "-e",  "--extension DIR"         , "Load extensions from DIR")
		
		opts.on("-u", "--url [URL]", "Configure the remote provider listening in the given url") do |v|
			v = 'localhost:4444' unless v
			v = 'http://'+v unless v.match /^https?:\/\//
			v = v + '/wd/hub' unless v.end_with? '/wd/hub'
			options[:driver] = 'remote'
			options[:driver_opt][:url] = v
		end
		
		opts.on("--csv-summary [FILE]", "Output a single-line CSV summary") { |v| options[:csv] = v || 'summary.csv' }
		
		opts.on("--jenkins DIR", "Configure default options for Jenkins integration") do |v|
			options[:driver] = 'remote'
			options[:driver_opt][:url] = 'http://localhost:4444/wd/hub'
			options[:logger] = ['echo', 'html']
			options[:output] = v
			options[:logger_opt][:html_package] = true
			options[:capture] = true
			options[:csv] = 'summary.csv'
		end
		
		opts.on("--version", "Show version") do
			puts "bauxite #{Bauxite::VERSION}"
			exit
		end
		
		opts.separator ""
		opts.separator "Loggers:"
		Context::loggers.sort.each do |s|
			opts.separator "    #{s}"
		end
		
		opts.separator ""
		opts.separator "Selectors:".ljust(32)+" Actions:"
		max_action_size = Context::max_action_name_size
		selectors = Context::selectors.sort
		actions   = Context::actions.sort
		[selectors.size, actions.size].max.times.zip(selectors, actions).each do |idx,s,a|
			a = a ? "#{a.ljust(max_action_size)} #{Context.action_args(a).join(' ')}" : ''
			s = '' unless s
			opts.separator "    #{s.ljust(28)}     #{a}"
		end
		
		opts.separator ""
	end.parse!
	
	options[:logger] << default_logger unless options[:logger].size > 0
	
	ctx = nil
	begin
		ctx = Context.new(options)
	rescue StandardError => e
		puts e.message
		if options[:verbose]
			p e
			puts e.backtrace
		end
		exit false
	end

	files = ARGV
	files = ['stdin'] if files.size == 0 and not options[:debug]

	actions = files.map { |f| "load \"#{f.gsub('"', '""')}\"" }

	if options[:reset] and files.size > 1
		actions = actions.map { |a| [a] }.inject do |sum,a|
			sum += ['reset']
			sum + a
		end.flatten
	end

	if options[:debug] and actions.size == 0
		actions = ['debug'] 
	end

	begin
		ctx.start(actions)
	rescue StandardError => e
		exit false
	ensure
		if ctx.tests.any?

			if (csv_file = options[:csv])
				total  = ctx.tests.size
				ok     = ctx.tests.inject(0) { |s,t| s + (t[:status] == 'OK' ? 1 : 0) }
				failed = total - ok
				time   = ctx.tests.inject(0) { |s,t| s + t[:time] }
				File.open(ctx.output_path(csv_file), 'w') do |f|
					f.write "Total,OK,Failed,Time\n#{total},#{ok},#{failed},#{time}\n"
				end
			end
			
			failed = 0
			
			puts
			puts 'Test summary:'
			puts '============='
			puts 'Name'.ljust(40 ) + ' Time'.ljust(7 ) + ' Status'.ljust(7)+' Error'
			puts ''.ljust(40, '-') + ' '.ljust(7, '-') + ' '.ljust(7, '-' )+' '.ljust(5, '-')
			ctx.tests.each do |t|
				error = t[:error]
				error = error ? error.message : ''
				puts t[:name].ljust(40) + ' ' + t[:time].round(2).to_s.rjust(6) + ' ' + t[:status].ljust(6) + ' ' + error
				failed += 1 if t[:status] != 'OK'
			end
			exit failed if failed > 0
		end
	end
end