Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Metrics/AbcSize:
Max: 66

Metrics/ClassLength:
Max: 300
Max: 310

Metrics/CyclomaticComplexity:
Max: 14
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 30 additions & 3 deletions lib/capybara/cuprite/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
Loading