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/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..9b2d8cd 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -13,6 +13,9 @@ 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 = "this.value = (this.value + value).slice(0, this.maxLength >= 0 ? this.maxLength : undefined)" def initialize(driver, node, initial_cache = {}) super(driver, self, initial_cache) @@ -62,10 +65,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 +99,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 +243,30 @@ 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()") + 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: middle) + node.type(value[-3..]) + 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) command { Finder.find(driver.browser, method, selector, node, **hints) }.map do |native, cache| self.class.new(driver, native, cache) 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) 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"