mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
more fill_options for selenium node#set
This commit is contained in:
parent
dfd985fdf2
commit
339bebf55e
2 changed files with 36 additions and 2 deletions
|
@ -23,7 +23,18 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(value, fill_options={})
|
##
|
||||||
|
#
|
||||||
|
# Set the value of the form element to the given value.
|
||||||
|
#
|
||||||
|
# @param [String] value The new value
|
||||||
|
# @param [Hash{}] options Driver specific options for how to set the value
|
||||||
|
# @option options [Symbol,Array] :clear (nil) The method used to clear the previous value <br/>
|
||||||
|
# nil => clear via javascript <br/>
|
||||||
|
# :none => append the new value to the existing value <br/>
|
||||||
|
# :backspace => send backspace keystrokes to clear the field <br/>
|
||||||
|
# Array => an array of keys to send before the value being set, e.g. [[:command, 'a'], :backspace]
|
||||||
|
def set(value, options={})
|
||||||
tag_name = self.tag_name
|
tag_name = self.tag_name
|
||||||
type = self[:type]
|
type = self[:type]
|
||||||
if (Array === value) && !self[:multiple]
|
if (Array === value) && !self[:multiple]
|
||||||
|
@ -42,10 +53,14 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
|
||||||
elsif value.to_s.empty?
|
elsif value.to_s.empty?
|
||||||
native.clear
|
native.clear
|
||||||
else
|
else
|
||||||
if fill_options[:clear] == :backspace
|
if options[:clear] == :backspace
|
||||||
# Clear field by sending the correct number of backspace keys.
|
# Clear field by sending the correct number of backspace keys.
|
||||||
backspaces = [:backspace] * self.value.to_s.length
|
backspaces = [:backspace] * self.value.to_s.length
|
||||||
native.send_keys(*(backspaces + [value.to_s]))
|
native.send_keys(*(backspaces + [value.to_s]))
|
||||||
|
elsif options[:clear] == :none
|
||||||
|
native.send_keys(value.to_s)
|
||||||
|
elsif options[:clear].is_a? Array
|
||||||
|
native.send_keys(*options[:clear], value.to_s)
|
||||||
else
|
else
|
||||||
# Clear field by JavaScript assignment of the value property.
|
# Clear field by JavaScript assignment of the value property.
|
||||||
# Script can change a readonly element which user input cannot, so
|
# Script can change a readonly element which user input cannot, so
|
||||||
|
|
|
@ -100,6 +100,25 @@ RSpec.describe Capybara::Session do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "#fill_in with { clear: :none } fill_options" do
|
||||||
|
it 'should append to content in a field' do
|
||||||
|
@session.visit('/form')
|
||||||
|
@session.fill_in('form_first_name', :with => 'Harry',
|
||||||
|
fill_options: { clear: :none} )
|
||||||
|
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnHarry')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "#fill_in with { clear: Array } fill_options" do
|
||||||
|
it 'should pass the array through to the element', tw: true do
|
||||||
|
#this is mainly for use with [[:ctrl, 'a'], :backspace] - however since that is platform dependant I'm testing with something less useful
|
||||||
|
@session.visit('/form')
|
||||||
|
@session.fill_in('form_first_name', :with => 'Harry',
|
||||||
|
fill_options: { clear: [[:shift, 'abc'], :backspace] } )
|
||||||
|
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnABHarry')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "#path" do
|
describe "#path" do
|
||||||
it "returns xpath" do
|
it "returns xpath" do
|
||||||
# this is here because it is testing for an XPath that is specific to the algorithm used in the selenium driver
|
# this is here because it is testing for an XPath that is specific to the algorithm used in the selenium driver
|
||||||
|
|
Loading…
Reference in a new issue