From 9c1077841eb0747b875794eb468a3aa881f97ff5 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 26 Jan 2013 14:36:02 +0000 Subject: [PATCH] Adds first Capybara tests (#12822). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11270 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- Gemfile | 1 + doc/RUNNING_TESTS | 11 ++++++- lib/tasks/testing.rake | 7 +++++ test/ui/base.rb | 63 ++++++++++++++++++++++++++++++++++++++ test/ui/issues_test.rb | 68 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 test/ui/base.rb create mode 100644 test/ui/issues_test.rb diff --git a/Gemfile b/Gemfile index 324754da7..e82523cba 100644 --- a/Gemfile +++ b/Gemfile @@ -80,6 +80,7 @@ group :test do platforms << :jruby if defined?(JRUBY_VERSION) && JRUBY_VERSION >= "1.7" gem "test-unit", :platforms => platforms gem "mocha", "0.12.3" + gem 'capybara', '~> 2.0.0' end local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local") diff --git a/doc/RUNNING_TESTS b/doc/RUNNING_TESTS index cd216e07c..b6a53542f 100644 --- a/doc/RUNNING_TESTS +++ b/doc/RUNNING_TESTS @@ -9,7 +9,7 @@ Running Tests Run `rake --tasks test` to see available tests. Run `rake test` to run the entire test suite (except the tests for the -Apache perl module Redmine.pm, see below). +Apache perl module Redmine.pm and Capybara tests, see below). You can run `ruby test/unit/issue_test.rb` for running a single test case. @@ -58,3 +58,12 @@ Then, you can run the tests with: If you svn server is not running on localhost, you can use the REDMINE_TEST_DAV_SERVER environment variable to specify another host. + +Running Capybara tests +====================== + +You need to have PhantomJS WebDriver listening on port 4444: +`phantomjs --webdriver 4444` + +Capybara tests can be run with: +`rake test:ui` diff --git a/lib/tasks/testing.rake b/lib/tasks/testing.rake index 474a09092..cfb8e77f9 100644 --- a/lib/tasks/testing.rake +++ b/lib/tasks/testing.rake @@ -100,4 +100,11 @@ namespace :test do t.test_files = FileList['test/integration/routing/*_test.rb'] end Rake::Task['test:rdm_routing'].comment = "Run the routing tests" + + Rake::TestTask.new(:ui => "db:test:prepare") do |t| + t.libs << "test" + t.verbose = true + t.test_files = FileList['test/ui/**/*_test.rb'] + end + Rake::Task['test:ui'].comment = "Run the UI tests with Capybara (PhantomJS listening on port 4444 is required)" end diff --git a/test/ui/base.rb b/test/ui/base.rb new file mode 100644 index 000000000..29bc45b98 --- /dev/null +++ b/test/ui/base.rb @@ -0,0 +1,63 @@ +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../../test_helper', __FILE__) +require 'capybara/rails' + +Capybara.default_driver = :selenium +Capybara.register_driver :selenium do |app| + # Use the following driver definition to test locally using Chrome (also requires chromedriver to be in PATH) + # Capybara::Selenium::Driver.new(app, :browser => :chrome) + # Add :switches => %w[--lang=en] to force default browser locale to English + # Default for Selenium remote driver is to connect to local host on port 4444 + # This can be change using :url => 'http://localhost:9195' if necessary + # PhantomJS 1.8 now directly supports Webdriver Wire API, simply run it with `phantomjs --webdriver 4444` + # Add :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.internet_explorer) to run on Selenium Grid Hub with IE + Capybara::Selenium::Driver.new(app, :browser => :remote) +end + +module Redmine + module UiTest + # Base class for UI tests + class Base < ActionDispatch::IntegrationTest + include Capybara::DSL + + # Stop ActiveRecord from wrapping tests in transactions + # Transactional fixtures do not work with Selenium tests, because Capybara + # uses a separate server thread, which the transactions would be hidden + self.use_transactional_fixtures = false + + # Should not depend on locale since Redmine displays login page + # using default browser locale which depend on system locale for "real" browsers drivers + def log_user(login, password) + visit '/my/page' + assert_equal '/login', current_path + within('#login-form form') do + fill_in 'username', :with => login + fill_in 'password', :with => password + find('input[name=login]').click + end + assert_equal '/my/page', current_path + end + + teardown do + Capybara.reset_sessions! # Forget the (simulated) browser state + Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver + end + end + end +end diff --git a/test/ui/issues_test.rb b/test/ui/issues_test.rb new file mode 100644 index 000000000..43f3536ac --- /dev/null +++ b/test/ui/issues_test.rb @@ -0,0 +1,68 @@ +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../base', __FILE__) + +class Redmine::UiTest::IssuesTest < Redmine::UiTest::Base + fixtures :projects, :users, :roles, :members, :member_roles, + :trackers, :projects_trackers, :enabled_modules, :issue_statuses, :issues, + :enumerations, :custom_fields, :custom_values, :custom_fields_trackers + + # create an issue + def test_add_issue + log_user('jsmith', 'jsmith') + visit new_issue_path(:project_id => 1) + within('form#issue-form') do + select 'Bug', :from => 'Tracker' + select 'Low', :from => 'Priority' + fill_in 'Subject', :with => 'new test issue' + fill_in 'Description', :with => 'new issue' + select '0 %', :from => 'Done' + fill_in 'Due date', :with => '' + select '', :from => 'Assignee' + fill_in 'Searchable field', :with => 'Value for field 2' + # click_button 'Create' would match both 'Create' and 'Create and continue' buttons + find('input[name=commit]').click + end + + # find created issue + issue = Issue.find_by_subject("new test issue") + assert_kind_of Issue, issue + + # check redirection + find 'div#flash_notice', :visible => true, :text => "Issue \##{issue.id} created." + assert_equal issue_path(:id => issue), current_path + + # check issue attributes + assert_equal 'jsmith', issue.author.login + assert_equal 1, issue.project.id + assert_equal IssueStatus.find_by_name('New'), issue.status + assert_equal Tracker.find_by_name('Bug'), issue.tracker + assert_equal IssuePriority.find_by_name('Low'), issue.priority + assert_equal 'Value for field 2', issue.custom_field_value(CustomField.find_by_name('Searchable field')) + end + + def test_preview_issue_description + log_user('jsmith', 'jsmith') + visit new_issue_path(:project_id => 1) + within('form#issue-form') do + fill_in 'Description', :with => 'new issue description' + click_link 'Preview' + end + find 'div#preview fieldset', :visible => true, :text => 'new issue description' + end +end -- 2.39.5