Skip to content

Testing: Integration tests

David Wessman edited this page Jan 19, 2016 · 1 revision

An integration test is a test that runs against the whole rails application. You can think of the test as a simulated web browser. We use capybara to implement this. You find some commands here:

You can tell the web browser to do things like visit /, click_button id: foo_button and so on.

A short example of how to sign in to the app:

visit '/logga_in'
fill_in 'user_username', with: 'MyUsername'
fill_in 'user_password', with: 'MyPassword'
click_button 'Logga in'
page.should have_css('div.alert.alert-info') # Verify we get an alert
find('div.alert.alert-info').text.should eq(' Loggade in. ') # Verify that the alert states we are signed in

To avoid redoing the sign in all the time, you can use this:

let(:login) { LoginPage.new }
let(:user ) { create(:user) }

login.visit_page.login(user, "password")

You can use CSS ids as identifiers (usually the best), names or link texts. Above I used CSS id for the input fields and the name for the submit button.

See spec/features/home_spec.rb for the basic skeleton. It visits the home page and verifies F-sektionen is written on it.

##Cheat sheet

Copied from https://gist.github.com/zhengjia/428105, thanks!

###Navigating visit('/projects') visit(post_comments_path(post))

###Clicking links and buttons click_link('id-of-link') click_link('Link Text') click_button('Save') click('Link Text') # Click either a link or a button click('Button Value')

###Interacting with forms fill_in('First Name', :with => 'John') fill_in('Password', :with => 'Seekrit') fill_in('Description', :with => 'Really Long Text…') choose('A Radio Button') check('A Checkbox') uncheck('A Checkbox') attach_file('Image', '/path/to/image.jpg') select('Option', :from => 'Select Box')

###scoping within("//li[@id='employee']") do fill_in 'Name', :with => 'Jimmy' end within(:css, "li#employee") do fill_in 'Name', :with => 'Jimmy' end within_fieldset('Employee') do fill_in 'Name', :with => 'Jimmy' end within_table('Employee') do fill_in 'Name', :with => 'Jimmy' end

###Querying page.has_xpath?('//table/tr') page.has_css?('table tr.foo') page.has_content?('foo') page.should have_xpath('//table/tr') page.should have_css('table tr.foo') page.should have_content('foo') page.should have_no_content('foo') find_field('First Name').value find_link('Hello').visible? find_button('Send').click find('//table/tr').click locate("//*[@id='overlay'").find("//h1").click all('a').each { |a| a[:href] }

###Scripting result = page.evaluate_script('4 + 4');

###Debugging save_and_open_page

###Asynchronous JavaScript NOTE: Our tests does currently not support JS. Contact @henrikssn if this is an issue.

click_link('foo')
click_link('bar')
page.should have_content('baz')
page.should_not have_xpath('//a')
page.should have_no_xpath('//a')

###XPath and CSS within(:css, 'ul li') { ... } find(:css, 'ul li').text locate(:css, 'input#name').value Capybara.default_selector = :css within('ul li') { ... } find('ul li').text locate('input#name').value