From 2906e5f57edb6ad26de2d0bbaf197e69385dcb92 Mon Sep 17 00:00:00 2001 From: Shamir Khodzha Date: Fri, 28 Feb 2014 20:32:09 +0400 Subject: [PATCH] added submit event triggering --- lib/capybara/poltergeist/client/agent.coffee | 13 ++++++++++--- lib/capybara/poltergeist/client/compiled/agent.js | 15 ++++++++++++--- spec/integration/session_spec.rb | 6 ++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/capybara/poltergeist/client/agent.coffee b/lib/capybara/poltergeist/client/agent.coffee index df0fa48d..391218d5 100644 --- a/lib/capybara/poltergeist/client/agent.coffee +++ b/lib/capybara/poltergeist/client/agent.coffee @@ -75,7 +75,8 @@ class PoltergeistAgent.Node @EVENTS = { FOCUS: ['blur', 'focus', 'focusin', 'focusout'], MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', - 'mouseover', 'mouseout', 'mouseup'] + 'mouseover', 'mouseout', 'mouseup'], + FORM: ['submit'] } constructor: (@agent, @element) -> @@ -272,13 +273,19 @@ class PoltergeistAgent.Node false, false, false, false, 0, null ) else if Node.EVENTS.FOCUS.indexOf(name) != -1 - event = document.createEvent('HTMLEvents') - event.initEvent(name, true, true) + event = this.obtainEvent(name) + else if Node.EVENTS.FORM.indexOf(name) != -1 + event = this.obtainEvent(name) else throw "Unknown event" @element.dispatchEvent(event) + obtainEvent: (name) -> + event = document.createEvent('HTMLEvents') + event.initEvent(name, true, true) + event + mouseEventTest: (x, y) -> frameOffset = this.frameOffset() diff --git a/lib/capybara/poltergeist/client/compiled/agent.js b/lib/capybara/poltergeist/client/compiled/agent.js index 0d7036ea..7a214784 100644 --- a/lib/capybara/poltergeist/client/compiled/agent.js +++ b/lib/capybara/poltergeist/client/compiled/agent.js @@ -145,7 +145,8 @@ PoltergeistAgent.InvalidSelector = (function() { PoltergeistAgent.Node = (function() { Node.EVENTS = { FOCUS: ['blur', 'focus', 'focusin', 'focusout'], - MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup'] + MOUSE: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', 'mouseout', 'mouseup'], + FORM: ['submit'] }; function Node(agent, element) { @@ -405,14 +406,22 @@ PoltergeistAgent.Node = (function() { event = document.createEvent('MouseEvent'); event.initMouseEvent(name, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); } else if (Node.EVENTS.FOCUS.indexOf(name) !== -1) { - event = document.createEvent('HTMLEvents'); - event.initEvent(name, true, true); + event = this.obtainEvent(name); + } else if (Node.EVENTS.FORM.indexOf(name) !== -1) { + event = this.obtainEvent(name); } else { throw "Unknown event"; } return this.element.dispatchEvent(event); }; + Node.prototype.obtainEvent = function(name) { + var event; + event = document.createEvent('HTMLEvents'); + event.initEvent(name, true, true); + return event; + }; + Node.prototype.mouseEventTest = function(x, y) { var el, frameOffset, origEl; frameOffset = this.frameOffset(); diff --git a/spec/integration/session_spec.rb b/spec/integration/session_spec.rb index c474aed5..6d48699d 100644 --- a/spec/integration/session_spec.rb +++ b/spec/integration/session_spec.rb @@ -555,6 +555,12 @@ expect { @session.find(:xpath, '#remove_me') }.to raise_error(Capybara::Poltergeist::InvalidSelector) end + it 'should submit form' do + @session.visit('/poltergeist/send_keys') + @session.find(:css, '#without_submit_button').trigger('submit') + expect(@session.find(:css, '#without_submit_button input').value).to eq('Submitted') + end + context 'whitespace stripping tests' do before do @session.visit '/poltergeist/filter_text_test'