33
capybara

Functional testing with capybara

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Functional testing with capybara

capybara

Page 2: Functional testing with capybara
Page 3: Functional testing with capybara

overviewautomated user interaction

automatically waits for asynchronous tasks

driver agnostic

high level access to ui elements

Page 4: Functional testing with capybara

test frameworkscucumber

rspec

test::unit

minitest::spec

Page 5: Functional testing with capybara

drivers

Page 6: Functional testing with capybara

rack_testdefault

fast, no server

no js

mechanizerack_test with remote server

Page 7: Functional testing with capybara

selenium2.0 aka webdriver

firefox, chrome, ie, ...

Page 8: Functional testing with capybara

webkitheadless

QtWebKitk

logging / messages

screenshots

cookies

resizing the window

Page 9: Functional testing with capybara

poltergeistheadless

phantomjs

screenshots

resizing the window

remote debugging (web inspector)

no X (CI integration)

Page 10: Functional testing with capybara

dsl

Page 11: Functional testing with capybara

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

Page 12: Functional testing with capybara

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

Page 13: Functional testing with capybara

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')

Page 14: Functional testing with capybara

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')

Page 15: Functional testing with capybara

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

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

Page 16: Functional testing with capybara

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

Page 17: Functional testing with capybara

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

Page 18: Functional testing with capybara

debuggingsave_and_open_page # snapshotprint page.html

Page 19: Functional testing with capybara

setup

Page 20: Functional testing with capybara

Capybara.default_driver = :selenium

# sinatraCapybara.app = App

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

Page 21: Functional testing with capybara

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

Page 22: Functional testing with capybara

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

Page 23: Functional testing with capybara

in the wild

Page 24: Functional testing with capybara

metaflopweb based platform for metafonts and

related type projects

modulator

experimental font generation

export as otf, webfont

Page 25: Functional testing with capybara

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

Page 26: Functional testing with capybara

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

Page 27: Functional testing with capybara

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

Page 28: Functional testing with capybara

problems

Page 29: Functional testing with capybara

generaldialogs (downloads)

new openend windows

driver specialties

Page 30: Functional testing with capybara

seleniumslow, startup

slow, random errors

no http response

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

installation

Page 31: Functional testing with capybara

solutionavoid selenium

use headless

test visual concerns manually