From 257a7f3b3ff6058a879975352b9993889961c951 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Tue, 24 Nov 2026 21:54:57 +0300 Subject: [PATCH 1/3] fix(node): fill text fields with real key presses so minlength validates Chrome checks minlength only after a user edit, and a value set from JS never counts as one. Text inputs and textareas are now typed with Ferrum's node.type, and values over 30 characters use Capybara's Selenium rapid mode. Newlines are sent as \r because Ferrum drops \n. has_field with valid, CRLF in a textarea and \n submitting a single-input form pass and are no longer skipped. --- CHANGELOG.md | 2 ++ lib/capybara/cuprite/node.rb | 29 ++++++++++++++++++++++++++--- spec/spec_helper.rb | 7 ------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f569b0c..8c934c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - `:url_blocklist` and `:url_allowlist` driver options as aliases for `:url_blacklist` and `:url_whitelist` [#330] ### Changed +- Text inputs and textareas are filled with real key presses like Selenium, values over 30 characters type the first 4 + and last 3 and set the rest from JS. `minlength` validates, `\n` submits a single-input form, and `rapid:` is supported - `CUPRITE_DEBUG` is renamed to `CUPRITE_LOG`, which takes `FERRUM_LOG` values and passes them on, `CUPRITE_DEBUG` is deprecated and acts as `CUPRITE_LOG=*`. `FERRUM_DEBUG` is deprecated in Ferrum 0.21 - Clicks, hovers and drags find their point with Ferrum's `node.pinpoint` instead of `find_position`, deprecated in diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index 4b6ad99..deff76f 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -13,6 +13,12 @@ class Node < Capybara::Driver::Node DRAG_MODIFIER_ALIASES = { control: :ctrl, command: :meta, cmd: :meta }.freeze DRAG_HTML5_JS = File.read(File.expand_path("javascripts/drag.js", __dir__)) TRIGGER_CLICK_WAIT = ENV.fetch("CUPRITE_TRIGGER_CLICK_WAIT", 0.1).to_f + TYPED_INPUTS = %w[text password email search tel url].freeze + RAPID_SET_LENGTH = 30 + RAPID_APPEND_TEXT = <<~JS + let appended = this.value + value; + this.value = this.maxLength >= 0 ? appended.slice(0, this.maxLength) : appended; + JS def initialize(driver, node, initial_cache = {}) super(driver, self, initial_cache) @@ -62,10 +68,10 @@ def value end def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize - warn "Options passed to Node#set but Cuprite doesn't currently support any - ignoring" unless options.empty? - if tag_name == "input" case self[:type] + when *TYPED_INPUTS + set_text(value.to_s, **options) when "radio" click when "checkbox" @@ -96,7 +102,7 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met command(:set, value.to_s) end elsif tag_name == "textarea" - command(:set, value.to_s) + set_text(value.to_s, **options) elsif self[:isContentEditable] command(:deleteText) click.type(value.to_s) @@ -240,6 +246,23 @@ def inspect private + def set_text(value, rapid: nil, **) + return command(:set, value) if value.empty? + return if command { node.evaluate("this.readOnly || this.disabled") } + + value = value.gsub(/\r\n?|\n/, "\r") + node.focus + node.evaluate("this.select()") + if rapid == true || (value.length > RAPID_SET_LENGTH && rapid != false) + node.type(value[0..3]) + node.evaluate(RAPID_APPEND_TEXT, value: value[4...-3]) + node.type(value[-3..]) + else + node.type(value) + end + node.evaluate("this.blur(); this.focus()") + end + def find(method, selector, **hints) command { Finder.find(driver.browser, method, selector, node, **hints) }.map do |native, cache| self.class.new(driver, native, cache) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index daaabca..066af5f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -47,10 +47,6 @@ module TestSessions RSpec.configure do |config| config.define_derived_metadata do |metadata| - regexes = <<~REGEXP.split("\n").map { |s| Regexp.quote(s.strip) }.join("|") - #has_field with valid should be false if field is invalid - REGEXP - hash_inspect = <<~REGEXP.split("\n").map { |s| Regexp.quote(s.strip) }.join("|") #assert_matches_style should raise error if the elements style doesn't contain the given properties #has_css? :style option should support Hash @@ -59,15 +55,12 @@ module TestSessions # These are tests that are currently skipped intentionally, as they don't even work in capybara. intentional_skip = <<~REGEXP.split("\n").map { |s| Regexp.quote(s.strip) }.join("|") Capybara::Session Cuprite #reset_session! closes extra windows - #fill_in should handle carriage returns with line feeds in a textarea correctly - node #set should submit single text input forms if ended with REGEXP browser_behavior = <<~REGEXP.split("\n").map { |s| Regexp.quote(s.strip) }.join("|") #drag_to HTML5 should preserve clientX/Y from last dragover event REGEXP - metadata[:skip] = true if metadata[:full_description].match(/#{regexes}/) metadata[:skip] = "Intentionally skipped" if metadata[:full_description].match(/#{intentional_skip}/) if metadata[:full_description].match(/#{browser_behavior}/) metadata[:skip] = "Expects behavior browsers don't have, e.g. dragleave together with drop" From 70e34ba0cda35fc595d5893031cd1b3d2ea1e25a Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Tue, 24 Nov 2026 22:03:59 +0300 Subject: [PATCH 2/3] fix(node): skip refocusing a field whose typing left the page Typing a trailing \r presses Enter, which can submit the form and navigate away before the final blur/focus runs. That raised ObsoleteNode, and Capybara retried set on the stale node until it timed out. Also inline the rapid-append JS and raise Metrics/ClassLength to 310. --- .rubocop.yml | 2 +- lib/capybara/cuprite/node.rb | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b7b9364..e46af7b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,7 +44,7 @@ Metrics/AbcSize: Max: 66 Metrics/ClassLength: - Max: 300 + Max: 310 Metrics/CyclomaticComplexity: Max: 14 diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index deff76f..8dd3842 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -15,10 +15,7 @@ class Node < Capybara::Driver::Node TRIGGER_CLICK_WAIT = ENV.fetch("CUPRITE_TRIGGER_CLICK_WAIT", 0.1).to_f TYPED_INPUTS = %w[text password email search tel url].freeze RAPID_SET_LENGTH = 30 - RAPID_APPEND_TEXT = <<~JS - let appended = this.value + value; - this.value = this.maxLength >= 0 ? appended.slice(0, this.maxLength) : appended; - JS + RAPID_APPEND_TEXT = "this.value = (this.value + value).slice(0, this.maxLength >= 0 ? this.maxLength : undefined)" def initialize(driver, node, initial_cache = {}) super(driver, self, initial_cache) @@ -260,7 +257,13 @@ def set_text(value, rapid: nil, **) else node.type(value) end + refocus + end + + def refocus node.evaluate("this.blur(); this.focus()") + rescue Ferrum::NodeNotFoundError + nil end def find(method, selector, **hints) From 0c3f6cf68f5a2174826ff25d6b2dd439386b0432 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Tue, 24 Nov 2026 22:40:23 +0300 Subject: [PATCH 3/3] fix(node): set short values in full with rapid: true Rapid mode types the first 4 and last 3 characters and sets the middle from JS, so a value of 7 characters or fewer overlapped: "Hello" became "Hellllo". It now types such values in full. Adds specs for a long value, a short one with rapid: true and maxlength with rapid: true. --- lib/capybara/cuprite/node.rb | 5 +++-- spec/node_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index 8dd3842..9b2d8cd 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -250,9 +250,10 @@ def set_text(value, rapid: nil, **) value = value.gsub(/\r\n?|\n/, "\r") node.focus node.evaluate("this.select()") - if rapid == true || (value.length > RAPID_SET_LENGTH && rapid != false) + middle = value[4...-3].to_s + if !middle.empty? && (rapid == true || (value.length > RAPID_SET_LENGTH && rapid != false)) node.type(value[0..3]) - node.evaluate(RAPID_APPEND_TEXT, value: value[4...-3]) + node.evaluate(RAPID_APPEND_TEXT, value: middle) node.type(value[-3..]) else node.type(value) diff --git a/spec/node_spec.rb b/spec/node_spec.rb index ae0e90a..dc6a0ee 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -459,6 +459,24 @@ expect(element.value).to eq("100") end + it "sets a value over 30 characters in full" do + element = @session.find(:css, "#change_me") + element.set(("a".."z").to_a.join * 2) + expect(element.value).to eq(("a".."z").to_a.join * 2) + end + + it "sets a short value in full with rapid: true" do + element = @session.find(:css, "#change_me") + element.set("Hello", rapid: true) + expect(element.value).to eq("Hello") + end + + it "cuts a value set with rapid: true at maxlength" do + element = @session.find(:css, "#change_me_maxlength") + element.set(("a".."z").to_a.join * 2, rapid: true) + expect(element.value).to eq((("a".."z").to_a.join * 2)[0, 30]) + end + it "accepts negatives in a number field" do element = @session.find(:css, "#change_me_number") element.set(-100)