Skip to content

fix: ignore empty or nil keys in Node#send_keys#313

Open
myabc wants to merge 1 commit into
rubycdp:mainfrom
myabc:fix/send-keys-empty-nil
Open

fix: ignore empty or nil keys in Node#send_keys#313
myabc wants to merge 1 commit into
rubycdp:mainfrom
myabc:fix/send-keys-empty-nil

Conversation

@myabc

@myabc myabc commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

Capybara::Cuprite::Node#send_keys raises on empty or nil keys, diverging from the Selenium and rack_test drivers which treat them as a no-op:

input.send_keys("")   # => ArgumentError: empty keys passed
input.send_keys(nil)  # => ArgumentError: unexpected argument

send_keys forwards straight to Ferrum::Keyboard#type, which rejects empty/nil input. This breaks otherwise driver-agnostic Capybara code such as send_keys(value) where value may be "" — common when conditionally clearing or filling a field. Callers currently have to add their own send_keys(x) if x guards for Cuprite only.

Real world example: https://github.com/citizensadvice/capybara_accessible_selectors/pull/157/changes#diff-8a7441fcb90a95d9647b64ef89630240641dfb8cd8d88ea3d4768d4053e8af5aR75

Fix

Reject nil and "" entries before delegating to Ferrum, and no-op when nothing remains. Meaningful keys (including symbols like :enter and modifier chords like [:control, "a"]) pass through unchanged, and empty strings mixed with real keys are dropped rather than crashing.

Testing

New spec in the "has ability to send keys" context asserts send_keys("") and send_keys(nil) do not raise and leave the field unchanged. Full send_keys context green (25 examples). Rubocop clean.

Copilot AI review requested due to automatic review settings July 9, 2026 12:52
Ferrum raises ArgumentError on empty or nil keys, so
`send_keys("")` or `send_keys(nil)` crashes under Cuprite while the
Selenium and rack_test drivers treat them as a no-op. Reject empty and
nil entries before delegating to Ferrum so otherwise portable Capybara
code stays driver-agnostic.
@myabc myabc force-pushed the fix/send-keys-empty-nil branch from db26711 to d0f5829 Compare July 9, 2026 12:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aligns Capybara::Cuprite::Node#send_keys behavior with Capybara’s Selenium and rack_test drivers by treating nil and empty-string key inputs as a no-op instead of raising, improving driver-agnostic test/code portability.

Changes:

  • Filter out nil and "" entries in Node#send_keys, and return early when no keys remain.
  • Add a feature spec asserting send_keys("") and send_keys(nil) do not raise and do not change the field value.
  • Document the behavior change in the changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
lib/capybara/cuprite/node.rb Filters out nil/empty keys before delegating, avoiding Ferrum exceptions and no-op’ing when nothing remains.
spec/features/driver_spec.rb Adds coverage ensuring empty/nil send_keys inputs do not raise and do not mutate the field.
CHANGELOG.md Notes the fixed behavior for Node#send_keys in Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@myabc myabc changed the title fix: ignore empty or nil keys in Node#send_keys fix: ignore empty or nil keys in Node#send_keys Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants