You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. require 'capybara/rails'
  19. require 'fileutils'
  20. require 'timeout'
  21. module Redmine
  22. module UiTest
  23. module Downloads
  24. DOWNLOADS_PATH = File.expand_path(File.join(Rails.root, 'tmp', 'downloads'))
  25. def clear_downloaded_files
  26. FileUtils.rm downloaded_files
  27. end
  28. def downloaded_files
  29. Dir.glob("#{DOWNLOADS_PATH}/*").reject {|f| f=~/crdownload$/}
  30. end
  31. def downloaded_file
  32. Timeout.timeout(5) do
  33. while downloaded_files.empty?
  34. sleep 0.2
  35. end
  36. end
  37. downloaded_files.first
  38. end
  39. end
  40. end
  41. end
  42. FileUtils.mkdir_p Redmine::UiTest::Downloads::DOWNLOADS_PATH
  43. Capybara.register_driver :chrome do |app|
  44. Capybara::Selenium::Driver.new(app,
  45. :browser => :chrome,
  46. :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
  47. 'chromeOptions' => {
  48. 'args' => [ "--window-size=1024,900" ],
  49. 'prefs' => {
  50. 'download.default_directory' => Redmine::UiTest::Downloads::DOWNLOADS_PATH,
  51. 'download.prompt_for_download' => false,
  52. 'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
  53. }
  54. }
  55. )
  56. )
  57. end
  58. Capybara.default_driver = :chrome
  59. # default: 2
  60. Capybara.default_wait_time = 2
  61. module Redmine
  62. module UiTest
  63. # Base class for UI tests
  64. class Base < ActionDispatch::IntegrationTest
  65. include Capybara::DSL
  66. include Redmine::UiTest::Downloads
  67. # Stop ActiveRecord from wrapping tests in transactions
  68. # Transactional fixtures do not work with Selenium tests, because Capybara
  69. # uses a separate server thread, which the transactions would be hidden
  70. self.use_transactional_tests = false
  71. # Should not depend on locale since Redmine displays login page
  72. # using default browser locale which depend on system locale for "real" browsers drivers
  73. def log_user(login, password)
  74. visit '/my/page'
  75. assert_equal '/login', current_path
  76. within('#login-form form') do
  77. fill_in 'username', :with => login
  78. fill_in 'password', :with => password
  79. find('input[name=login]').click
  80. end
  81. assert_equal '/my/page', current_path
  82. end
  83. setup do
  84. clear_downloaded_files
  85. Setting.delete_all
  86. Setting.clear_cache
  87. end
  88. teardown do
  89. Capybara.reset_sessions! # Forget the (simulated) browser state
  90. Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
  91. Setting.delete_all
  92. Setting.clear_cache
  93. end
  94. end
  95. end
  96. end