-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play with idea of plugins to support actions with common UI libraries
- Loading branch information
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Capybara | ||
module Node | ||
module Pluginify | ||
def self.prepended(mod) | ||
mod.public_instance_methods.each do |method_name| | ||
define_method method_name do |*args, using: nil, **options| | ||
if using | ||
plugin = Capybara.plugins[using] | ||
raise ArgumentError, "Plugin not loaded: #{using}" unless plugin | ||
raise NoMethodError, "Action not implemented in plugin: #{using}:#{method_name}" unless plugin.respond_to?(method_name) | ||
plugin.send(method_name, self, *args, **options) | ||
else | ||
super(*args, **options) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Capybara | ||
module Plugins | ||
class Select2 | ||
def select(scope, value, from: nil, **options) | ||
select2 = if from | ||
scope.find(:select2, from, options.merge(visible: false)) | ||
else | ||
select = scope.find(:option, value, options).ancestor(:css, 'select', visible: false) | ||
select.find(:xpath, XPath.next_sibling(:span)[XPath.attr(:class).contains_word('select2')][XPath.attr(:class).contains_word('select2-container')]) | ||
end | ||
select2.click | ||
scope.find(:select2_option, value).click | ||
end | ||
end | ||
end | ||
end | ||
|
||
Capybara.add_selector(:select2) do | ||
xpath do |locator, **options| | ||
xpath = XPath.descendant(:select) | ||
xpath = locate_field(xpath, locator, options) | ||
xpath = xpath.next_sibling(:span)[XPath.attr(:class).contains_word('select2')][XPath.attr(:class).contains_word('select2-container')] | ||
xpath | ||
end | ||
end | ||
|
||
Capybara.add_selector(:select2_option) do | ||
xpath do |locator| | ||
xpath = XPath.anywhere(:ul)[XPath.attr(:class).contains_word('select2-results__options')][XPath.attr(:id)] | ||
xpath = xpath.descendant(:li)[XPath.attr(:role) == 'treeitem'] | ||
xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil? | ||
xpath | ||
end | ||
end | ||
|
||
Capybara.register_plugin(:select2, Capybara::Plugins::Select2.new) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'capybara/plugins/select2' | ||
|
||
Capybara::SpecHelper.spec 'Plugin', requires: [:js] do | ||
before do | ||
@session.visit('https://select2.org/appearance') | ||
end | ||
|
||
it 'should raise if wrong plugin specified' do | ||
expect do | ||
@session.select 'Florida', from: 'Click this to focus the single select element', using: :select3 | ||
end.to raise_error(ArgumentError, /Plugin not loaded/) | ||
end | ||
|
||
it 'should raise if non-implemented action is called' do | ||
expect do | ||
@session.click_on('blah', using: :select2) | ||
end.to raise_error(NoMethodError, /Action not implemented/) | ||
end | ||
|
||
it 'should select an option' do | ||
@session.select 'Florida', from: 'Click this to focus the single select element', using: :select2 | ||
|
||
expect(@session).to have_field(type: 'select', with: 'FL', visible: false) | ||
end | ||
|
||
it 'should work with multiple select' do | ||
@session.select 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2 | ||
@session.select 'California', from: 'Click this to focus the multiple select element', using: :select2 | ||
|
||
expect(@session).to have_select(multiple: true, selected: %w[Pennsylvania California], visible: false) | ||
end | ||
|
||
it 'should work with id' do | ||
@session.select 'Florida', from: 'id_label_single', using: :select2 | ||
expect(@session).to have_field(type: 'select', with: 'FL', visible: false) | ||
end | ||
|
||
it 'works without :from' do | ||
@session.within(:css, 'div.s2-example:nth-of-type(2) p:first-child') do | ||
@session.select 'Florida', using: :select2 | ||
expect(@session).to have_field(type: 'select', with: 'FL', visible: false) | ||
end | ||
end | ||
end |