Project

General

Profile

Feature #12822 » capybara.patch

Etienne Massip, 2013-01-12 15:52

View differences:

Gemfile (working copy)
82 82
  platforms << :jruby if defined?(JRUBY_VERSION) && JRUBY_VERSION >= "1.7"
83 83
  gem "test-unit", :platforms => platforms
84 84
  gem "mocha", "0.12.3"
85
  gem 'capybara', '~> 2.0.0'
86
  gem 'database_cleaner'
85 87
end
86 88

  
87 89
local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
test/integration/issues_test.rb (working copy)
17 17

  
18 18
require File.expand_path('../../test_helper', __FILE__)
19 19

  
20
class IssuesTest < ActionController::IntegrationTest
20
class IssuesTest < ActionDispatch::IntegrationTest
21 21
  fixtures :projects,
22 22
           :users,
23 23
           :roles,
......
36 36
  # create an issue
37 37
  def test_add_issue
38 38
    log_user('jsmith', 'jsmith')
39
    get 'projects/1/issues/new', :tracker_id => '1'
40
    assert_response :success
41
    assert_template 'issues/new'
39

  
40
    visit new_issue_path(:project_id => 1)
41

  
42
    within('form#issue-form') do
43

  
44
      select 'Bug', :from => 'Tracker'
45
      select 'Low', :from => 'Priority'
46
      fill_in 'Subject', :with => 'new test issue'
47
      fill_in 'Description', :with => 'new issue'
48
      select '0 %', :from => 'Done'
49
      fill_in 'Due date', :with => ''
50
      select '', :from => 'Assignee'
51
      fill_in 'Searchable field', :with => 'Value for field 2'
52

  
53
      # click_button 'Create' would match both 'Create' and 'Create and continue' buttons
54
      find('input[name=commit]').click
55
    end
42 56

  
43
    post 'projects/1/issues', :tracker_id => "1",
44
                                 :issue => { :start_date => "2006-12-26",
45
                                             :priority_id => "4",
46
                                             :subject => "new test issue",
47
                                             :category_id => "",
48
                                             :description => "new issue",
49
                                             :done_ratio => "0",
50
                                             :due_date => "",
51
                                             :assigned_to_id => "" },
52
                                 :custom_fields => {'2' => 'Value for field 2'}
53 57
    # find created issue
54 58
    issue = Issue.find_by_subject("new test issue")
55 59
    assert_kind_of Issue, issue
56 60

  
57 61
    # check redirection
58
    assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
59
    follow_redirect!
60
    assert_equal issue, assigns(:issue)
62
    find 'div#flash_notice', :visible => true, :text => "Issue \##{issue.id} created."
63
    assert_equal issue_path(:id => issue), current_path
61 64

  
62 65
    # check issue attributes
63 66
    assert_equal 'jsmith', issue.author.login
64 67
    assert_equal 1, issue.project.id
65
    assert_equal 1, issue.status.id
68
    assert_equal IssueStatus.find_by_name('New'), issue.status 
69
    assert_equal Tracker.find_by_name('Bug'), issue.tracker
70
    assert_equal IssuePriority.find_by_name('Low'), issue.priority
71
    assert_equal 'Value for field 2', issue.custom_field_value(CustomField.find_by_name('Searchable field'))
72
  end
73

  
74
  def test_preview_issue_description
75
    log_user('jsmith', 'jsmith')
76

  
77
    visit new_issue_path(:project_id => 1)
78

  
79
    within('form#issue-form') do
80

  
81
      fill_in 'Description', :with => 'new issue description'
82

  
83
      click_link 'Preview'
84
    end
85

  
86
    find 'div#preview fieldset', :visible => true, :text => 'new issue description'
66 87
  end
67 88

  
68 89
  def test_update_issue_form
test/test_helper.rb (working copy)
20 20
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
21 21
require 'rails/test_help'
22 22
require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
23
require 'capybara/rails'
23 24

  
24 25
require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
25 26
include ObjectHelpers
26 27

  
28
# Transactional fixtures do not work with Selenium tests, because Capybara
29
# uses a separate server thread, which the transactions would be hidden
30
# from. We hence use DatabaseCleaner to truncate our test database.
31
DatabaseCleaner.strategy = :truncation
32
Capybara.default_driver = :selenium
33

  
34
# Use the following driver definition to test locally using Chrome (also requires chromedriver to be in PATH)
35
#Capybara.register_driver :selenium do |app|
36
#  Capybara::Selenium::Driver.new(app, :browser => :chrome) # Add :switches => %w[--lang=en] to force default browser locale to English
37
#end
38

  
39
Capybara.register_driver :selenium do |app|
40
  # Default for Selenium remote driver is to connect to local host on port 4444 
41
  # This can be change using :url => 'http://localhost:9195' if necessary
42
  # PhantomJS 1.8 now directly supports Webdriver Wire API, simply run it with 'phantomjs --webdriver 4444'
43
  Capybara::Selenium::Driver.new(app, :browser => :remote)
44
end
45

  
46
class ActionDispatch::IntegrationTest
47
  # Make the Capybara DSL available in all integration tests
48
  include Capybara::DSL
49

  
50
  # Overrides ActiveSupport::TestCase's
51
  # Should not depend on locale since Redmine now displays login page
52
  # using default browser locale which depend on system locale for "real" browsers drivers
53
  def log_user(login, password)
54
    User.anonymous
55
    visit '/my/page'
56
    assert_equal '/login', current_path
57
    within('#login-form form') do
58
      fill_in 'username', :with => login
59
      fill_in 'password', :with => password
60
      find('input[name=login]').click
61
    end
62
    assert_equal '/my/page', current_path
63
  end
64

  
65
  # Stop ActiveRecord from wrapping tests in transactions
66
  self.use_transactional_fixtures = false
67

  
68
  teardown do
69
    DatabaseCleaner.clean       # Truncate the database
70
    Capybara.reset_sessions!    # Forget the (simulated) browser state
71
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
72
  end
73
end
74

  
27 75
class ActiveSupport::TestCase
28
  include ActionDispatch::TestProcess
29
  
76

  
30 77
  # Transactional fixtures accelerate your tests by wrapping each test method
31 78
  # in a transaction that's rolled back on completion.  This ensures that the
32 79
  # test database remains unchanged so your fixtures don't have to be reloaded
(1-1/3)