19
async makes your tests fun Michael Reinsch [email protected] github / twitter: mreinsch

async makes your tests fun - acceptance testing with capybara

Embed Size (px)

Citation preview

Page 1: async makes your tests fun - acceptance testing with capybara

async makes your tests fun

Michael [email protected]

github / twitter: mreinsch

Page 2: async makes your tests fun - acceptance testing with capybara

capybara acceptance testing

Page 3: async makes your tests fun - acceptance testing with capybara

scenario "publish event" do visit event_url(event) click_on "Publish Event" expect(event.reload).to be_published end

Page 4: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(event.reload).to be_published end

Page 5: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(event.reload).to be_published end

!

Page 6: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(event.reload).to be_published end

!Failure/Error: expect(event.reload).to be_published

Page 7: async makes your tests fun - acceptance testing with capybara

rspec process

Page 8: async makes your tests fun - acceptance testing with capybara

rspec process

rails server process

spawns

Page 9: async makes your tests fun - acceptance testing with capybara

rspec process

rails server process

spawns

browser process

spawns / sends

commands

Page 10: async makes your tests fun - acceptance testing with capybara

rspec process

rails server process

spawns

browser process

spawns / sends

commands

http requests

Page 11: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(page).to have_content("Registration: Open") expect(event.reload).to be_published end

Page 12: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(page).to have_content("Registration: Open") expect(event.reload).to be_published end

"

Page 13: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(page).to have_content("Registration: Open") expect(event.reload).to be_published end

Failure/Error: Unable to find matching line from backtrace ActiveRecord::StatementInvalid: PG::TRDeadlockDetected: ERROR: deadlock detected DETAIL: Process 7186 waits for AccessShareLock on relation 16570 of database 16396; blocked by process 7274. Process 7274 waits for AccessExclusiveLock on relation 17012 of database 16396; blocked by process 7186.

#

Page 14: async makes your tests fun - acceptance testing with capybara

ajax

vs.

database_cleaner

Page 15: async makes your tests fun - acceptance testing with capybara

# in spec/spec_helper.rb

config.append_after(:each) do Rails.logger.debug("======= cleaning DB") DatabaseCleaner.clean end

Page 16: async makes your tests fun - acceptance testing with capybara

module RSpecLoggerListener extend self

def start(notification) Rails.logger.info("\n\n====== Starting new test run") end def stop(notification) Rails.logger.info("\n\n====== Test run finished") end def example_started(notification) Rails.logger.info("\n\n====== START #{notification.example.full_description}") end def example_passed(notification) Rails.logger.info("====== PASSED #{notification.example.full_description}") end def example_failed(notification) Rails.logger.info("====== FAILED #{notification.example.full_description}") end def example_pending(notification) Rails.logger.info("====== PENDING #{notification.example.full_description}") end end

RSpec.configuration.reporter.register_listener( RSpecLoggerListener, :start, :stop, :example_started, :example_passed, :example_failed, :example_pending)

get it from: https://gist.github.com/mreinsch/2c8dbb01e51c32c3c5c8

Page 17: async makes your tests fun - acceptance testing with capybara

module CapybaraHelperMethods def wait_for_ajax counter = 0 while !finished_all_ajax_requests? counter += 1 sleep 0.2 raise "AJAX request took too long." if counter >= (Capybara.default_wait_time * 5) end end

def finished_all_ajax_requests? active = page.evaluate_script <<-SCRIPT.strip.gsub(/\s+/,' ') (function () { if (typeof jQuery != 'undefined') { return jQuery.active; } else { console.error("jQuery was undefined on " + document.URL + " - will retry..."); return -1; } })() SCRIPT

active && active.zero? end end

RSpec.configuration.include CapybaraHelperMethods, :type => :feature

based on https://coderwall.com/p/aklybw/wait-for-ajax-with-capybara-2-0

Page 18: async makes your tests fun - acceptance testing with capybara

scenario "publish event", js: true do visit event_url(event) click_on "Publish Event" expect(page).to have_content("Registration: Open") expect(event.reload).to be_published wait_for_ajax end

"

Page 19: async makes your tests fun - acceptance testing with capybara

Beware of the async!

Michael [email protected] github / twitter: mreinsch