Skip to content

Instantly share code, notes, and snippets.

@patorash
Created June 2, 2017 07:54
Show Gist options
  • Save patorash/2591e34809c34a8168689eb2185f0275 to your computer and use it in GitHub Desktop.
Save patorash/2591e34809c34a8168689eb2185f0275 to your computer and use it in GitHub Desktop.
Capybara::Node::Actionsを開いて、click_linkとclick_buttonで例外が発生したらtriggerを使って処理を行うようにしてみた
module Capybara
module Node
module Actions
def new_click_button(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
begin
find(:button, locator, options).click
rescue Capybara::Poltergeist::MouseEventFailed => e
find(:button, locator, options).trigger('click')
end
end
def new_click_link(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
begin
find(:link, locator, options).click
rescue Capybara::Poltergeist::MouseEventFailed => e
find(:link, locator, options).trigger('click')
end
end
alias_method :old_click_button, :click_button
alias_method :click_button, :new_click_button
alias_method :old_click_link, :click_link
alias_method :click_link, :new_click_link
end
end
end
@kazuhisa
Copy link

kazuhisa commented Jun 14, 2017

BOND GATEはTurnipのweb_stepの中で次のようにクリックしてる。

step 'I follow :link' do |link|
  first(:link, link).click
end

なので、click_buttonやclick_linkは使ってないことになる。
first(:link, link)Capybara::Node:Elementクラスなので次のようにclickをこじ開けて対応。

module Capybara
  module Node
    class Element
      def click
        synchronize do
          begin
            base.click
          rescue Capybara::Poltergeist::MouseEventFailed
            base.trigger('click')
          end
        end
        return self
      end
    end
  end
end

...しようかと思ったんだけど再現性が無いので検証もできずちょっとためらってる。
BOND GATEでCapybara::Poltergeist::MouseEventFailed出ない気がするのは、きっとrspec-retry.gemが入ってるから。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment