Functional testing with capybara

Preview:

DESCRIPTION

Functional testing with capybara, shown by examples from the metaflop project.

Citation preview

capybara

overviewautomated user interaction

automatically waits for asynchronous tasks

driver agnostic

high level access to ui elements

test frameworkscucumber

rspec

test::unit

minitest::spec

drivers

rack_testdefault

fast, no server

no js

mechanizerack_test with remote server

selenium2.0 aka webdriver

firefox, chrome, ie, ...

webkitheadless

QtWebKitk

logging / messages

screenshots

cookies

resizing the window

poltergeistheadless

phantomjs

screenshots

resizing the window

remote debugging (web inspector)

no X (CI integration)

dsl

navigatingvisit('/projects')visit(post_comments_path(post))

clicking links and buttonsclick_link('id-of-link')click_link('Link Text')click_button('Save')click_on('Link Text') # links or buttons

interacting with formsfill_in('Username', :with => 'user')choose('A Radio Button')check('A Checkbox')attach_file('Image', '/path/to/image.jpg')select('Option', :from => 'Select Box')

querying (rspec matchers)page.should have_selector('table tr')page.should have_selector( :xpath, '//table/tr')page.should have_xpath('//table/tr')page.should have_css('table tr.foo')page.should have_content('foo')

findingfind_field('First Name').valuefind_link('Hello').visible?find_button('Send').click

find("#overlay").find("h1").clickall('a').each { |a| a[:href] }

scopingwithin("li#employee") do fill_in 'Name', :with => 'Jimmy'end

scriptingpage.execute_script("$('body').empty()")result = page.evaluate_script('4 + 4');

debuggingsave_and_open_page # snapshotprint page.html

setup

Capybara.default_driver = :selenium

# sinatraCapybara.app = App

# remote appCapybara.app_host = 'http://www.google.com'

per specCapybara.javascript_driver = :selenium

# ...

describe 'requires js', :js => true do it 'will use the default js driver' it 'will switch to one specific driver', :driver => :webkitend

set browserCapybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new( app, :browser => :chrome )end

in the wild

metaflopweb based platform for metafonts and

related type projects

modulator

experimental font generation

export as otf, webfont

spec sample 1context 'when i change the unit width' do it 'should show the loading indicator' do fill_in 'param-unit-width', :with => 2 page.should have_selector( '.preview-loading-text') endend

spec sample 2context 'when i enable anatomy' do it 'shows the anatomy image' do within '#menu' do click_link 'on' end page.should have_selector '#info-panel' endend

spec sample 3context 'when i click the "webfont" link' do it 'should call the font generator url' do click_link('webfont') current_url.should include 'modulator/export/font/web' endend

problems

generaldialogs (downloads)

new openend windows

driver specialties

seleniumslow, startup

slow, random errors

no http response

no .trigger() (e.g. mouse over)

installation

solutionavoid selenium

use headless

test visual concerns manually

Recommended