From ee02a3c1158277366fe447e28c2fd6e86894e621 Mon Sep 17 00:00:00 2001 From: zhouyu Date: Mon, 15 Apr 2024 12:12:26 -0700 Subject: [PATCH 1/6] setup selenium testing --- docker-compose.yml | 9 ++++++++ spec/features/click_login_link_spec.rb | 31 ++++++++++++++++++++++++++ spec/rails_helper.rb | 27 ++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 spec/features/click_login_link_spec.rb diff --git a/docker-compose.yml b/docker-compose.yml index 9a0e426..ad011fc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,6 +30,7 @@ services: target: development depends_on: - db + - selenium ports: - 3000:3000 restart: always @@ -38,3 +39,11 @@ services: SOLR_URL: http://solr:8983/solr/geodata-test volumes: - ./:/opt/app:delegated + + selenium: + image: selenium/standalone-chrome + ports: + - 4444:4444 + - 7900:7900 + platform: linux/amd64 + shm_size: 2g diff --git a/spec/features/click_login_link_spec.rb b/spec/features/click_login_link_spec.rb new file mode 100644 index 0000000..afdae72 --- /dev/null +++ b/spec/features/click_login_link_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + + +# RSpec.feature 'Clicking login link on root page', type: :feature do +# scenario 'User clicks login link and is redirected login page' do +# Rails.logger.info 'User is logged in' +# visit root_path +# click_link 'login' +# logger.info("PPPPP-#{user_calnet_omniauth_authorize_path}") +# expect(page).to have_current_path(user_calnet_omniauth_authorize_path) +# end +# end + + +# describe 'User Util Links' do +# it 'has link' do +# visit root_path +# click_link 'login' +# # logger.info("PPPPP-#{user_calnet_omniauth_authorize_path}") +# # expect(page).to have_current_path(user_calnet_omniauth_authorize_path) +# end +# end + +RSpec.feature 'Homepage', type: :feature, js: true do + include Capybara::DSL + scenario 'User visits the homepage' do + # root_path = 'https://www.google.com' + visit root_path + expect(page).to have_content('UC Berkeley GeoData Repository') + end +end \ No newline at end of file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index fe98012..daf16e5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -8,6 +8,33 @@ abort('The Rails environment is running in production mode!') if Rails.env.production? require 'rspec/rails' +Capybara.register_driver :remote_selenium_headless do |app| + options = Selenium::WebDriver::Chrome::Options.new + options.add_argument('--headless=new') + options.add_argument('--no-sandbox') + # options.add_argument('--disable-dev-shm-usage') +# options.add_argument('--disable-gpu') + options.add_argument('--window-size=2560,1344') + options.add_argument('--disable-smooth-scrolling') + capabilities = [ + options, + Selenium::WebDriver::Remote::Capabilities.new( + 'goog:loggingPrefs' => { + browser: 'ALL', driver: 'ALL' + } + ) + ] + + Capybara::Selenium::Driver.new(app, + browser: :remote, + capabilities:, + url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", + ) + +end + +Capybara.default_driver = Capybara.javascript_driver = :remote_selenium_headless + RSpec.configure do |config| config.use_transactional_fixtures = false From 8a7133f9d4e1686bfbaf6863585ecfa1c7800777 Mon Sep 17 00:00:00 2001 From: zhouyu Date: Mon, 15 Apr 2024 13:59:59 -0700 Subject: [PATCH 2/6] with config from TIND-QA --- Gemfile | 1 + Gemfile.lock | 23 +++++--- spec/features/click_login_link_spec.rb | 4 +- spec/rails_helper.rb | 27 --------- spec/spec_helper.rb | 76 ++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 38 deletions(-) diff --git a/Gemfile b/Gemfile index 09ec8c5..026a25f 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ end group :test do gem 'capybara' + gem 'rspec', '~> 3.13' gem 'rspec_junit_formatter', require: false gem 'selenium-webdriver' gem 'simplecov', '~> 0.21', require: false diff --git a/Gemfile.lock b/Gemfile.lock index fdd604c..edc0a43 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -124,7 +124,7 @@ GEM deep_merge (1.2.2) deprecation (1.1.0) activesupport - devise (4.9.3) + devise (4.9.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 4.1.0) @@ -144,7 +144,7 @@ GEM faraday-net_http_persistent (2.1.0) faraday (~> 2.5) net-http-persistent (~> 4.0) - faraday-retry (2.2.0) + faraday-retry (2.2.1) faraday (~> 2.0) ffi (1.16.3) ffi-compiler (1.3.2) @@ -242,7 +242,7 @@ GEM net-smtp marcel (1.0.4) matrix (0.4.2) - method_source (1.0.0) + method_source (1.1.0) mime-types (3.5.2) mime-types-data (~> 3.2015) mime-types-data (3.2024.0305) @@ -264,11 +264,11 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.1) - nokogiri (1.16.3-aarch64-linux) + nokogiri (1.16.4-aarch64-linux) racc (~> 1.4) - nokogiri (1.16.3-x86_64-darwin) + nokogiri (1.16.4-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.3-x86_64-linux) + nokogiri (1.16.4-x86_64-linux) racc (~> 1.4) oj (3.16.3) bigdecimal (>= 3.0) @@ -340,7 +340,7 @@ GEM rdoc (6.6.3.1) psych (>= 4.0.0) regexp_parser (2.9.0) - reline (0.5.0) + reline (0.5.1) io-console (~> 0.5) request_store (1.6.0) rack (>= 1.4) @@ -355,6 +355,10 @@ GEM rsolr (2.6.0) builder (>= 2.1.2) faraday (>= 0.9, < 3, != 2.0.0) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) rspec-core (3.13.0) rspec-support (~> 3.13.0) rspec-expectations (3.13.0) @@ -374,7 +378,7 @@ GEM rspec-support (3.13.1) rspec_junit_formatter (0.6.0) rspec-core (>= 2, < 4, != 2.12.0) - rubocop (1.63.0) + rubocop (1.63.1) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) @@ -401,7 +405,7 @@ GEM rubocop-capybara (~> 2.17) rubocop-factory_bot (~> 2.22) rubocop-rspec_rails (~> 2.28) - rubocop-rspec_rails (2.28.2) + rubocop-rspec_rails (2.28.3) rubocop (~> 1.40) ruby-progressbar (1.13.0) rubyzip (2.3.2) @@ -510,6 +514,7 @@ DEPENDENCIES rack-timeout (~> 0.6.3) rails (~> 7.0.8) rsolr (>= 1.0, < 3) + rspec (~> 3.13) rspec-rails rspec_junit_formatter rubocop diff --git a/spec/features/click_login_link_spec.rb b/spec/features/click_login_link_spec.rb index afdae72..b112efb 100644 --- a/spec/features/click_login_link_spec.rb +++ b/spec/features/click_login_link_spec.rb @@ -1,5 +1,4 @@ -require 'rails_helper' - +require 'spec_helper' # RSpec.feature 'Clicking login link on root page', type: :feature do # scenario 'User clicks login link and is redirected login page' do @@ -25,6 +24,7 @@ include Capybara::DSL scenario 'User visits the homepage' do # root_path = 'https://www.google.com' + root_path = 'http://localhost:3000/' visit root_path expect(page).to have_content('UC Berkeley GeoData Repository') end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index daf16e5..fe98012 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -8,33 +8,6 @@ abort('The Rails environment is running in production mode!') if Rails.env.production? require 'rspec/rails' -Capybara.register_driver :remote_selenium_headless do |app| - options = Selenium::WebDriver::Chrome::Options.new - options.add_argument('--headless=new') - options.add_argument('--no-sandbox') - # options.add_argument('--disable-dev-shm-usage') -# options.add_argument('--disable-gpu') - options.add_argument('--window-size=2560,1344') - options.add_argument('--disable-smooth-scrolling') - capabilities = [ - options, - Selenium::WebDriver::Remote::Capabilities.new( - 'goog:loggingPrefs' => { - browser: 'ALL', driver: 'ALL' - } - ) - ] - - Capybara::Selenium::Driver.new(app, - browser: :remote, - capabilities:, - url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", - ) - -end - -Capybara.default_driver = Capybara.javascript_driver = :remote_selenium_headless - RSpec.configure do |config| config.use_transactional_fixtures = false diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 16d4c41..57e00ee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,6 +12,82 @@ # require 'rspec_junit_formatter' # SimpleCov.start 'rails' +### testing code ### +# Capybara.register_driver :remote_selenium_headless do |app| +# options = Selenium::WebDriver::Chrome::Options.new +# options.add_argument('--headless=new') +# options.add_argument('--no-sandbox') +# # options.add_argument('--disable-dev-shm-usage') +# # options.add_argument('--disable-gpu') +# options.add_argument('--window-size=2560,1344') +# options.add_argument('--disable-smooth-scrolling') +# capabilities = [ +# options, +# Selenium::WebDriver::Remote::Capabilities.new( +# 'goog:loggingPrefs' => { +# browser: 'ALL', driver: 'ALL' +# } +# ) +# ] + +# Capybara::Selenium::Driver.new(app, +# browser: :remote, +# capabilities:, +# url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", +# ) + +# end + +# Capybara.default_driver = Capybara.javascript_driver = :remote_selenium_headless +### testing code end ### + + +################## from TIND_QA ###################### + +require 'rspec' +require 'capybara/rspec' +require 'selenium-webdriver' + +Capybara.register_driver(:remote) do |app| + chrome_args = %w[ + --window-size=2560,1344 + --disable-smooth-scrolling + ] + + chrome_prefs = { + 'download.prompt_for_download' => false, + 'download.default_directory' => '/tmp', + } + + chrome_options = ::Selenium::WebDriver::Chrome::Options.new(args: chrome_args, prefs: chrome_prefs).tap do |options| + # NOTE: Different Selenium/Chrome versions set download directory differently -- see + # https://github.com/teamcapybara/capybara/blob/3.38.0/spec/selenium_spec_chrome.rb#L15-L20 + if (download_dir = chrome_prefs['download.default_directory']) + options.add_preference(:download, default_directory: download_dir) + end + end + + capabilities = [ + chrome_options, + Selenium::WebDriver::Remote::Capabilities.new( + 'goog:loggingPrefs' => { + browser: 'ALL', driver: 'ALL' + } + ) + ] + + Capybara::Selenium::Driver.new(app, + browser: :remote, + capabilities:, + url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", + ) +end + +Capybara.default_driver = Capybara.javascript_driver = :remote + +############# + + # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate From 516e072a84d65ff0c1ed4a56e5b50088e4d21728 Mon Sep 17 00:00:00 2001 From: zhouyu Date: Mon, 15 Apr 2024 15:04:32 -0700 Subject: [PATCH 3/6] Add Configure from TIND-QA --- docker-compose.yml | 2 +- spec/features/click_login_link_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index ad011fc..671139d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -46,4 +46,4 @@ services: - 4444:4444 - 7900:7900 platform: linux/amd64 - shm_size: 2g + shm_size: 3g diff --git a/spec/features/click_login_link_spec.rb b/spec/features/click_login_link_spec.rb index b112efb..ef78d32 100644 --- a/spec/features/click_login_link_spec.rb +++ b/spec/features/click_login_link_spec.rb @@ -24,6 +24,7 @@ include Capybara::DSL scenario 'User visits the homepage' do # root_path = 'https://www.google.com' + sleep(60) root_path = 'http://localhost:3000/' visit root_path expect(page).to have_content('UC Berkeley GeoData Repository') From 1a4e03c6557abd59c4ccc3ac24d0c65c825ea264 Mon Sep 17 00:00:00 2001 From: zhouyu Date: Mon, 15 Apr 2024 20:57:14 -0700 Subject: [PATCH 4/6] add service aliases --- config/environments/development.rb | 1 + docker-compose.yml | 8 +++ spec/features/click_login_link_spec.rb | 7 ++- spec/rails_helper.rb | 49 +++++++++++++++ spec/spec_helper.rb | 82 +++++++++++++------------- 5 files changed, 103 insertions(+), 44 deletions(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 5793a57..5eeeef9 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -71,4 +71,5 @@ # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + config.hosts << 'app.test' end diff --git a/docker-compose.yml b/docker-compose.yml index 671139d..9f89ee7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,10 @@ services: depends_on: - db - selenium + networks: + default: + aliases: + - app.test ports: - 3000:3000 restart: always @@ -47,3 +51,7 @@ services: - 7900:7900 platform: linux/amd64 shm_size: 3g + networks: + default: + aliases: + - selenium.test diff --git a/spec/features/click_login_link_spec.rb b/spec/features/click_login_link_spec.rb index ef78d32..be26a5c 100644 --- a/spec/features/click_login_link_spec.rb +++ b/spec/features/click_login_link_spec.rb @@ -1,4 +1,5 @@ -require 'spec_helper' +# require 'spec_helper' +require 'rails_helper' # RSpec.feature 'Clicking login link on root page', type: :feature do # scenario 'User clicks login link and is redirected login page' do @@ -24,8 +25,8 @@ include Capybara::DSL scenario 'User visits the homepage' do # root_path = 'https://www.google.com' - sleep(60) - root_path = 'http://localhost:3000/' + # sleep(60) + root_path = 'http://app.test:3000/' visit root_path expect(page).to have_content('UC Berkeley GeoData Repository') end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index fe98012..bb5b8eb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -8,6 +8,55 @@ abort('The Rails environment is running in production mode!') if Rails.env.production? require 'rspec/rails' +################## from TIND_QA ###################### + +# require 'rspec' +require 'capybara/rspec' +require 'selenium-webdriver' + +Capybara.register_driver(:remote) do |app| + chrome_args = %w[ + --window-size=2560,1344 + --disable-smooth-scrolling + ] + + chrome_prefs = { + 'download.prompt_for_download' => false, + 'download.default_directory' => '/tmp', + } + + chrome_options = ::Selenium::WebDriver::Chrome::Options.new(args: chrome_args, prefs: chrome_prefs).tap do |options| + # NOTE: Different Selenium/Chrome versions set download directory differently -- see + # https://github.com/teamcapybara/capybara/blob/3.38.0/spec/selenium_spec_chrome.rb#L15-L20 + if (download_dir = chrome_prefs['download.default_directory']) + options.add_preference(:download, default_directory: download_dir) + end + end + + capabilities = [ + chrome_options, + Selenium::WebDriver::Remote::Capabilities.new( + 'goog:loggingPrefs' => { + browser: 'ALL', driver: 'ALL' + } + ) + ] + + Capybara::Selenium::Driver.new(app, + browser: :remote, + capabilities:, + url: "http://#{ENV['SELENIUM_HOST'] || 'selenium.test'}:4444/", + ) +end + +Capybara.default_driver = Capybara.javascript_driver = :remote +Capybara.app_host = 'http://app.test:3000' +Capybara.server_host = '0.0.0.0' +Capybara.always_include_port = true + +############# + + RSpec.configure do |config| config.use_transactional_fixtures = false diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 57e00ee..2b299eb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -42,50 +42,50 @@ ### testing code end ### -################## from TIND_QA ###################### - -require 'rspec' -require 'capybara/rspec' -require 'selenium-webdriver' - -Capybara.register_driver(:remote) do |app| - chrome_args = %w[ - --window-size=2560,1344 - --disable-smooth-scrolling - ] - - chrome_prefs = { - 'download.prompt_for_download' => false, - 'download.default_directory' => '/tmp', - } - - chrome_options = ::Selenium::WebDriver::Chrome::Options.new(args: chrome_args, prefs: chrome_prefs).tap do |options| - # NOTE: Different Selenium/Chrome versions set download directory differently -- see - # https://github.com/teamcapybara/capybara/blob/3.38.0/spec/selenium_spec_chrome.rb#L15-L20 - if (download_dir = chrome_prefs['download.default_directory']) - options.add_preference(:download, default_directory: download_dir) - end - end +# ################## from TIND_QA ###################### - capabilities = [ - chrome_options, - Selenium::WebDriver::Remote::Capabilities.new( - 'goog:loggingPrefs' => { - browser: 'ALL', driver: 'ALL' - } - ) - ] - - Capybara::Selenium::Driver.new(app, - browser: :remote, - capabilities:, - url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", - ) -end +# require 'rspec' +# require 'capybara/rspec' +# require 'selenium-webdriver' + +# Capybara.register_driver(:remote) do |app| +# chrome_args = %w[ +# --window-size=2560,1344 +# --disable-smooth-scrolling +# ] + +# chrome_prefs = { +# 'download.prompt_for_download' => false, +# 'download.default_directory' => '/tmp', +# } + +# chrome_options = ::Selenium::WebDriver::Chrome::Options.new(args: chrome_args, prefs: chrome_prefs).tap do |options| +# # NOTE: Different Selenium/Chrome versions set download directory differently -- see +# # https://github.com/teamcapybara/capybara/blob/3.38.0/spec/selenium_spec_chrome.rb#L15-L20 +# if (download_dir = chrome_prefs['download.default_directory']) +# options.add_preference(:download, default_directory: download_dir) +# end +# end + +# capabilities = [ +# chrome_options, +# Selenium::WebDriver::Remote::Capabilities.new( +# 'goog:loggingPrefs' => { +# browser: 'ALL', driver: 'ALL' +# } +# ) +# ] + +# Capybara::Selenium::Driver.new(app, +# browser: :remote, +# capabilities:, +# url: "http://#{ENV['SELENIUM_HOST'] || 'selenium'}:4444/", +# ) +# end -Capybara.default_driver = Capybara.javascript_driver = :remote +# Capybara.default_driver = Capybara.javascript_driver = :remote -############# +# ############# # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration From 0cdfa1332c600d69d8b153ba72c8f6c7c67d4736 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Tue, 16 Apr 2024 11:03:05 -0700 Subject: [PATCH 5/6] Updated Chrome settings - Adds `--headless` - Removes shared-memory related settings and disables shm After doing this, I'm able to run the feature tests. Before adding the headless option I still had trouble with frequent Selenium Chrome crashes, but not since. In any case, those are "resolved" by running `docker compose restart selenium` and re-running the tests. --- docker-compose.yml | 1 - spec/rails_helper.rb | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9f89ee7..c899612 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,7 +50,6 @@ services: - 4444:4444 - 7900:7900 platform: linux/amd64 - shm_size: 3g networks: default: aliases: diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index bb5b8eb..9bcc999 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -16,13 +16,15 @@ Capybara.register_driver(:remote) do |app| chrome_args = %w[ - --window-size=2560,1344 + --disable-dev-shm-usage --disable-smooth-scrolling + --headless + --window-size=2560,1344 ] chrome_prefs = { 'download.prompt_for_download' => false, - 'download.default_directory' => '/tmp', + 'download.default_directory' => '/tmp' } chrome_options = ::Selenium::WebDriver::Chrome::Options.new(args: chrome_args, prefs: chrome_prefs).tap do |options| From 7e179ed09d200b59d76165f2547937f76570460d Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Tue, 16 Apr 2024 11:23:01 -0700 Subject: [PATCH 6/6] Restores shm settings -- headless is sufficient --- docker-compose.yml | 1 + spec/rails_helper.rb | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index c899612..fbcb37a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -54,3 +54,4 @@ services: default: aliases: - selenium.test + shm_size: 2g diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 9bcc999..0f45342 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -16,7 +16,6 @@ Capybara.register_driver(:remote) do |app| chrome_args = %w[ - --disable-dev-shm-usage --disable-smooth-scrolling --headless --window-size=2560,1344