diff --git a/README.md b/README.md index 3a0e5bb..194f4ad 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,12 @@ If select2 field has [tags](http://ivaynberg.github.io/select2/#tags) option you select2_tag('value', from: 'Label of input') ``` +To deselect tag just do the following: + +```ruby +select2_deselect_tag('value', from: 'Label of input') +``` + ## Contributing 1. Fork it diff --git a/gem/lib/capybara/selectors/tag_selector.rb b/gem/lib/capybara/selectors/tag_selector.rb index 3448364..7742167 100644 --- a/gem/lib/capybara/selectors/tag_selector.rb +++ b/gem/lib/capybara/selectors/tag_selector.rb @@ -2,13 +2,24 @@ module Capybara module Selectors module TagSelector def select2_tag(value, options = {}) + find_select2_input(options).set(value) + find('.select2-drop li', text: value).click + end + + def select2_deselect_tag(value, options = {}) + find_select2_input(options).parent + .find('.select2-choices div', text: value) + .find(:xpath, 'following-sibling::a').click + end + + private + + def find_select2_input(options) if options[:from] - find(:fillable_field, options[:from]).set(value) + find(:fillable_field, options[:from]) else - find('input.select2-input').set(value) + find('input.select2-input') end - - find('.select2-drop li', text: value).click end end end