Best Konacha code snippet using Konacha.session
runner_spec.rb
Source:runner_spec.rb
...8 Konacha.should_receive(:formatters) { :formatters }9 Konacha::Reporter.should_receive(:new).with(:formatters)10 described_class.new11 end12 it 'accepts an existing capybara session' do13 instance = described_class.new 'existing_session'14 instance.session.should == 'existing_session'15 end16 end17 describe ".start" do18 it 'sets the Capybara.server_port' do19 Capybara.should_receive(:server_port=).with(Konacha.runner_port)20 Konacha::Runner.any_instance.stub(:run)21 Konacha::Runner.start22 end23 end24 shared_examples_for "Konacha::Runner" do |driver|25 before do26 Konacha.configure do |config|27 config.driver = driver28 config.formatters = [Konacha::Formatter.new(StringIO.new)]29 end30 end31 describe "#run" do32 let(:suite) do33 {'event' => 'suite',34 'type' => 'suite',35 'data' => {36 'title' => 'failure',37 'fullTitle' => 'failure',38 'path' => 'failing_spec.js'39 }}40 end41 let(:suite_end) do42 {'event' => 'suite end',43 'type' => 'suite',44 'data' => {45 'title' => 'failure',46 'fullTitle' => 'failure',47 'path' => 'failing_spec.js'48 }}49 end50 let(:test) do51 {'event' => 'test',52 'type' => 'test',53 'data' => {54 'title' => 'fails',55 'fullTitle' => 'failure fails',56 'parentFullTitle' => 'failure',57 'path' => 'failing_spec.js'}}58 end59 let(:failure) do60 {'event' => 'fail',61 'type' => 'test',62 'data' => {63 'duration' => anything,64 'title' => 'fails',65 'fullTitle' => 'failure fails',66 'parentFullTitle' => 'failure',67 'status' => 'failed',68 'path' => 'failing_spec.js',69 'error' => {'message' => 'expected 4 to equal 5', 'name' => 'AssertionError'}}}70 end71 let(:error_async) do72 {'event' => 'fail',73 'type' => 'test',74 'data' => {75 'title' => 'errors asynchronously',76 'fullTitle' => 'failure errors asynchronously',77 'parentFullTitle' => 'failure',78 'status' => 'failed',79 'path' => 'failing_spec.js',80 # Accept anything for 'message' since async errors have URLs, which81 # vary on every run, and line #, which may change in Chai releases.82 'error' => {'message' => anything(), 'name' => 'Error'}}}83 end84 let(:pass) do85 {'event' => 'pass',86 'type' => 'test',87 'data' => {88 'title' => 'is empty',89 'fullTitle' => 'the body#konacha element is empty',90 'parentFullTitle' => 'the body#konacha element',91 'status' => 'passed',92 'path' => 'body_spec.js.coffee',93 'duration' => anything}}94 end95 let(:pending) do96 {'event' => 'pending',97 'type' => 'test',98 'data' => {99 'title' => 'is pending',100 'fullTitle' => 'pending test is pending',101 'parentFullTitle' => 'pending test',102 'path' => 'pending_spec.js',103 'status' => 'pending'}}104 end105 let(:start) { {'event' => 'start', 'testCount' => kind_of(Integer), 'data' => {} } }106 let(:end_event) { {'event' => 'end', 'data' => {} } }107 it "passes along the right events" do108 subject.reporter.should_receive(:process_mocha_event).with(start)109 subject.reporter.should_receive(:process_mocha_event).with(suite)110 subject.reporter.should_receive(:process_mocha_event).with(suite_end)111 subject.reporter.should_receive(:process_mocha_event).with(test)112 subject.reporter.should_receive(:process_mocha_event).with(failure)113 subject.reporter.should_receive(:process_mocha_event).with(error_async)114 subject.reporter.should_receive(:process_mocha_event).with(pass)115 subject.reporter.should_receive(:process_mocha_event).with(pending)116 subject.reporter.should_receive(:process_mocha_event).with(end_event)117 subject.reporter.stub(:process_mocha_event)118 subject.run119 end120 it 'accepts paths to test' do121 session = double('capybara session')122 session.stub(:evaluate_script).and_return([start, pass, end_event].to_json)123 session.should_receive(:visit).with('/test_path')124 instance = described_class.new session125 instance.run('/test_path')126 end127 end128 end129 describe "with selenium" do130 it_behaves_like "Konacha::Runner", :selenium131 end132 describe "with poltergeist" do133 it_behaves_like "Konacha::Runner", :poltergeist134 end135end...
runner.rb
Source:runner.rb
...29 UI.info "#{action}ing Konacha", :reset => true30 spawn_konacha31 end32 def kill_konacha33 clear_session!34 if @process35 @process.stop(5)36 UI.info "Konacha Stopped", :reset => true37 end38 end39 def run(paths=[])40 return UI.info("Konacha server not running") unless konacha_running?41 UI.info "Konacha Running: #{paths.empty? ? 'All tests' : paths.join(' ')}"42 urls = paths.map { |p| konacha_url(p) }43 urls = [konacha_url] if paths.empty?44 test_results = {45 :examples => 0,46 :failures => 0,47 :pending => 0,48 :duration => 049 }50 urls.each_with_index do |url, index|51 individual_result = run_tests(url, paths[index])52 test_results[:examples] += individual_result[:examples]53 test_results[:failures] += individual_result[:failures]54 test_results[:pending] += individual_result[:pending]55 test_results[:duration] += individual_result[:duration]56 end57 result_line = "#{test_results[:examples]} examples, #{test_results[:failures]} failures"58 result_line << ", #{test_results[:pending]} pending" if test_results[:pending] > 059 text = [60 result_line,61 "in #{"%.2f" % test_results[:duration]} seconds"62 ].join "\n"63 UI.info text if urls.length > 164 if @options[:notification]65 image = test_results[:failures] > 0 ? :failed : :success66 ::Guard::Notifier.notify(text, :title => 'Konacha Specs', :image => image )67 end68 end69 EMPTY_RESULT = {70 :examples => 0,71 :failures => 0,72 :pending => 0,73 :duration => 0,74 }75 def run_tests(url, path)76 session.reset!77 unless valid_spec? url78 UI.warning "No spec found for: #{path}"79 return EMPTY_RESULT80 end81 runner = ::Konacha::Runner.new session82 runner.run url83 return {84 :examples => runner.reporter.example_count,85 :failures => runner.reporter.failure_count,86 :pending => runner.reporter.pending_count,87 :duration => runner.reporter.duration88 }89 rescue => e90 UI.error e.inspect91 @session = nil92 end93 def run_all94 run if @options[:run_all]95 end96 def run_all_on_start97 run_all if @options[:all_on_start]98 end99 private100 def konacha_url(path = nil)101 url_path = path.gsub(/^#{@options[:spec_dir]}\/?/, '').gsub(/\.coffee$/, '').gsub(/\.js$/, '') unless path.nil?102 "#{konacha_base_url}/#{url_path}?mode=runner&unique=#{unique_id}"103 end104 def unique_id105 "#{Time.now.to_i}#{rand(100)}"106 end107 def session108 UI.info "Starting Konacha-Capybara session using #{@options[:driver]} driver, this can take a few seconds..." if @session.nil?109 @session ||= Capybara::Session.new @options[:driver]110 end111 def clear_session!112 return unless @session113 @session.reset!114 @session = nil115 end116 def spawn_konacha_command117 cmd_parts = ''118 cmd_parts << "bundle exec " if bundler?119 cmd_parts << "rake konacha:serve"120 cmd_parts.split121 end122 def spawn_konacha123 unless @process124 @process = ChildProcess.build(*spawn_konacha_command)125 @process.io.inherit! if ::Guard.respond_to?(:options) && ::Guard.options && ::Guard.options[:verbose]126 @process.start127 Timeout::timeout(@options[:spawn_wait]) do128 until konacha_running?129 sleep(0.2)130 end131 end132 end133 end134 def konacha_base_url135 "http://#{@options[:host]}:#{@options[:port]}"136 end137 def konacha_running?138 Net::HTTP.get_response(URI.parse(konacha_base_url))139 rescue Errno::ECONNREFUSED140 end141 def bundler?142 @bundler ||= options[:bundler] != false && File.exist?("#{Dir.pwd}/Gemfile")143 end144 def valid_spec? url145 session.visit url146 konacha_spec = session.evaluate_script('typeof window.top.Konacha')147 konacha_spec == 'object'148 end149 end150 end151end...
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!!