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

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