2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
Capybara::SpecHelper.spec '#fill_in' do
|
2012-07-21 16:44:10 -04:00
|
|
|
before do
|
|
|
|
@session.visit('/form')
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a text field by id' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form_first_name', with: 'Harry')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['first_name']).to eq('Harry')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a text field by name' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form[last_name]', with: 'Green')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['last_name']).to eq('Green')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-27 03:11:22 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a text field by label without for' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('First Name', with: 'Harry')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['first_name']).to eq('Harry')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-27 03:11:22 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a url field by label without for' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Html5 Url', with: 'http://www.avenueq.com')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('html5_submit')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['html5_url']).to eq('http://www.avenueq.com')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-06-29 15:58:57 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a textarea by id' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form_description', with: 'Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['description']).to eq('Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a textarea by label' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Description', with: 'Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['description']).to eq('Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a textarea by name' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form[description]', with: 'Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['description']).to eq('Texty text')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-12-14 19:06:50 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a password field by id' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form_password', with: 'supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['password']).to eq('supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'Date/Time' do
|
|
|
|
it 'should fill in a date input' do
|
2018-03-09 17:23:52 -05:00
|
|
|
date = Date.today
|
|
|
|
@session.fill_in('form_date', with: date)
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(Date.parse(extract_results(@session)['date'])).to eq date
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a time input' do
|
2018-03-09 17:23:52 -05:00
|
|
|
time = Time.new(2018, 3, 9, 15, 26)
|
|
|
|
@session.fill_in('form_time', with: time)
|
|
|
|
@session.click_button('awesome')
|
|
|
|
results = extract_results(@session)['time']
|
|
|
|
expect(Time.parse(results).strftime('%r')).to eq time.strftime('%r')
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a datetime input' do
|
2018-03-09 17:23:52 -05:00
|
|
|
dt = Time.new(2018, 3, 13, 9, 53)
|
|
|
|
@session.fill_in('form_datetime', with: dt)
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(Time.parse(extract_results(@session)['datetime'])).to eq dt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should handle HTML in a textarea' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form_description', with: 'is <strong>very</strong> secret!')
|
2013-03-26 11:19:37 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['description']).to eq('is <strong>very</strong> secret!')
|
2013-03-26 11:19:37 -04:00
|
|
|
end
|
2012-12-14 19:06:50 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should handle newlines in a textarea' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form_description', with: "\nSome text\n")
|
2013-04-25 21:09:34 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['description']).to eq("\r\nSome text\r\n")
|
2013-04-25 21:09:34 -04:00
|
|
|
end
|
2013-03-26 11:19:37 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a field with a custom type' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Schmooo', with: 'Schmooo is the game')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['schmooo']).to eq('Schmooo is the game')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-03-12 14:11:21 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a field without a type' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Phone', with: '+1 555 7022')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['phone']).to eq('+1 555 7022')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-05-20 07:43:16 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a text field respecting its maxlength attribute' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Zipcode', with: '52071350')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['zipcode']).to eq('52071')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2011-02-20 14:23:11 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a password field by name' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form[password]', with: 'supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['password']).to eq('supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-03-12 14:11:21 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a password field by label' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Password', with: 'supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['password']).to eq('supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a password field by name' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('form[password]', with: 'supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['password']).to eq('supasikrit')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a field based on current value' do
|
2016-12-20 12:52:51 -05:00
|
|
|
@session.fill_in(currently_with: 'John', with: 'Thomas')
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(extract_results(@session)['first_name']).to eq('Thomas')
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should fill in a field based on type' do
|
2017-04-24 21:50:14 -04:00
|
|
|
@session.fill_in(type: 'schmooo', with: 'Schmooo for all')
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(extract_results(@session)['schmooo']).to eq('Schmooo for all')
|
|
|
|
end
|
|
|
|
|
2018-08-31 13:01:40 -04:00
|
|
|
it 'should be able to fill in element called on when no locator passed' do
|
|
|
|
field = @session.find(:fillable_field, 'form[password]')
|
|
|
|
field.fill_in(with: 'supasikrit')
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(extract_results(@session)['password']).to eq('supasikrit')
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should throw an exception if a hash containing 'with' is not provided" do
|
2018-02-28 19:11:41 -05:00
|
|
|
expect { @session.fill_in 'Name' }.to raise_error(ArgumentError, /with/)
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-01-31 10:20:35 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should wait for asynchronous load', requires: [:js] do
|
2018-07-05 21:08:59 -04:00
|
|
|
Capybara.default_max_wait_time = 2
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Click me')
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('new_field', with: 'Testing...')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'casts to string' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@session.fill_in(:form_first_name, with: :Harry)
|
2012-09-06 03:33:43 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['first_name']).to eq('Harry')
|
2012-09-06 03:33:43 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'casts to string if field has maxlength' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@session.fill_in(:form_zipcode, with: 1234567)
|
2013-03-26 16:29:25 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['zipcode']).to eq('12345')
|
2013-03-26 16:29:25 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'fills in a field if default_set_options is nil' do
|
2018-05-18 13:29:53 -04:00
|
|
|
Capybara.default_set_options = nil
|
|
|
|
@session.fill_in(:form_first_name, with: 'Thomas')
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(extract_results(@session)['first_name']).to eq('Thomas')
|
|
|
|
end
|
|
|
|
|
2016-10-04 14:10:29 -04:00
|
|
|
context 'on a pre-populated textfield with a reformatting onchange', requires: [:js] do
|
2012-07-21 16:44:10 -04:00
|
|
|
it 'should only trigger onchange once' do
|
|
|
|
@session.visit('/with_js')
|
2018-05-21 19:34:00 -04:00
|
|
|
# Click somewhere on the page to ensure focus is acquired. Without this FF won't generate change events for some reason???
|
|
|
|
@session.find(:css, 'body').click
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('with_change_event', with: 'some value')
|
2013-04-09 09:04:58 -04:00
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
2016-10-04 14:10:29 -04:00
|
|
|
expect(@session.find(:css, '.change_event_triggered', match: :one)).to have_text 'some value'
|
2010-04-21 17:55:33 -04:00
|
|
|
end
|
2013-11-10 08:10:56 -05:00
|
|
|
|
|
|
|
it 'should trigger change when clearing field' do
|
|
|
|
@session.visit('/with_js')
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('with_change_event', with: '')
|
2013-11-10 08:10:56 -05:00
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
2016-10-04 14:10:29 -04:00
|
|
|
expect(@session).to have_selector(:css, '.change_event_triggered', match: :one)
|
2013-11-10 08:10:56 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-01-31 10:20:35 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with ignore_hidden_fields' do
|
2012-07-21 16:44:10 -04:00
|
|
|
before { Capybara.ignore_hidden_elements = true }
|
2018-06-19 16:57:42 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
after { Capybara.ignore_hidden_elements = false }
|
2018-06-19 16:57:42 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not find a hidden field' do
|
|
|
|
msg = 'Unable to find visible field "Super Secret"'
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Super Secret', with: '777')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2010-01-30 13:48:25 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-01-30 13:48:25 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with a locator that doesn't exist" do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should raise an error' do
|
|
|
|
msg = 'Unable to find field "does not exist"'
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('does not exist', with: 'Blah blah')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 11:18:03 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'on a disabled field' do
|
|
|
|
it 'should raise an error' do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.fill_in('Disabled Text Field', with: 'Blah blah')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
2012-09-09 11:18:03 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-24 10:41:20 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with :exact option' do
|
|
|
|
it 'should accept partial matches when false' do
|
|
|
|
@session.fill_in('Explanation', with: 'Dude', exact: false)
|
|
|
|
@session.click_button('awesome')
|
|
|
|
expect(extract_results(@session)['name_explanation']).to eq('Dude')
|
2013-02-24 10:41:20 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not accept partial matches when true' do
|
2013-02-24 10:41:20 -05:00
|
|
|
expect do
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.fill_in('Explanation', with: 'Dude', exact: true)
|
2013-02-24 10:41:20 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
2016-10-10 17:43:40 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should return the element filled in' do
|
2016-10-10 17:43:40 -04:00
|
|
|
el = @session.find(:fillable_field, 'form_first_name')
|
|
|
|
expect(@session.fill_in('form_first_name', with: 'Harry')).to eq el
|
|
|
|
end
|
2018-12-14 14:53:09 -05:00
|
|
|
|
|
|
|
it 'should warn if passed what looks like a CSS id selector' do
|
|
|
|
expect do
|
|
|
|
@session.fill_in('#form_first_name', with: 'Harry')
|
|
|
|
end.to raise_error(/you may be passing a CSS selector or XPath expression rather than a locator/)
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|