Skip to content

Commit

Permalink
Merge pull request #14426 from opf/fix/use-headless-new-for-chrome-in…
Browse files Browse the repository at this point in the history
…-capybara

Use --headless=new for Chrome in Capybara
  • Loading branch information
cbliard authored Dec 18, 2023
2 parents 2297574 + e086485 commit f440c9e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ group :test do
gem 'capybara', '~> 3.39.0'
gem 'capybara-screenshot', '~> 1.0.17'
gem 'cuprite', '~> 0.15.0'
gem 'selenium-webdriver', '~> 4.15.0'
gem 'selenium-devtools'
gem 'selenium-webdriver', '~> 4.15'
gem 'capybara_accessible_selectors', git: 'https://github.com/citizensadvice/capybara_accessible_selectors', branch: 'main'

gem 'fuubar', '~> 2.5.0'
Expand Down
7 changes: 5 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,9 @@ GEM
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
secure_headers (6.5.0)
selenium-webdriver (4.15.0)
selenium-devtools (0.120.0)
selenium-webdriver (~> 4.2)
selenium-webdriver (4.16.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
Expand Down Expand Up @@ -1210,7 +1212,8 @@ DEPENDENCIES
rubytree (~> 2.0.0)
sanitize (~> 6.1.0)
secure_headers (~> 6.5.0)
selenium-webdriver (~> 4.15.0)
selenium-devtools
selenium-webdriver (~> 4.15)
semantic (~> 1.6.1)
shoulda-context (~> 2.0)
shoulda-matchers (~> 5.0)
Expand Down
4 changes: 2 additions & 2 deletions docker/ci/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ setup_tests() {
run_background backend_stuff
run_background frontend_stuff
# pre-cache browsers and their drivers binaries
run_background $(bundle show selenium)/bin/linux/selenium-manager --browser chrome --debug
run_background $(bundle show selenium)/bin/linux/selenium-manager --browser firefox --debug
run_background $(bundle show selenium-webdriver)/bin/linux/selenium-manager --browser chrome --debug
run_background $(bundle show selenium-webdriver)/bin/linux/selenium-manager --browser firefox --debug
wait_for_background
}

Expand Down
6 changes: 6 additions & 0 deletions frontend/src/app/core/setup/globals/global-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export function initializeGlobalListeners():void {
return;
}

// Avoid opening new tab when clicking links while editing in ckeditor
if (linkElement.classList.contains('ck-link_selected')) {
evt.preventDefault();
return;
}

const callbacks = [
openExternalLinksInNewTab,
performAnchorHijacking,
Expand Down
10 changes: 10 additions & 0 deletions modules/bim/spec/features/bcf/api_authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,18 @@ def oauth_path(client_id)

logout

# A basic auth alert is displayed asking to enter name and password Register
# some basic auth credentials
# - A non-matching url is used so that capybara will issue a CancelAuth
# instead of trying to authenticate
# - The register method is not recognized by selenium-webdriver with Chrome
# 120 with old headless
if page.driver.browser.respond_to?(:register)
page.driver.browser.register(username: 'foo', password: 'bar', uri: /does_not_match/)
end
# While not being logged in and without a token, the api cannot be accessed
visit("/api/bcf/2.1/projects/#{project.id}")
# Cancel button of basic auth should have been chosen now
expect(page)
.to have_content(JSON.dump({ message: "You need to be authenticated to access this resource." }))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

require_relative '../../support/pages/my/page'

RSpec.describe 'My spent time widget with a negative time zone', :js,
RSpec.describe 'My spent time widget with a negative time zone', :chrome_headless_new, :js,
with_settings: { start_of_week: 1 } do
let(:beginning_of_week) { monday }
let(:end_of_week) { sunday }
Expand Down
15 changes: 14 additions & 1 deletion spec/support/browsers/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ def register_chrome(language, name: :"chrome_#{language}", override_time_zone: n
end
else
options.add_argument('--window-size=1920,1080')
options.add_argument('--headless')
# Our tests are not that stable with the new headless mode, but since
# Chrome 120 the old headless mode has browser name
# "chrome-headless-shell". This name is not recognized by the current
# selenium-webdriver and so some extensions like `execute_cdp` are
# missing. This wil be fixed in next selenium-webdriver version. See
# https://github.com/SeleniumHQ/selenium/pull/13271 for more information.
#
# In the meantime, the :chrome_headless_new metadata allows activating the
# new headless for tests that need it.
if RSpec.current_example.metadata[:chrome_headless_new]
options.add_argument('--headless=new')
else
options.add_argument('--headless=old')
end
end

options.add_argument('--no-sandbox')
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared/drag_and_drop_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def drag_n_drop_element(from:, to:, offset_x: nil, offset_y: nil)
end

def drag_by_pixel(element:, by_x:, by_y:)
scroll_to_element(element)
scroll_to_element(element, block: :center)

page
.driver
Expand Down
9 changes: 7 additions & 2 deletions spec/support/shared/scroll_into_view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
#++

# Scrolls a native element into view using JS
def scroll_to_element(element)
# @param element [Capybara::Node::Element] the element to scroll into view
# @param block [Symbol] (optional) Defines vertical alignment.
# One of `:start`, `:center`, `:end`, or `:nearest`. Defaults to `:start`.
# @param inline [Symbol] (optional) Defines horizontal alignment.
# One of `:start`, `:center`, `:end`, or `:nearest`. Defaults to `:nearest`..
def scroll_to_element(element, block: :start, inline: :nearest)
script = <<-JS
arguments[0].scrollIntoView(true);
arguments[0].scrollIntoView({block: "#{block}", inline: "#{inline}"});
JS
if using_cuprite?
page.driver.execute_script(script, element.native)
Expand Down

0 comments on commit f440c9e

Please sign in to comment.