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.

application_system_test_case.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative 'test_helper'
  19. class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  20. DOWNLOADS_PATH = File.expand_path(File.join(Rails.root, 'tmp', 'downloads'))
  21. # Allow running Capybara default server on custom IP address and/or port
  22. Capybara.server_host = ENV['CAPYBARA_SERVER_HOST'] if ENV['CAPYBARA_SERVER_HOST']
  23. Capybara.server_port = ENV['CAPYBARA_SERVER_PORT'] if ENV['CAPYBARA_SERVER_PORT']
  24. # Allow defining Google Chrome options arguments based on a comma-delimited string environment variable
  25. GOOGLE_CHROME_OPTS_ARGS = ENV['GOOGLE_CHROME_OPTS_ARGS'].present? ? ENV['GOOGLE_CHROME_OPTS_ARGS'].split(",") : []
  26. options = {}
  27. if ENV['SELENIUM_REMOTE_URL']
  28. options[:url] = ENV['SELENIUM_REMOTE_URL']
  29. options[:browser] = :remote
  30. end
  31. # Allow running tests using a remote Selenium hub
  32. driven_by :selenium, using: :chrome, screen_size: [1024, 900], options: options do |driver_option|
  33. GOOGLE_CHROME_OPTS_ARGS.each do |arg|
  34. driver_option.add_argument arg
  35. end
  36. driver_option.add_preference 'download.default_directory', DOWNLOADS_PATH.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
  37. driver_option.add_preference 'download.prompt_for_download', false
  38. driver_option.add_preference 'plugins.plugins_disabled', ["Chrome PDF Viewer"]
  39. end
  40. setup do
  41. Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}"
  42. # Allow defining a custom app host (useful when using a remote Selenium hub)
  43. if ENV['CAPYBARA_APP_HOST']
  44. Capybara.configure do |config|
  45. config.app_host = ENV['CAPYBARA_APP_HOST']
  46. end
  47. end
  48. clear_downloaded_files
  49. Setting.delete_all
  50. Setting.clear_cache
  51. end
  52. teardown do
  53. Setting.delete_all
  54. Setting.clear_cache
  55. end
  56. # Should not depend on locale since Redmine displays login page
  57. # using default browser locale which depend on system locale for "real" browsers drivers
  58. def log_user(login, password)
  59. visit '/my/page'
  60. assert_equal '/login', current_path
  61. within('#login-form form') do
  62. fill_in 'username', :with => login
  63. fill_in 'password', :with => password
  64. find('input[name=login]').click
  65. end
  66. assert_equal '/my/page', current_path
  67. end
  68. def wait_for_ajax
  69. Timeout.timeout(Capybara.default_max_wait_time) do
  70. loop until page.evaluate_script("jQuery.active").zero?
  71. end
  72. end
  73. def clear_downloaded_files
  74. FileUtils.rm downloaded_files
  75. end
  76. def downloaded_files(filename='*')
  77. Dir.glob("#{DOWNLOADS_PATH}/#{filename}").
  78. reject{|f| f=~/\.(tmp|crdownload)$/}.sort_by{|f| File.mtime(f)}
  79. end
  80. # Returns the path of the download file
  81. def downloaded_file(filename='*')
  82. files = []
  83. Timeout.timeout(5) do
  84. loop do
  85. files = downloaded_files(filename)
  86. break if files.present?
  87. sleep 0.2
  88. end
  89. end
  90. files.last
  91. end
  92. end
  93. FileUtils.mkdir_p ApplicationSystemTestCase::DOWNLOADS_PATH