From e7e88f8f36285a0e53d10384f6657de8a82db876 Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Mon, 12 May 2014 11:59:39 +0100 Subject: [PATCH] use modified steps instead of patching capybara for i18n --- config/initializers/i18n_in_cukes.rb | 89 ------------------------ features/session/user_session.feature | 4 +- features/step_definitions/web_steps.rb | 8 ++- features/support/cuke_i18n.rb | 7 ++ features/support/login_steps.rb | 2 +- features/users/lost_password.feature | 2 +- features/users/user_auth.feature | 2 +- features/users/user_registration.feature | 2 +- 8 files changed, 19 insertions(+), 97 deletions(-) delete mode 100644 config/initializers/i18n_in_cukes.rb create mode 100644 features/support/cuke_i18n.rb diff --git a/config/initializers/i18n_in_cukes.rb b/config/initializers/i18n_in_cukes.rb deleted file mode 100644 index b4b18db01b..0000000000 --- a/config/initializers/i18n_in_cukes.rb +++ /dev/null @@ -1,89 +0,0 @@ -#-- encoding: UTF-8 -#-- copyright -# OpenProject is a project management system. -# Copyright (C) 2012-2014 the OpenProject Foundation (OPF) -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License version 3. -# -# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: -# Copyright (C) 2006-2013 Jean-Philippe Lang -# Copyright (C) 2010-2013 the ChiliProject Team -# -# 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. -# -# See doc/COPYRIGHT.rdoc for more details. -#++ - -## -# The following bit of code patches rspec to accept i18n codes in all finders and when looking for text. -# This way labels don't have to be hardcoded which means they can be changed without breaking tests. -# Examples: -# -# Given I click on "t:account.delete" -# When I follow "t:label_register" -# And I press "t:button_login" -# -# Where the i18n file contains the following: -# -# en: -# label_register: "Sign up" -# label_button_login: "Sign in" -# -# account: -# delete: "Delete account" -# -if Rails.env.test? - require 'capybara' - require 'capybara/rspec/matchers' - - Capybara::Node::Finders.module_eval do - def find_with_i18n(*args) - i18n = args[1] - if args.size >= 2 && i18n.is_a?(String) && i18n =~ /^t:[^\s]/ - begin - args[1] = I18n.t(i18n.split(":").last) - find_without_i18n(*args) - rescue Capybara::ElementNotFound - # perhaps it was not intended to be an i18n code after all - args[1] = i18n - find_without_i18n(*args) - end - else - find_without_i18n(*args) - end - end - - alias_method_chain :find, :i18n - end - - Capybara::RSpecMatchers::HaveText.module_eval do - def matches_with_i18n?(actual) - if content.is_a?(String) && content =~ /^t:[^\s]/ - i18n = content - @content = I18n.t(i18n.split(":").last) - matches_without_i18n?(actual) || begin - # perhaps it was not intended to be an i18n code after all - @content = i18n - matches_without_i18n?(actual) - end - else - matches_without_i18n?(actual) - end - end - - alias_method_chain :matches?, :i18n - end -end diff --git a/features/session/user_session.feature b/features/session/user_session.feature index e069f525e4..f8b5a81f86 100644 --- a/features/session/user_session.feature +++ b/features/session/user_session.feature @@ -36,7 +36,7 @@ Feature: User session Then I should be on the login page When I fill in "bob" for "username" within "#login-form" And I fill in "adminADMIN!" for "password" within "#login-form" - And I click on "t:button_login" within "#login-form" + And I click on "t:button_login" within "#login-form" [i18n] And I go to the my account page Then I should be on the my account page @@ -45,7 +45,7 @@ Feature: User session Then I should be on the login page When I fill in "bob" for "username" within "#login-form" And I fill in "adminADMIN!" for "password" within "#login-form" - And I click on "t:button_login" within "#login-form" + And I click on "t:button_login" within "#login-form" [i18n] Then I should be on the my account page Scenario: Autologin works if enabled diff --git a/features/step_definitions/web_steps.rb b/features/step_definitions/web_steps.rb index 1bee1dd168..bd4a07e3cf 100644 --- a/features/step_definitions/web_steps.rb +++ b/features/step_definitions/web_steps.rb @@ -94,10 +94,14 @@ end World(WithinHelpers) # Single-line step scoper -When /^(.*) within (.*[^:])$/ do |step_name, parent| +When /^(.*) within "(.*[^:"])"$/ do |step_name, parent| with_scope(parent) { step step_name } end +When /^(.*) \[i18n\]$/ do |actual_step| + step translate(actual_step) +end + When(/^I ctrl\-click on "([^\"]+)"$/) do |text| #Click all elements that you want, in this case we click all as elements = page.all('a', :text => text) @@ -417,7 +421,7 @@ Given /^I (accept|dismiss) the alert dialog$/ do |method| end Then(/^(.*) in the new window$/) do |step| - new_window=page.driver.browser.window_handles.last + new_window=page.driver.browser.window_handles.last page.within_window new_window do step(step) end diff --git a/features/support/cuke_i18n.rb b/features/support/cuke_i18n.rb new file mode 100644 index 0000000000..544b993bca --- /dev/null +++ b/features/support/cuke_i18n.rb @@ -0,0 +1,7 @@ +module CukeI18n + def translate(step) + step.gsub(/t:[\w]+/) { |code| I18n.t(code.split(":").last) } + end +end + +World(CukeI18n) diff --git a/features/support/login_steps.rb b/features/support/login_steps.rb index 25c1ea03aa..5b7f0a7cfd 100644 --- a/features/support/login_steps.rb +++ b/features/support/login_steps.rb @@ -34,7 +34,7 @@ module LoginSteps within('#login-form') do fill_in User.human_attribute_name(:login), :with => login fill_in 'Password', :with => password - click_button 't:button_login' + click_button translate('t:button_login') end end end diff --git a/features/users/lost_password.feature b/features/users/lost_password.feature index 71879065d1..00faa3d2a5 100644 --- a/features/users/lost_password.feature +++ b/features/users/lost_password.feature @@ -37,7 +37,7 @@ Feature: Lost Password Scenario: Set a new password using lost password link And I am on the login page - When I follow "t:label_password_lost" within "#login-form" + When I follow "t:label_password_lost" within "#login-form" [i18n] And I fill in "johndoe@example.com" for "Email" And I press "Submit" Then I should see "has been sent to you" diff --git a/features/users/user_auth.feature b/features/users/user_auth.feature index 61194bb19a..b2653be6ca 100644 --- a/features/users/user_auth.feature +++ b/features/users/user_auth.feature @@ -64,7 +64,7 @@ Then I should see "Admin" within "#top-menu-items" Scenario: Lost password notification mail will not be sent in case incorrect mail is given Given I am on the login page And I open the "Openproject Admin" menu -And I follow "t:label_password_lost" within "#login-form" +And I follow "t:label_password_lost" within "#login-form" [i18n] Then I should be on the lost password page And I fill in "mail" with "bilbo@shire.com" And I click on "Submit" diff --git a/features/users/user_registration.feature b/features/users/user_registration.feature index 50a59f8dd9..75d4656b5b 100644 --- a/features/users/user_registration.feature +++ b/features/users/user_registration.feature @@ -51,7 +51,7 @@ Feature: User Registration Background: Given I am on the homepage And I open the "Sign in" menu -And I follow "t:label_register" within "#top-menu-items" +And I follow "t:label_register" within "#top-menu-items" [i18n] Then I should be on the registration page Scenario: A user can register successfully after filling in the registration form