Best Bacon_ruby code snippet using KnockOutput.after
spec.rb
Source:spec.rb
...232 end233 end234 class Specification235 attr_reader :description236 def initialize(context, description, block, before_filters, after_filters)237 @context = context238 @description = description239 @block = block240 @before_filters = before_filters.dup241 @after_filters = after_filters.dup242 @postponed_blocks_count = 0243 @ran_spec_block = false244 @ran_after_filters = false245 @exception_occurred = false246 @error = ""247 end248 def postponed?249 @postponed_blocks_count != 0250 end251 def run_before_filters252 execute_block { @before_filters.each { |f| @context.instance_eval(&f) } }253 end254 def run_spec_block255 @ran_spec_block = true256 # If an exception occurred, we definitely don't need to perform the actual spec anymore257 unless @exception_occurred258 execute_block { @context.instance_eval(&@block) }259 end260 finish_spec unless postponed?261 end262 def run_after_filters263 @ran_after_filters = true264 execute_block { @after_filters.each { |f| @context.instance_eval(&f) } }265 end266 def run267 Bacon.handle_requirement_begin(@description)268 Counter[:depth] += 1269 run_before_filters270 @number_of_requirements_before = Counter[:requirements]271 run_spec_block unless postponed?272 end273 def schedule_block(seconds, &block)274 # If an exception occurred, we definitely don't need to schedule any more blocks275 unless @exception_occurred276 @postponed_blocks_count += 1277 unless Platform.android?278 performSelector("run_postponed_block:", withObject:block, afterDelay:seconds)279 else280 sleep seconds281 run_postponed_block(block)282 end283 end284 end285 def postpone_block(timeout = 1, &block)286 # If an exception occurred, we definitely don't need to schedule any more blocks287 unless @exception_occurred288 if @postponed_block289 raise "Only one indefinite `wait' block at the same time is allowed!"290 else291 @postponed_blocks_count += 1292 @postponed_block = block293 unless Platform.android?294 performSelector("postponed_block_timeout_exceeded", withObject:nil, afterDelay:timeout)295 else296 sleep timeout297 postponed_block_timeout_exceeded298 end299 end300 end301 end302 def postpone_block_until_change(object_to_observe, key_path, timeout = 1, &block)303 # If an exception occurred, we definitely don't need to schedule any more blocks304 unless @exception_occurred305 if @postponed_block306 raise "Only one indefinite `wait' block at the same time is allowed!"307 else308 @postponed_blocks_count += 1309 @postponed_block = block310 @observed_object_and_key_path = [object_to_observe, key_path]311 object_to_observe.addObserver(self, forKeyPath:key_path, options:0, context:nil)312 unless Platform.android?313 performSelector("postponed_change_block_timeout_exceeded", withObject:nil, afterDelay:timeout)314 else315 sleep timeout316 postponed_change_block_timeout_exceeded317 end318 end319 end320 end321 def observeValueForKeyPath(key_path, ofObject:object, change:_, context:__)322 resume323 end324 def postponed_change_block_timeout_exceeded325 remove_observer!326 postponed_block_timeout_exceeded327 end328 def remove_observer!329 if @observed_object_and_key_path330 object, key_path = @observed_object_and_key_path331 object.removeObserver(self, forKeyPath:key_path)332 @observed_object_and_key_path = nil333 end334 end335 def postponed_block_timeout_exceeded336 cancel_scheduled_requests!337 execute_block { raise Error.new(:failed, "timeout exceeded: #{@context.name} - #{@description}") }338 @postponed_blocks_count = 0339 finish_spec340 end341 def resume342 unless Platform.android?343 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_block_timeout_exceeded', object:nil)344 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector:'postponed_change_block_timeout_exceeded', object:nil)345 end346 remove_observer!347 block = @postponed_block348 @postponed_block = nil349 run_postponed_block(block)350 end351 def run_postponed_block(block)352 # If an exception occurred, we definitely don't need execute any more blocks353 execute_block(&block) unless @exception_occurred354 @postponed_blocks_count -= 1355 unless postponed?356 if @ran_after_filters357 exit_spec358 elsif @ran_spec_block359 finish_spec360 else361 run_spec_block362 end363 end364 end365 def finish_spec366 if !@exception_occurred && Counter[:requirements] == @number_of_requirements_before367 # the specification did not contain any requirements, so it flunked368 execute_block { raise Error.new(:missing, "empty specification: #{@context.name} #{@description}") }369 end370 run_after_filters371 exit_spec unless postponed?372 end373 def cancel_scheduled_requests!374 unless Platform.android?375 NSObject.cancelPreviousPerformRequestsWithTarget(@context)376 NSObject.cancelPreviousPerformRequestsWithTarget(self)377 end378 end379 def exit_spec380 cancel_scheduled_requests!381 Counter[:depth] -= 1382 Bacon.handle_requirement_end(@error)383 @context.specification_did_finish(self)384 end385 def execute_block386 begin387 yield388 rescue Object => e389 @exception_occurred = true390 if e.is_a?(Exception)391 ErrorLog << "#{e.class}: #{e.message}\n"392 lines = $DEBUG ? e.backtrace : e.backtrace.find_all { |line| line !~ /bin\/macbacon|\/mac_bacon\.rb:\d+/ }393 lines.each_with_index { |line, i|394 ErrorLog << "\t#{line}#{i==0 ? ": #{@context.name} - #{@description}" : ""}\n"395 }396 ErrorLog << "\n"397 else398 unless Platform.android?399 # Pure NSException.400 ErrorLog << "#{e.name}: #{e.reason}\n"401 else402 # Pure Java exception.403 ErrorLog << "#{e.class.toString} : #{e.getMessage}"404 end405 end406 @error = if e.is_a? Error407 Counter[e.count_as] += 1408 "#{e.count_as.to_s.upcase} - #{e}"409 else410 Counter[:errors] += 1411 "ERROR: #{e.class} - #{e}"412 end413 end414 end415 end416 class Platform417 def self.android?418 defined?(NSObject) ? false : true419 end420 end421 def self.add_context(context)422 (@contexts ||= []) << context423 end424 def self.current_context_index425 @current_context_index ||= 0426 end427 def self.current_context428 @contexts[current_context_index]429 end430 def self.run(arg=nil)431 unless respond_to?(:handle_specification_begin)432 extend(Outputs[ENV['output']] || SpecDoxOutput)433 end434 @timer ||= Time.now435 unless Platform.android?436 Counter[:context_depth] += 1437 handle_specification_begin(current_context.name)438 current_context.performSelector("run", withObject:nil, afterDelay:0)439 else440 @main_activity ||= arg441 @contexts.each do |context|442 Counter[:context_depth] += 1443 handle_specification_begin(context.name)444 context.run445 handle_specification_end446 Counter[:context_depth] -= 1447 end448 handle_summary449 end450 end451 # Android-only.452 def self.main_activity453 @main_activity454 end455 def self.context_did_finish(context)456 unless Platform.android?457 handle_specification_end458 Counter[:context_depth] -= 1459 if (@current_context_index + 1) < @contexts.size460 @current_context_index += 1461 run462 else463 # DONE464 handle_summary465 unless Platform.android?466 exit(Counter.values_at(:failed, :errors).inject(:+))467 else468 # In Android there is no need to exit as we terminate the activity right after Bacon.469 end470 end471 end472 end473 class Context474 attr_reader :name, :block475 def initialize(name, before = nil, after = nil, &block)476 @name = name477 @before = (before ? before.dup : [])478 @after = (after ? after.dup : [])479 @block = block480 @specifications = []481 @current_specification_index = 0482 Bacon.add_context(self)483 instance_eval(&block)484 end485 def run486 # TODO487 #return unless name =~ RestrictContext488 unless Platform.android?489 if spec = current_specification490 spec.performSelector("run", withObject:nil, afterDelay:0)491 else492 Bacon.context_did_finish(self)493 end494 else495 @specifications.each do |spec|496 spec.run497 end498 Bacon.context_did_finish(self)499 end500 end501 def current_specification502 @specifications[@current_specification_index]503 end504 def specification_did_finish(spec)505 unless Platform.android?506 if (@current_specification_index + 1) < @specifications.size507 @current_specification_index += 1508 run509 else510 Bacon.context_did_finish(self)511 end512 end513 end514 def before(&block); @before << block; end515 def after(&block); @after << block; end516 def behaves_like(*names)517 names.each { |name| instance_eval(&Shared[name]) }518 end519 def it(description, &block)520 return unless description =~ RestrictName521 block ||= proc { should.flunk "not implemented" }522 Counter[:specifications] += 1523 @specifications << Specification.new(self, description, block, @before, @after)524 end525 def should(*args, &block)526 if Counter[:depth]==0527 it('should '+args.first, &block)528 else529 super(*args, &block)530 end531 end532 def describe(*args, &block)533 context = Bacon::Context.new(args.join(' '), @before, @after, &block)534 # FIXME: fix RM-879 and RM-806535 unless Platform.android?536 (parent_context = self).methods(false).each { |e|537 class<<context; self end.send(:define_method, e) { |*args| parent_context.send(e, *args) }538 }539 end540 context541 end542 def wait(seconds = nil, &block)543 if seconds544 current_specification.schedule_block(seconds, &block)545 else546 current_specification.postpone_block(&block)547 end...
bacon.rb
Source:bacon.rb
...107 attr_reader :name, :block108 109 def initialize(name, &block)110 @name = name111 @before, @after = [], []112 @block = block113 end114 115 def run116 return unless name =~ RestrictContext117 Bacon.handle_specification(name) { instance_eval(&block) }118 self119 end120 def before(&block); @before << block; end121 def after(&block); @after << block; end122 def behaves_like(*names)123 names.each { |name| instance_eval(&Shared[name]) }124 end125 def it(description, &block)126 return unless description =~ RestrictName127 block ||= lambda { should.flunk "not implemented" }128 Counter[:specifications] += 1129 run_requirement description, block130 end131 132 def should(*args, &block)133 if Counter[:depth]==0134 it('should '+args.first,&block)135 else136 super(*args,&block)137 end138 end139 def run_requirement(description, spec)140 Bacon.handle_requirement description do141 begin142 Counter[:depth] += 1143 rescued = false144 begin145 @before.each { |block| instance_eval(&block) }146 prev_req = Counter[:requirements]147 instance_eval(&spec)148 rescue Object => e149 rescued = true150 raise e151 ensure152 if Counter[:requirements] == prev_req and not rescued153 raise Error.new(:missing,154 "empty specification: #{@name} #{description}")155 end156 begin157 @after.each { |block| instance_eval(&block) }158 rescue Object => e159 raise e unless rescued160 end161 end162 rescue => e163 ErrorLog << "#{e.class}: #{e.message}\n"164 if e.backtrace165 e.backtrace.find_all do |line| 166 line !~ /bin\/bacon|\/bacon\.rb:\d+/ 167 end.each_with_index do |line, i|168 ErrorLog << "\t#{line}#{i==0 ? ": #@name - #{description}" : ""}\n"169 end170 end171 ErrorLog << "\n"172 if e.kind_of? Error173 Counter[e.count_as] += 1174 e.count_as.to_s.upcase175 else176 Counter[:errors] += 1177 "ERROR: #{e.class}"178 end179 else180 ""181 ensure182 Counter[:depth] -= 1183 end184 end185 end186 def describe(*args, &block)187 context = Bacon::Context.new(args.join(' '), &block)188 @before.each { |b| context.before(&b) }189 @after.each { |b| context.after(&b) }190 context.run191 end192 def raise?(*args, &block); block.raise?(*args); end193 def throw?(*args, &block); block.throw?(*args); end194 def change?(*args, &block); block.change?(*args); end195 end196end197class Object198 def true?; false; end199 def false?; false; end200end201class TrueClass202 def true?; true; end203end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!