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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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. require 'webdrivers/chromedriver'
  20. class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  21. DOWNLOADS_PATH = File.expand_path(File.join(Rails.root, 'tmp', 'downloads'))
  22. GOOGLE_CHROME_OPTS_ARGS = []
  23. # Allow running Capybara default server on custom IP address and/or port
  24. Capybara.server_host = ENV['CAPYBARA_SERVER_HOST'] if ENV['CAPYBARA_SERVER_HOST']
  25. Capybara.server_port = ENV['CAPYBARA_SERVER_PORT'] if ENV['CAPYBARA_SERVER_PORT']
  26. # Allow defining Google Chrome options arguments based on a comma-delimited string environment variable
  27. GOOGLE_CHROME_OPTS_ARGS = ENV['GOOGLE_CHROME_OPTS_ARGS'].split(",") if ENV['GOOGLE_CHROME_OPTS_ARGS']
  28. options = {}
  29. # Allow running tests using a remote Selenium hub
  30. options[:url] = ENV['SELENIUM_REMOTE_URL'] if ENV['SELENIUM_REMOTE_URL']
  31. options[:desired_capabilities] = Selenium::WebDriver::Remote::Capabilities.chrome(
  32. 'goog:chromeOptions' => {
  33. 'args' => GOOGLE_CHROME_OPTS_ARGS,
  34. 'prefs' => {
  35. 'download.default_directory' => DOWNLOADS_PATH.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR),
  36. 'download.prompt_for_download' => false,
  37. 'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
  38. }
  39. }
  40. )
  41. driven_by(
  42. :selenium, using: :chrome, screen_size: [1024, 900],
  43. options: options
  44. )
  45. setup do
  46. # Allow defining a custom app host (useful when using a remote Selenium hub)
  47. if ENV['CAPYBARA_APP_HOST']
  48. Capybara.configure do |config|
  49. config.app_host = ENV['CAPYBARA_APP_HOST']
  50. end
  51. end
  52. clear_downloaded_files
  53. Setting.delete_all
  54. Setting.clear_cache
  55. end
  56. teardown do
  57. Setting.delete_all
  58. Setting.clear_cache
  59. end
  60. # Should not depend on locale since Redmine displays login page
  61. # using default browser locale which depend on system locale for "real" browsers drivers
  62. def log_user(login, password)
  63. visit '/my/page'
  64. assert_equal '/login', current_path
  65. within('#login-form form') do
  66. fill_in 'username', :with => login
  67. fill_in 'password', :with => password
  68. find('input[name=login]').click
  69. end
  70. assert_equal '/my/page', current_path
  71. end
  72. def wait_for_ajax
  73. Timeout.timeout(Capybara.default_max_wait_time) do
  74. loop until page.evaluate_script("jQuery.active").zero?
  75. end
  76. end
  77. def clear_downloaded_files
  78. FileUtils.rm downloaded_files
  79. end
  80. def downloaded_files(filename='*')
  81. Dir.glob("#{DOWNLOADS_PATH}/#{filename}").
  82. reject{|f| f=~/\.(tmp|crdownload)$/}.sort_by{|f| File.mtime(f)}
  83. end
  84. # Returns the path of the download file
  85. def downloaded_file(filename='*')
  86. files = []
  87. Timeout.timeout(5) do
  88. loop do
  89. files = downloaded_files(filename)
  90. break if files.present?
  91. sleep 0.2
  92. end
  93. end
  94. files.last
  95. end
  96. end
  97. FileUtils.mkdir_p ApplicationSystemTestCase::DOWNLOADS_PATH