use modified steps instead of patching capybara for i18n

pull/1270/head
Markus Kahl 11 years ago
parent ca256f28a7
commit e7e88f8f36
  1. 89
      config/initializers/i18n_in_cukes.rb
  2. 4
      features/session/user_session.feature
  3. 8
      features/step_definitions/web_steps.rb
  4. 7
      features/support/cuke_i18n.rb
  5. 2
      features/support/login_steps.rb
  6. 2
      features/users/lost_password.feature
  7. 2
      features/users/user_auth.feature
  8. 2
      features/users/user_registration.feature

@ -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

@ -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

@ -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

@ -0,0 +1,7 @@
module CukeI18n
def translate(step)
step.gsub(/t:[\w]+/) { |code| I18n.t(code.split(":").last) }
end
end
World(CukeI18n)

@ -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

@ -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"

@ -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"

@ -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

Loading…
Cancel
Save