Skip to content

Commit

Permalink
clean up test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Aug 27, 2024
1 parent 339ff0e commit 5a14853
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rubyonrails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
run: |
bundle exec rake db:reset
bundle exec rake db:seed
bundle exec rake test
bundle exec rake test:system
bundle exec rake test TESTOPTS="-v"
bundle exec rake test:system TESTOPTS="-v"
- name: Run tests with mysql
env:
RAILS_ENV: test
Expand All @@ -53,5 +53,5 @@ jobs:
run: |
bundle exec rake db:reset
bundle exec rake db:seed
bundle exec rake test
bundle exec rake test:system
bundle exec rake test TESTOPTS="-v"
bundle exec rake test:system TESTOPTS="-v"
24 changes: 17 additions & 7 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,41 @@
require 'test_helper'

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400] do |options|
def setup

Capybara.server_host = "0.0.0.0"
Capybara.app_host = "http://#{IPSocket.getaddress(Socket.gethostname)}" if ENV["SELENIUM_SERVER"].present?
Capybara.default_max_wait_time = 5
Capybara.save_path = "tmp/test-screenshots"

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=1280,800')

# Add preferences
options.add_preference(:download, {
prompt_for_download: false,
default_directory: File.join(Rails.root, 'tmp')
})
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })

# Remote driver setup
Capybara.register_driver :selenium do |app|
Capybara.register_driver :my_selenium do |app|
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: "http://#{ENV.fetch('SELENIUM_SERVER')}:4444",
options: options
)
end
end

def setup
super

user = FactoryGirl.create(:user)
login_as(user)
login_as(user, role: User::STAFF)
end

Capybara.save_path = "tmp/test-screenshots"

driven_by :my_selenium

end
18 changes: 3 additions & 15 deletions test/helpers/system_test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@

## HELPERS FOR SYSTEM TESTS
module SystemTestHelper

## Chosen Js Plugin selector
def select_chosen_option(dropdown_selector, option_text)
select_element = find(dropdown_selector)
select_element.click
within "#{dropdown_selector} .chosen-results" do
find('li', text: option_text).click
end
assert_selector "#{dropdown_selector} .chosen-single", text: option_text
end

def wait_for_download(filename)
loop do
break if File.exist?(filename)
sleep 1
def wait_for_download(filename, timeout=10)
Timeout.timeout(timeout) do
sleep 0.1 until File.exist?(filename)
end
end
end
4 changes: 2 additions & 2 deletions test/system/theses_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class ThesesTest < ApplicationSystemTestCase
click_link("Under Review Theses")
assert_selector 'a', text: 'Download Excel'
click_link("Download Excel")
filename = "tmp/theses_report.xlsx"
filename = File.join(Rails.root, "tmp/theses_report.xlsx")
wait_for_download(filename)
assert File.exist?(filename), "Expected file #{filename} to be downloaded"
File.delete("tmp/theses_report.xlsx")
File.delete(filename)
end

test 'Assign a thesis to ME and unassign this' do
Expand Down
14 changes: 1 addition & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@
require 'minitest/unit'
require 'mocha/minitest'
require 'database_cleaner/active_record'
require 'capybara/rails'
require 'capybara/minitest'
require 'rails-controller-testing'

# Configure shoulda-matchers to use Minitest
require 'shoulda/matchers'

Rails::Controller::Testing.install

DatabaseCleaner.url_allowlist = [
%r{.*test.*}
]
DatabaseCleaner.strategy = :truncation

Capybara.server_host = '0.0.0.0'
Capybara.app_host = "http://#{Socket.gethostname}:#{Capybara.server_port}"
Capybara.default_max_wait_time = 20

include ActionDispatch::TestProcess

Shoulda::Matchers.configure do |config|
Expand All @@ -49,18 +44,11 @@ class TestCase
include Warden::Test::Helpers
Warden.test_mode!

# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Make `assert_*` methods behave like Minitest assertions
include Capybara::Minitest::Assertions

setup do
DatabaseCleaner.start
end

teardown do
Capybara.reset_sessions!
Capybara.use_default_driver
DatabaseCleaner.clean
end
end
Expand Down

0 comments on commit 5a14853

Please sign in to comment.