2018-04-18 20:45:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-06 17:59:11 +00:00
|
|
|
Capybara::SpecHelper.spec Capybara::Selector do
|
|
|
|
before do
|
|
|
|
@session.visit('/form')
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
describe ':label selector' do
|
|
|
|
it 'finds a label by text' do
|
2016-04-06 17:59:11 +00:00
|
|
|
expect(@session.find(:label, 'Customer Name').text).to eq 'Customer Name'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'finds a label by for attribute string' do
|
2016-04-06 17:59:11 +00:00
|
|
|
expect(@session.find(:label, for: 'form_other_title')['for']).to eq 'form_other_title'
|
|
|
|
end
|
|
|
|
|
2019-05-12 21:09:19 +00:00
|
|
|
it 'finds a label for for attribute regex' do
|
|
|
|
expect(@session.find(:label, for: /_other_title/)['for']).to eq 'form_other_title'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'finds a label from nested input using :for filter with id string' do
|
2016-09-28 18:49:08 +00:00
|
|
|
expect(@session.find(:label, for: 'nested_label').text).to eq 'Nested Label'
|
|
|
|
end
|
|
|
|
|
2019-05-12 21:09:19 +00:00
|
|
|
it 'finds a label from nested input using :for filter with id regexp' do
|
|
|
|
expect(@session.find(:label, for: /nested_lab/).text).to eq 'Nested Label'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'finds a label from nested input using :for filter with element' do
|
2016-04-06 17:59:11 +00:00
|
|
|
input = @session.find(:id, 'nested_label')
|
|
|
|
expect(@session.find(:label, for: input).text).to eq 'Nested Label'
|
|
|
|
end
|
|
|
|
|
2019-10-19 18:49:12 +00:00
|
|
|
it 'finds a label from nested input using :for filter with element when no id on label' do
|
|
|
|
input = @session.find(:css, '#wrapper_label').find(:css, 'input')
|
|
|
|
expect(@session.find(:label, for: input).text).to eq 'Wrapper Label'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'finds the label for an non-nested element when using :for filter' do
|
2016-04-06 17:59:11 +00:00
|
|
|
select = @session.find(:id, 'form_other_title')
|
|
|
|
expect(@session.find(:label, for: select)['for']).to eq 'form_other_title'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
context 'with exact option' do
|
|
|
|
it 'matches substrings' do
|
2016-04-06 17:59:11 +00:00
|
|
|
expect(@session.find(:label, 'Customer Na', exact: false).text).to eq 'Customer Name'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't match substrings" do
|
|
|
|
expect { @session.find(:label, 'Customer Na', exact: true) }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
describe 'field selectors' do
|
2018-10-01 20:15:00 +00:00
|
|
|
context 'with :id option' do
|
|
|
|
it 'can find specifically by id' do
|
|
|
|
expect(@session.find(:field, id: 'customer_email').value).to eq 'ben@ben.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find by regex' do
|
|
|
|
expect(@session.find(:field, id: /ustomer.emai/).value).to eq 'ben@ben.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find by case-insensitive id' do
|
|
|
|
expect(@session.find(:field, id: /StOmer.emAI/i).value).to eq 'ben@ben.com'
|
|
|
|
end
|
2016-04-26 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
2019-12-08 14:21:10 +00:00
|
|
|
it 'can find specifically by name string' do
|
2018-07-10 21:18:39 +00:00
|
|
|
expect(@session.find(:field, name: 'form[other_title]')['id']).to eq 'form_other_title'
|
2016-04-26 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
2019-12-08 14:21:10 +00:00
|
|
|
it 'can find specifically by name regex' do
|
|
|
|
expect(@session.find(:field, name: /form\[other_.*\]/)['id']).to eq 'form_other_title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can find specifically by placeholder string' do
|
2018-07-10 21:18:39 +00:00
|
|
|
expect(@session.find(:field, placeholder: 'FirstName')['id']).to eq 'form_first_name'
|
2016-04-26 15:51:22 +00:00
|
|
|
end
|
2016-08-18 20:27:35 +00:00
|
|
|
|
2019-12-08 14:21:10 +00:00
|
|
|
it 'can find specifically by placeholder regex' do
|
|
|
|
expect(@session.find(:field, placeholder: /FirstN.*/)['id']).to eq 'form_first_name'
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'can find by type' do
|
2016-08-18 20:27:35 +00:00
|
|
|
expect(@session.find(:field, 'Confusion', type: 'checkbox')['id']).to eq 'confusion_checkbox'
|
|
|
|
expect(@session.find(:field, 'Confusion', type: 'text')['id']).to eq 'confusion_text'
|
|
|
|
expect(@session.find(:field, 'Confusion', type: 'textarea')['id']).to eq 'confusion_textarea'
|
|
|
|
end
|
2016-09-07 07:34:15 +00:00
|
|
|
|
2018-07-10 21:18:39 +00:00
|
|
|
it 'can find by class' do
|
2016-09-07 07:34:15 +00:00
|
|
|
expect(@session.find(:field, class: 'confusion-checkbox')['id']).to eq 'confusion_checkbox'
|
|
|
|
expect(@session).to have_selector(:field, class: 'confusion', count: 3)
|
2019-06-25 20:46:33 +00:00
|
|
|
expect(@session.find(:field, class: %w[confusion confusion-textarea])['id']).to eq 'confusion_textarea'
|
2016-09-07 07:34:15 +00:00
|
|
|
end
|
2016-04-26 15:51:22 +00:00
|
|
|
end
|
2018-03-01 00:11:41 +00:00
|
|
|
end
|