2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec "#select" do
|
|
|
|
before do
|
|
|
|
@session.visit('/form')
|
|
|
|
end
|
2010-10-03 13:15:40 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return value of the first option" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Title').value).to eq('Mrs')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-02-18 17:41:13 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return value of the selected option" do
|
|
|
|
@session.select("Miss", :from => 'Title')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Title').value).to eq('Miss')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2013-02-16 04:20:50 -05:00
|
|
|
it "should allow selecting options where there are inexact matches" do
|
|
|
|
@session.select("Mr", :from => 'Title')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Title').value).to eq('Mr')
|
2013-02-16 04:20:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow selecting options where they are the only inexact match" do
|
|
|
|
@session.select("Mis", :from => 'Title')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Title').value).to eq('Miss')
|
2013-02-16 04:20:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow selecting options where they are the only inexact match if `Capybara.exact_options = true`" do
|
|
|
|
Capybara.exact_options = true
|
|
|
|
expect do
|
|
|
|
@session.select("Mis", :from => 'Title')
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow selecting an option if the match is ambiguous" do
|
|
|
|
expect do
|
|
|
|
@session.select("M", :from => 'Title')
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return the value attribute rather than content if present" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Locale').value).to eq('en')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-04 16:17:07 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should select an option from a select box by id" do
|
|
|
|
@session.select("Finish", :from => 'form_locale')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['locale']).to eq('fi')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should select an option from a select box by label" do
|
|
|
|
@session.select("Finish", :from => 'Locale')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['locale']).to eq('fi')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should select an option without giving a select box" do
|
|
|
|
@session.select("Swedish")
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['locale']).to eq('sv')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should escape quotes" do
|
|
|
|
@session.select("John's made-up language", :from => 'Locale')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['locale']).to eq('jo')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should obey from" do
|
|
|
|
@session.select("Miss", :from => "Other title")
|
|
|
|
@session.click_button('awesome')
|
|
|
|
results = extract_results(@session)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(results['other_title']).to eq("Miss")
|
|
|
|
expect(results['title']).not_to eq("Miss")
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "show match labels with preceding or trailing whitespace" do
|
|
|
|
@session.select("Lojban", :from => 'Locale')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['locale']).to eq('jbo')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2012-09-06 03:33:43 -04:00
|
|
|
it "casts to string" do
|
|
|
|
@session.select(:"Miss", :from => :'Title')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Title').value).to eq('Miss')
|
2012-09-06 03:33:43 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with a locator that doesn't exist" do
|
|
|
|
it "should raise an error" do
|
|
|
|
msg = "Unable to find select box \"does not exist\""
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.select('foo', :from => 'does not exist')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with an option that doesn't exist" do
|
|
|
|
it "should raise an error" do
|
|
|
|
msg = "Unable to find option \"Does not Exist\""
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.select('Does not Exist', :from => 'form_locale')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2010-02-03 08:42:28 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-02-03 08:42:28 -05:00
|
|
|
|
2012-09-09 11:18:03 -04:00
|
|
|
context "on a disabled select" do
|
|
|
|
it "should raise an error" do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-09-09 11:18:03 -04:00
|
|
|
@session.select('Should not see me', :from => 'Disabled Select')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
2012-09-09 11:18:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with multiple select" do
|
|
|
|
it "should return an empty value" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Language').value).to eq([])
|
2010-04-26 11:23:34 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return value of the selected options" do
|
|
|
|
@session.select("Ruby", :from => 'Language')
|
|
|
|
@session.select("Javascript", :from => 'Language')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Language').value).to include('Ruby', 'Javascript')
|
2010-07-01 22:45:02 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should select one option" do
|
|
|
|
@session.select("Ruby", :from => 'Language')
|
2010-05-31 23:16:58 -04:00
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['languages']).to eq(['Ruby'])
|
2010-05-31 23:16:58 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should select multiple options" do
|
|
|
|
@session.select("Ruby", :from => 'Language')
|
|
|
|
@session.select("Javascript", :from => 'Language')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['languages']).to include('Ruby', 'Javascript')
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2010-01-29 19:02:26 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should remain selected if already selected" do
|
|
|
|
@session.select("Ruby", :from => 'Language')
|
|
|
|
@session.select("Javascript", :from => 'Language')
|
|
|
|
@session.select("Ruby", :from => 'Language')
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['languages']).to include('Ruby', 'Javascript')
|
2010-01-29 19:02:26 -05:00
|
|
|
end
|
2010-02-17 18:55:11 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return value attribute rather than content if present" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find_field('Underwear').value).to include('thermal')
|
2010-02-17 18:55:11 -05:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2013-02-24 10:58:46 -05:00
|
|
|
|
|
|
|
context "with :exact option" do
|
|
|
|
context "when `false`" do
|
|
|
|
it "can match select box approximately" do
|
|
|
|
@session.select("Finish", :from => "Loc", :exact => false)
|
|
|
|
@session.click_button("awesome")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)["locale"]).to eq("fi")
|
2013-02-24 10:58:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can match option approximately" do
|
|
|
|
@session.select("Fin", :from => "Locale", :exact => false)
|
|
|
|
@session.click_button("awesome")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)["locale"]).to eq("fi")
|
2013-02-24 10:58:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can match option approximately when :from not given" do
|
|
|
|
@session.select("made-up language", :exact => false)
|
|
|
|
@session.click_button("awesome")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)["locale"]).to eq("jo")
|
2013-02-24 10:58:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when `true`" do
|
|
|
|
it "can match select box approximately" do
|
|
|
|
expect do
|
|
|
|
@session.select("Finish", :from => "Loc", :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can match option approximately" do
|
|
|
|
expect do
|
|
|
|
@session.select("Fin", :from => "Locale", :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can match option approximately when :from not given" do
|
|
|
|
expect do
|
|
|
|
@session.select("made-up language", :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|