lib/bauxite/core/context.rb in bauxite-0.6.0 vs lib/bauxite/core/context.rb in bauxite-0.6.1

- old
+ new

@@ -142,11 +142,12 @@ @driver_name = (options[:driver] || :firefox).to_sym @variables = { '__TIMEOUT__' => (options[:timeout] || 10).to_i, '__DEBUG__' => false, '__SELECTOR__' => options[:selector] || 'sid', - '__OUTPUT__' => options[:output] + '__OUTPUT__' => options[:output], + '__DIR__' => File.absolute_path(Dir.pwd) } @aliases = {} @tests = [] client = Selenium::WebDriver::Remote::Http::Default.new @@ -254,11 +255,11 @@ # # For example: # ctx.debug # # => this breaks into the debug console def debug - exec_action('debug', false) + exec_parsed_action('debug', [], false) end # Returns the value of the specified +element+. # # This method takes into account the type of element and selectively @@ -293,25 +294,36 @@ # # For example: # ctx.exec_action 'open "http://www.ruby-lang.org"' # # => navigates to www.ruby-lang.org # - def exec_action(text, log = true, file = '<unknown>', line = 0) - data = Context::parse_action_default(text, file, line) - exec_parsed_action(data[:action], data[:args], log, text, file, line) + def exec_action(text) + data = Context::parse_action_default(text, '<unknown>', 0) + exec_parsed_action(data[:action], data[:args], true, text) end # Executes the specified +file+. # # For example: # ctx.exec_file('file') # # => executes every action defined in 'file' # def exec_file(file) + current_dir = @variables['__DIR__' ] + current_file = @variables['__FILE__'] + current_line = @variables['__LINE__'] + @parser.parse(file) do |action, args, text, file, line| - exec_parsed_action(action, args, true, text, file, line) + @variables['__DIR__'] = File.absolute_path(File.dirname(file)) + @variables['__FILE__'] = file + @variables['__LINE__'] = line + exec_parsed_action(action, args, true, text) end + + @variables['__DIR__' ] = current_dir + @variables['__FILE__'] = current_file + @variables['__LINE__'] = current_line end # Executes the specified action handling errors, logging and debug # history. # @@ -323,12 +335,12 @@ # # For example: # ctx.exec_action 'open "http://www.ruby-lang.org"' # # => navigates to www.ruby-lang.org # - def exec_parsed_action(action, args, log = true, text = nil, file = nil, line = nil) - action = get_action(action, args, text, file, line) + def exec_parsed_action(action, args, log = true, text = nil) + action = get_action(action, args, text) ret = nil if log @logger.log_cmd(action) do Readline::HISTORY << action.text ret = exec_action_object(action) @@ -461,11 +473,11 @@ # # This method if part of the action execution chain and is intended # for advanced use (e.g. in complex actions). To execute an Action # directly, the #exec_action method is preferred. # - def get_action(action, args, text = nil, file = nil, line = nil) + def get_action(action, args, text = nil) while (alias_action = @aliases[action]) action = alias_action[:action] args = alias_action[:args].map do |a| a.gsub(/\$\{(\d+)(\*q?)?\}/) do |match| # expand ${1} to args[0], ${2} to args[1], etc. @@ -482,12 +494,12 @@ end end end text = ([action] + args.map { |a| '"'+a.gsub('"', '""')+'"' }).join(' ') unless text - file = @variables['__FILE__'] unless file - line = @variables['__LINE__'] unless line + file = @variables['__FILE__'] + line = @variables['__LINE__'] Action.new(self, action, args, text, file, line) end # Executes the specified action object injecting built-in variables. @@ -502,15 +514,10 @@ # action = ctx.get_action("echo", ['Hi!'], 'echo "Hi!"') # ret = ctx.exec_action_object(action) # ret.call if ret.respond_to? :call # def exec_action_object(action) - # Inject built-in variables - file = action.file - dir = (File.exists? file) ? File.dirname(file) : Dir.pwd - @variables['__FILE__'] = file - @variables['__DIR__'] = File.absolute_path(dir) action.execute end # Prints the specified +error+ using the Logger configured and # handling the verbose option. @@ -521,20 +528,25 @@ # rescue StandardError => e # @ctx.print_error e # # => additional error handling code here # end # - def print_error(e) + def print_error(e, capture = true) if @logger @logger.log "#{e.message}\n", :error else puts e.message end if @options[:verbose] p e puts e.backtrace end + if capture and @options[:capture] + with_vars(e.variables) do + exec_parsed_action('capture', [] , false) + end + end end # ======================================================================= # # :section: Metadata # ======================================================================= # @@ -654,9 +666,15 @@ # def with_vars(vars) current = @variables @variables = @variables.merge(vars) yield + rescue StandardError => e + e.instance_variable_set "@variables", @variables + def e.variables + @variables + end + raise ensure @variables = current end private \ No newline at end of file