From c9197cf9d853682932a611a06f00db7f8d9e6df5 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Sun, 11 Dec 2022 23:12:33 +0100 Subject: [PATCH] DEV: adds native support for mobile in system tests (#19400) Will make your test run in an emulated iPhone 12 Pro view. It means you can now use `click(delay: 0.5)` to emulate some long press or that `mobile_view=1` will be set automatically. Usage: ``` it "works", mobile: true do visit("/") end ``` Note: `window-size=390,950` is different than native iPhone 12 Pro size, but due to minimum browser size and the automated browser alert at the top of the view, this was the best size I could find. --- spec/rails_helper.rb | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 14e8ff951d3..21c575c6f99 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -280,6 +280,33 @@ RSpec.configure do |config| ) end + mobile_chrome_browser_options = + Selenium::WebDriver::Chrome::Options + .new(logging_prefs: { "browser" => "ALL", "driver" => "ALL" }) + .tap do |options| + options.add_argument("--window-size=390,950") + options.add_argument("--no-sandbox") + options.add_argument("--disable-dev-shm-usage") + options.add_emulation(device_name: "iPhone 12 Pro") + end + + Capybara.register_driver :selenium_mobile_chrome do |app| + Capybara::Selenium::Driver.new( + app, + browser: :chrome, + capabilities: mobile_chrome_browser_options, + ) + end + + Capybara.register_driver :selenium_mobile_chrome_headless do |app| + mobile_chrome_browser_options.add_argument("--headless") + Capybara::Selenium::Driver.new( + app, + browser: :chrome, + capabilities: mobile_chrome_browser_options, + ) + end + if ENV['ELEVATED_UPLOADS_ID'] DB.exec "SELECT setval('uploads_id_seq', 10000)" else @@ -342,9 +369,11 @@ RSpec.configure do |config| last_driven_by = nil config.before(:each, type: :system) do |example| if example.metadata[:js] - driver = "selenium_chrome" - driver += "_headless" unless ENV["SELENIUM_HEADLESS"] == "0" - driven_by driver.to_sym + driver = [:selenium] + driver << :mobile if example.metadata[:mobile] + driver << :chrome + driver << :headless unless ENV["SELENIUM_HEADLESS"] == "0" + driven_by driver.join("_").to_sym end setup_system_test end